Archived
1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
SOD/routes/admin/orders/index.js

40 lines
1.3 KiB
JavaScript
Raw Normal View History

2020-08-18 17:54:00 +02:00
let express = require("express");
let router = express.Router();
2020-08-24 23:23:46 +02:00
let sessionCheck = require("../../../middlewares/sessionCheck");
let models = require("../../../models");
let error = require("../../utils/error");
2020-08-18 17:54:00 +02:00
router.get("/", sessionCheck(3), async (req, res) => {
res.render("admin/orders", {
title: "SOD - Orders administration",
orders: await models.Order.findAll({include: models.Sandwich, order: ["date"]})
});
}).post("/order/delete", sessionCheck(3), async (req, res) => {
if (!req.body.id)
2020-08-20 12:14:38 +02:00
return error(req, res, "Fail to remove order !", 400, "Missing args");
2020-08-18 17:54:00 +02:00
try {
await (await models.Order.findByPk(req.body.id)).destroy();
res.redirect("/admin/orders");
} catch (e) {
2020-08-20 12:14:38 +02:00
error(req, res, "Fail to remove order !");
2020-08-18 17:54:00 +02:00
throw e;
}
}).post("/sandwich/delete", sessionCheck(3), async (req, res) => {
if (!req.body.id)
2020-08-20 12:14:38 +02:00
return error(req, res, "Fail to remove sandwich !", 400, "Missing args");
2020-08-18 17:54:00 +02:00
try {
let sandwich = await models.SandwichOrder.findByPk(req.body.id);
await sandwich.destroy();
res.redirect("/admin/orders");
} catch (e) {
2020-08-20 12:14:38 +02:00
error(req, res, "Fail to remove sandwich !");
2020-08-18 17:54:00 +02:00
throw e;
}
2020-08-24 23:23:46 +02:00
})
.use("/date", require("./date"));
2020-08-18 17:54:00 +02:00
module.exports = router;