akf-forum/routes/threads.js
2022-08-28 15:00:53 +03:00

32 lines
No EOL
761 B
JavaScript

const { Router } = require("express");
const app = Router();
const { ThreadModel, MessageModel } = require("../models")
app.get("/", async (req, res) => {
const threads = await ThreadModel.find(req.user?.admin ? {} : { deleted: false })//.limit(10);
return res.reply("threads", { threads });
});
app.get("/create*", (req, res) => res.reply("create_thread"));
app.get("/:id", async (req, res) => {
const { id } = req.params;
const thread = await ThreadModel.get(id);
thread.views++;
if (thread && (req.user?.admin || !thread.deleted))
res.reply("thread", { thread, scroll: req.query.scroll || false });
else
res.error(404, "We have not got this thread.");
thread.save();
});
module.exports = app;