mirror of
https://github.com/Akif9748/akf-forum.git
synced 2025-01-10 08:33:20 +03:00
43 lines
No EOL
1.2 KiB
JavaScript
43 lines
No EOL
1.2 KiB
JavaScript
const { Thread, User, Message } = require("../../../classes");
|
|
const ApiResponse = require("../ApiResponse");
|
|
const { Router } = require("express")
|
|
|
|
const app = Router();
|
|
|
|
app.get("/:id", async (req, res) => {
|
|
|
|
const error = (status, error) =>
|
|
res.status(status).json(new ApiResponse(status, { error }))
|
|
|
|
|
|
const { id = null } = req.params;
|
|
if (!id) return error(400, "Missing id in query")
|
|
|
|
const thread = await new Thread().getById(id);
|
|
if (!thread || thread.deleted) return error(404, "We have not got any thread declared as this id.");
|
|
|
|
res.status(200).json(new ApiResponse(200, thread));
|
|
|
|
});
|
|
|
|
|
|
app.post("/", async (req, res) => {
|
|
|
|
const error = (status, error) =>
|
|
res.status(status).json(new ApiResponse(status, { error }));
|
|
|
|
const { title = null, content = null } = req.body;
|
|
|
|
if (!content || !title) return error(400, "Missing content/title in request body.");
|
|
|
|
const user = await new User().getByName(req.headers.username)
|
|
const thread = await new Thread(title, user).takeId()
|
|
const message = await new Message(content, user, thread.id).takeId()
|
|
thread.push(message.id).write();
|
|
await message.write();
|
|
|
|
res.status(200).json(new ApiResponse(200, thread));
|
|
|
|
});
|
|
|
|
module.exports = app; |