const express = require("express");
constresponse = require("../../network/response");
const controller = require("./controller");
const router = express.Router();
router.get("/test", function (req, res) {
if (req.query.error == "ok") {
response.error(req, res, "Bad Request", 400, "Error is ...");
} else {
response.success(req, res, "Created", 201);
}
});
router.get("/", function (req, res) {
constfilter = req.query.user || null;
controller
.getMessages(filter)
.then((msg) => {
response.success(req, res, msg, 200);
})
.catch((err) => {
response.error(req, res, "Internal Error", 500, err);
});
});
router.post("/message", async function (req, res) {
const message = await controller
.addMessage(req.body.user, req.body.message)
.then((msg) => {
if (msg) {
response.success(req, res, msg, 201);
}
})
.catch((err) => {
response.error(req, res, "Bad Request", 400, err);
});
});
router.patch("/:id", function (req, res) {
const id = req.params.id;
const text = req.body.message;
controller
.updateMessage(id, text)
.then((data) => {
response.success(req, res, data, 200);
})
.catch((e) => {
response.error(req, res, "Internal Error", 500, e);
});
});
router.delete("/:id", function (req, res) {
controller
.deleteMessage(req.params.id)
.then(() => {
response.success(req, res, "User Deleted", 200);
})
.catch((e) => {
response.error(req, res, "Internal Error", 500, e);
});
});
module.exports = router;