From 0b20b10f5f8370cbd018a7ae5d6ca09686c1a7b3 Mon Sep 17 00:00:00 2001 From: flifloo Date: Thu, 20 Aug 2020 11:48:28 +0200 Subject: [PATCH] Add sandwiches management to routes --- app.js | 2 - locales/en.json | 13 +- locales/fr.json | 119 +++++++++--------- public/javascripts/admin/sandwiches.js | 5 + public/javascripts/{commands.js => orders.js} | 0 public/stylesheets/style.css | 4 +- routes/admin/index.js | 4 +- routes/admin/sandwiches/edit.js | 40 ++++++ routes/admin/sandwiches/index.js | 29 +++++ views/admin/sandwiches/edit.pug | 15 +++ views/admin/sandwiches/index.pug | 17 +++ views/profile.pug | 2 +- views/sandwiches.pug | 2 +- 13 files changed, 184 insertions(+), 68 deletions(-) create mode 100644 public/javascripts/admin/sandwiches.js rename public/javascripts/{commands.js => orders.js} (100%) create mode 100644 routes/admin/sandwiches/edit.js create mode 100644 routes/admin/sandwiches/index.js create mode 100644 views/admin/sandwiches/edit.pug create mode 100644 views/admin/sandwiches/index.pug diff --git a/app.js b/app.js index 368c626..8850074 100644 --- a/app.js +++ b/app.js @@ -14,7 +14,6 @@ let ordersRouter = require("./routes/orders"); let sandwichesRouter = require("./routes/sandwiches"); let profileRouter = require("./routes/profile"); let adminRouter = require("./routes/admin"); -let adminOrdersRouter = require("./routes/admin/orders"); let app = express(); let sess = { @@ -55,7 +54,6 @@ app.use("/orders", ordersRouter); app.use("/sandwiches", sandwichesRouter); app.use("/profile", profileRouter); app.use("/admin", adminRouter); -app.use("/admin/orders", adminOrdersRouter); // catch 404 and forward to error handler app.use((req, res) => { diff --git a/locales/en.json b/locales/en.json index 95daf44..9c8a5c1 100644 --- a/locales/en.json +++ b/locales/en.json @@ -32,8 +32,7 @@ }, "profile": { "title": "Profile", - "infos": "Infos", - "save": "Save" + "infos": "Infos" }, "admin": { "title": "Administration", @@ -43,7 +42,12 @@ "sandwichManagement": "Sandwich management", "manageSandwiches": "Manage sandwiches", "userManagement": "User management", - "manageUsers": "Manage users" + "manageUsers": "Manage users", + "add": "Add", + "edit": "Edit", + "remove": "Remove", + "name": "Name,", + "price": "Price" }, "firstName": "First name", "lastName": "Last name", @@ -54,5 +58,6 @@ "orders": "Orders", "sandwiches": "Sandwiches", "date": "Date", - "order": "Order" + "order": "Order", + "save": "Save" } diff --git a/locales/fr.json b/locales/fr.json index 328a84a..e895b95 100644 --- a/locales/fr.json +++ b/locales/fr.json @@ -1,58 +1,63 @@ { - "layout": { - "home": "Accueil", - "logout": "Se déconnecter", - "login": "S'identifier", - "register": "S'inscrire", - "about": "À propos", - "aboutDescription": "Ce logiciel répond à la crise sanitaire du COVID-19 pour l'approvisionnement alimentaire des bureaux d'étudiants suivants", - "bio": "BDE Bio", - "chemistry": "BDE Chimie", - "GC": "BDE GC", - "GCPD": "BDE GCPD", - "GEA": "BDE GEA", - "IT": "BDE Info", - "made": "Fait avec ❤️ par", - "contact": "Contact", - "orderIssue": "Problème de commande" - }, - "index": { - "welcome": "Bienvenue sur Sandwiches Order Doua", - "sandwich": "Sandwich", - "day": "Jour", - "pay": "Payer" - }, - "login": { - "title": "Se connecter", - "submit": "Connexion" - }, - "register": { - "title": "S'inscrire", - "submit": "Envoyer" - }, - "profile": { - "title": "Profil", - "infos": "Infos", - "save": "Enregistrer" - }, - "admin": { - "title": "Administration", - "ordersManagement": "Gestion des commandes", - "ordersDate": "Date des commandes", - "manageOrders": "Gérer les commandes", - "sandwichManagement": "Gestion des sandwichs", - "manageSandwiches": "Gérer les sandwichs", - "userManagement": "Gestion des utilisateurs", - "manageUsers": "Gérer les utilisateurs" - }, - "firstName": "Prénom", - "lastName": "Nom de famille", - "username": "Nom d'utilisateur", - "password": "Mot de passe", - "email": "Email", - "department": "Département", - "orders": "Commandes", - "sandwiches": "Sandwichs", - "date": "Date", - "order": "Commande" -} + "layout": { + "home": "Accueil", + "logout": "Se déconnecter", + "login": "S'identifier", + "register": "S'inscrire", + "about": "À propos", + "aboutDescription": "Ce logiciel répond à la crise sanitaire du COVID-19 pour l'approvisionnement alimentaire des bureaux d'étudiants suivants", + "bio": "BDE Bio", + "chemistry": "BDE Chimie", + "GC": "BDE GC", + "GCPD": "BDE GCPD", + "GEA": "BDE GEA", + "IT": "BDE Info", + "made": "Fait avec ❤️ par", + "contact": "Contact", + "orderIssue": "Problème de commande" + }, + "index": { + "welcome": "Bienvenue sur Sandwiches Order Doua", + "sandwich": "Sandwich", + "day": "Jour", + "pay": "Payer" + }, + "login": { + "title": "Se connecter", + "submit": "Connexion" + }, + "register": { + "title": "S'inscrire", + "submit": "Envoyer" + }, + "profile": { + "title": "Profil", + "infos": "Infos" + }, + "admin": { + "title": "Administration", + "ordersManagement": "Gestion des commandes", + "ordersDate": "Date des commandes", + "manageOrders": "Gérer les commandes", + "sandwichManagement": "Gestion des sandwichs", + "manageSandwiches": "Gérer les sandwichs", + "userManagement": "Gestion des utilisateurs", + "manageUsers": "Gérer les utilisateurs", + "add": "Ajouter", + "edit": "Éditer", + "remove": "Supprimer", + "name": "Nom", + "price": "Prix" + }, + "firstName": "Prénom", + "lastName": "Nom de famille", + "username": "Nom d'utilisateur", + "password": "Mot de passe", + "email": "Email", + "department": "Département", + "orders": "Commandes", + "sandwiches": "Sandwichs", + "date": "Date", + "order": "Commande", + "save": "Enregistrer" +} \ No newline at end of file diff --git a/public/javascripts/admin/sandwiches.js b/public/javascripts/admin/sandwiches.js new file mode 100644 index 0000000..097c76e --- /dev/null +++ b/public/javascripts/admin/sandwiches.js @@ -0,0 +1,5 @@ +document.querySelectorAll("a.remove") + .forEach(e => e.addEventListener("click", ev => { + if (!confirm("Do you really want to remove this sandwich ?")) + ev.preventDefault(); + })); diff --git a/public/javascripts/commands.js b/public/javascripts/orders.js similarity index 100% rename from public/javascripts/commands.js rename to public/javascripts/orders.js diff --git a/public/stylesheets/style.css b/public/stylesheets/style.css index f1e8cff..9cef70e 100644 --- a/public/stylesheets/style.css +++ b/public/stylesheets/style.css @@ -190,7 +190,7 @@ p.before-link a::before { overflow-y: auto; } -#ordersManagement { +#ordersManagement, #sandwichesManagement { max-height: 90%; } @@ -198,7 +198,7 @@ p.before-link a::before { margin: 0.2em 0 0 0; } -#ordersManagement>div { +#ordersManagement>div, #sandwichesManagement>div { overflow-y: auto; } diff --git a/routes/admin/index.js b/routes/admin/index.js index af72cf4..f705b26 100644 --- a/routes/admin/index.js +++ b/routes/admin/index.js @@ -11,6 +11,8 @@ router.get("/", sessionCheck(3), async (req, res) => { sandwiches: await models.Sandwich.findAll(), users: await models.User.findAll() }); -}); +}) + .use("/orders", require("./orders")) + .use("/sandwiches", require("./sandwiches")); module.exports = router; diff --git a/routes/admin/sandwiches/edit.js b/routes/admin/sandwiches/edit.js new file mode 100644 index 0000000..b648cc0 --- /dev/null +++ b/routes/admin/sandwiches/edit.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) => { + if (!req.query.name) + res.render("error", {message: "Can't edit sandwich !", error: {status: "Missing arg"}}); + else { + let sandwich = await models.Sandwich.findByPk(req.query.name); + if (!sandwich) + res.render("error", + {message: "Can't edit sandwich !", error: {status: "Sandwich not found"}}); + else + res.render("admin/sandwiches/edit", { + title: "SOD - Sandwiches administration", + user: req.session.user, + sandwich: sandwich + }); + } +}).post("/", sessionCheck(3), async (req, res) => { + if (!req.body.name) + res.render("error", {message: "Fail to edit sandwich !", error: {status: "Missing arg"}}); + else { + let sandwich = await models.Sandwich.findByPk(req.body.name); + if (!sandwich) + res.render("error", + {message: "Fail to edit sandwich !", error: {status: "Invalid sandwich name"}}); + else { + if (req.body.price && req.body.price !== sandwich.price) + sandwich.price = req.body.price; + await sandwich.save(); + if (req.body.newName && req.body.newName !== sandwich.name) + await models.Sandwich.update({name: req.body.newName}, {where: {name: req.body.name}}); + res.redirect("/admin/sandwiches"); + } + } +}); + +module.exports = router; diff --git a/routes/admin/sandwiches/index.js b/routes/admin/sandwiches/index.js new file mode 100644 index 0000000..36fe5f1 --- /dev/null +++ b/routes/admin/sandwiches/index.js @@ -0,0 +1,29 @@ +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/sandwiches/index", { + title: "SOD - Sandwiches administration", + user: req.session.user, + sandwiches: await models.Sandwich.findAll() + }); +}) + .use("/edit", require("./edit")) + .get("/delete", sessionCheck(3), async (req, res) => { + if (!req.query.name) + res.render("error", {message: "Can't remove sandwich !", error: {status: "Missing arg"}}); + else { + let sandwich = await models.Sandwich.findByPk(req.query.name); + if (!sandwich) + res.render("error", {message: "Can't remove sandwich !", error: {status: "Invalid sandwich"}}); + else { + await sandwich.destroy(); + res.redirect("/admin/sandwiches"); + } + } + }); + +module.exports = router; diff --git a/views/admin/sandwiches/edit.pug b/views/admin/sandwiches/edit.pug new file mode 100644 index 0000000..c71b324 --- /dev/null +++ b/views/admin/sandwiches/edit.pug @@ -0,0 +1,15 @@ +extends ../../layout + +block content + div.card#sandwichesManagement + h1=__("admin.edit")+" "+sandwich.name + form(action="/admin/sandwiches/edit" method="POST") + input.hide(type="text" name="name" value=sandwich.name required) + div.field + label(for="name")=__("admin.name")+":" + input#name(type="text" name="newName" value=sandwich.name required) + div.field + label(for="price")=__("admin.price") + ":" + input#price(type="number" min="0" step="any" name="price" value=sandwich.price required) + div.field + input(type="submit" value=__("save")) diff --git a/views/admin/sandwiches/index.pug b/views/admin/sandwiches/index.pug new file mode 100644 index 0000000..843b25f --- /dev/null +++ b/views/admin/sandwiches/index.pug @@ -0,0 +1,17 @@ +extends ../../layout + +block content + div.card#sandwichesManagement + h1=__("admin.sandwichManagement") + input.add(type="button" value=__("admin.add")) + div + each sandwich in sandwiches + div.sandwich + h2=sandwich.name + div.buttons + a.edit(href="/admin/sandwiches/edit?name="+sandwich.name) + button=__("admin.edit") + a.remove(href="/admin/sandwiches/delete?name="+sandwich.name) + button=__("admin.remove") + + script(src="/javascripts/admin/sandwiches.js") diff --git a/views/profile.pug b/views/profile.pug index 12a01be..e743d7c 100644 --- a/views/profile.pug +++ b/views/profile.pug @@ -24,7 +24,7 @@ block content label(for="password")=__("password")+":" input#password(type="password" name="password") div.field - input(type="submit" value=__("profile.save")) + input(type="submit" value=__("save")) datalist#department-list each department in departments diff --git a/views/sandwiches.pug b/views/sandwiches.pug index 90f3ff1..72c2e81 100644 --- a/views/sandwiches.pug +++ b/views/sandwiches.pug @@ -10,4 +10,4 @@ block content div.sandwich h2 #{sandwich.SandwichName}: #{sandwich.dataValues.number} - script(src="/javascripts/sandwiches.js") + script(src="/javascripts/index.js")