From 9a674f8acb3fa922cb7d1889a1e0dd2f722f4f4b Mon Sep 17 00:00:00 2001 From: flifloo Date: Mon, 17 Aug 2020 19:04:50 +0200 Subject: [PATCH] Add commands management to routes --- app.js | 2 ++ public/stylesheets/style.css | 27 +++++++++++++++++++ routes/admin.js | 11 -------- routes/admin/commands.js | 40 ++++++++++++++++++++++++++++ routes/admin/index.js | 11 ++++++++ views/admin/commands.pug | 20 ++++++++++++++ views/{admin.pug => admin/index.pug} | 10 +++---- 7 files changed, 105 insertions(+), 16 deletions(-) delete mode 100644 routes/admin.js create mode 100644 routes/admin/commands.js create mode 100644 routes/admin/index.js create mode 100644 views/admin/commands.pug rename views/{admin.pug => admin/index.pug} (70%) diff --git a/app.js b/app.js index 561eead..4d1740c 100644 --- a/app.js +++ b/app.js @@ -14,6 +14,7 @@ let ordersRouter = require("./routes/orders"); let sandwichesRouter = require("./routes/sandwiches"); let profileRouter = require("./routes/profile"); let adminRouter = require("./routes/admin"); +let adminCommandsRouter = require("./routes/admin/commands"); let app = express(); let sess = { @@ -46,6 +47,7 @@ app.use("/orders", ordersRouter); app.use("/sandwiches", sandwichesRouter); app.use("/profile", profileRouter); app.use("/admin", adminRouter); +app.use("/admin/commands", adminCommandsRouter); // catch 404 and forward to error handler app.use((req, res) => { diff --git a/public/stylesheets/style.css b/public/stylesheets/style.css index 218b7da..e21c4b6 100644 --- a/public/stylesheets/style.css +++ b/public/stylesheets/style.css @@ -190,6 +190,33 @@ p.before-link a::before { overflow-y: auto; } +#commandsManagement { + max-height: 90%; +} + +#commandsManagement h2 { + margin: 0.2em 0 0 0; +} + +#commandsManagement>div { + overflow-y: auto; +} + +#commandsManagement .command { + box-shadow: 0.5em 0.5em 0.5em rgba(0, 0, 255, .2); +} + +#commandsManagement .command .title, #commandsManagement .sandwich { + display: flex; + justify-content: center; +} + +#commandsManagement input[value="x"] { + border: none; + background-color: unset; + cursor: pointer; +} + @media (hover: none) and (pointer: coarse) { body { font-size: xx-large; diff --git a/routes/admin.js b/routes/admin.js deleted file mode 100644 index 7eed48f..0000000 --- a/routes/admin.js +++ /dev/null @@ -1,11 +0,0 @@ -let express = require("express"); -let router = express.Router(); -let sessionCheck = require("../middlewares/sessionCheck"); -let models = require("../models"); - - -router.get("/", sessionCheck(3), async (req, res) => { - res.render("admin", {title: "SOD", user: req.session.user, sandwiches: await models.Sandwich.findAll(), users: await models.User.findAll()}); -}); - -module.exports = router; diff --git a/routes/admin/commands.js b/routes/admin/commands.js new file mode 100644 index 0000000..5e29642 --- /dev/null +++ b/routes/admin/commands.js @@ -0,0 +1,40 @@ +let express = require("express"); +let router = express.Router(); +let sessionCheck = require("../../middlewares/sessionCheck"); +let models = require("../../models"); + + +router.get("/", sessionCheck(3), async (req, res) => { + res.render("admin/commands", { + title: "SOD", + user: req.session.user, + commands: await models.Command.findAll({include: models.Sandwich, order: ["date"]}) + }); +}).post("/command/delete", sessionCheck(3), async (req, res) => { + if (!req.body.id) + res.render("error", {message: "Fail to remove command !", error: {status: "Missing args"}}); + try { + await (await models.Command.findByPk(req.body.id)).destroy(); + res.redirect("/admin/commands"); + } catch (e) { + res.render("error", {message: "Fail to remove command !", error: {}}); + throw e; + } +}).post("/sandwich/delete", sessionCheck(3), async (req, res) => { + if (!req.body.id) + res.render("error", {message: "Fail to remove sandwich !", error: {status: "Missing args"}}); + try { + let sandwich = await models.SandwichCommand.findByPk(req.body.id); + let command = await models.Command.findByPk(sandwich.CommandId, {include: models.Sandwich}); + await sandwich.destroy(); + await command.reload(); + if (!command.Sandwiches.length) + await command.destroy(); + res.redirect("/admin/commands"); + } catch (e) { + res.render("error", {message: "Fail to remove sandwich !", error: {}}); + throw e; + } +}); + +module.exports = router; diff --git a/routes/admin/index.js b/routes/admin/index.js new file mode 100644 index 0000000..97a87bd --- /dev/null +++ b/routes/admin/index.js @@ -0,0 +1,11 @@ +let express = require("express"); +let router = express.Router(); +let sessionCheck = require("../../middlewares/sessionCheck"); +let models = require("../../models"); + + +router.get("/", sessionCheck(3), async (req, res) => { + res.render("admin/index", {title: "SOD", user: req.session.user, sandwiches: await models.Sandwich.findAll(), users: await models.User.findAll()}); +}); + +module.exports = router; diff --git a/views/admin/commands.pug b/views/admin/commands.pug new file mode 100644 index 0000000..a5f7f39 --- /dev/null +++ b/views/admin/commands.pug @@ -0,0 +1,20 @@ +extends ../layout + +block content + div.card#commandsManagement + h1 Commands management + div + each command in commands + div.command + div.title + h2=command.id + form(action="/admin/commands/command/delete" method="POST") + input.hide(type="number" name="id" value=command.id) + input(type="submit" value="x") + h3 #{command.firstName} #{command.lastName} + each sandwich in command.Sandwiches + div.sandwich + p #{sandwich.name} - #{sandwich.SandwichCommand.date} + form(action="/admin/commands/sandwich/delete" method="POST") + input.hide(type="number" name="id" value=sandwich.SandwichCommand.id) + input(type="submit" value="x") diff --git a/views/admin.pug b/views/admin/index.pug similarity index 70% rename from views/admin.pug rename to views/admin/index.pug index 93599cf..cd6b079 100644 --- a/views/admin.pug +++ b/views/admin/index.pug @@ -1,15 +1,15 @@ -extends layout +extends ../layout block content div.card h1 Administration div - h2 Order management + h2 Commands management div.buttons - a(href="/admin/orders/date") + a(href="/admin/commands/date") button Orders date - a(href="/admin/order") - button Manage orders + a(href="/admin/commands") + button Manage commands div h2 Sandwich management