Archived
1
0
Fork 0

Add commands management to routes

This commit is contained in:
Ethanell 2020-08-17 19:04:50 +02:00
parent c0a683d98e
commit 9a674f8acb
7 changed files with 105 additions and 16 deletions

2
app.js
View file

@ -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) => {

View file

@ -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;

View file

@ -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;

40
routes/admin/commands.js Normal file
View file

@ -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;

11
routes/admin/index.js Normal file
View file

@ -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;

20
views/admin/commands.pug Normal file
View file

@ -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")

View file

@ -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