From 02429d6fded6392dcba08ae0e44e3056ae4600a2 Mon Sep 17 00:00:00 2001 From: flifloo Date: Thu, 20 Aug 2020 13:34:50 +0200 Subject: [PATCH] Add department management to routes --- locales/en.json | 4 ++- locales/fr.json | 4 ++- public/javascripts/admin/departments.js | 5 ++++ public/stylesheets/style.css | 2 +- routes/admin/departments/add.js | 23 ++++++++++++++++ routes/admin/departments/edit.js | 35 +++++++++++++++++++++++++ routes/admin/departments/index.js | 29 ++++++++++++++++++++ routes/admin/index.js | 3 ++- views/admin/departments/add.pug | 11 ++++++++ views/admin/departments/edit.pug | 12 +++++++++ views/admin/departments/index.pug | 18 +++++++++++++ views/admin/index.pug | 6 +++++ 12 files changed, 148 insertions(+), 4 deletions(-) create mode 100644 public/javascripts/admin/departments.js create mode 100644 routes/admin/departments/add.js create mode 100644 routes/admin/departments/edit.js create mode 100644 routes/admin/departments/index.js create mode 100644 views/admin/departments/add.pug create mode 100644 views/admin/departments/edit.pug create mode 100644 views/admin/departments/index.pug diff --git a/locales/en.json b/locales/en.json index 2b5b93a..b4459dd 100644 --- a/locales/en.json +++ b/locales/en.json @@ -47,7 +47,9 @@ "remove": "Remove", "name": "Name,", "price": "Price", - "new": "New" + "new": "New", + "departmentsManagement": "Departments management", + "manageDepartments": "Manage departments" }, "firstName": "First name", "lastName": "Last name", diff --git a/locales/fr.json b/locales/fr.json index 0fc99c1..a85d267 100644 --- a/locales/fr.json +++ b/locales/fr.json @@ -47,7 +47,9 @@ "remove": "Supprimer", "name": "Nom", "price": "Prix", - "new": "Nouveau" + "new": "Nouveau", + "departmentsManagement": "Gestion des départements", + "manageDepartments": "Gérer les départements" }, "firstName": "Prénom", "lastName": "Nom de famille", diff --git a/public/javascripts/admin/departments.js b/public/javascripts/admin/departments.js new file mode 100644 index 0000000..b64f7b1 --- /dev/null +++ b/public/javascripts/admin/departments.js @@ -0,0 +1,5 @@ +document.querySelectorAll("a.remove") + .forEach(e => e.addEventListener("click", ev => { + if (!confirm("Do you really want to remove this department ?")) + ev.preventDefault(); + })); diff --git a/public/stylesheets/style.css b/public/stylesheets/style.css index b4e7aa3..1820216 100644 --- a/public/stylesheets/style.css +++ b/public/stylesheets/style.css @@ -217,7 +217,7 @@ p.before-link a::before { cursor: pointer; } -#sandwichesManagement>a>button { +#sandwichesManagement>a>button, #departmentsManagement>a>button { width: 100%; } diff --git a/routes/admin/departments/add.js b/routes/admin/departments/add.js new file mode 100644 index 0000000..251dd20 --- /dev/null +++ b/routes/admin/departments/add.js @@ -0,0 +1,23 @@ +let express = require("express"); +let router = express.Router(); +let sessionCheck = require("../../../middlewares/sessionCheck"); +let models = require("../../../models"); +let error = require("../../utils/error"); + +router.get("/", sessionCheck(3), (req, res) => { + res.render("admin/departments/add", { + title: "SOD - Departments administration", + user: req.session.user + }); +}).post("/", sessionCheck(3), async (req, res) => { + if (!req.body.name) + return error(req, res, "Fail to add department !", 400, "Missing arg"); + + if (await models.Department.findByPk(req.body.name)) + return error(req, res, "Fail to add department", 400, "Name already used"); + + await models.Department.create({name: req.body.name}); + res.redirect("/admin/departments"); +}); + +module.exports = router; diff --git a/routes/admin/departments/edit.js b/routes/admin/departments/edit.js new file mode 100644 index 0000000..89a5052 --- /dev/null +++ b/routes/admin/departments/edit.js @@ -0,0 +1,35 @@ +let express = require("express"); +let router = express.Router(); +let sessionCheck = require("../../../middlewares/sessionCheck"); +let models = require("../../../models"); +let error = require("../../utils/error"); + +router.get("/", sessionCheck(3), async (req, res) => { + if (!req.query.name) + return error(req, res, "Can't edit department !", 400, "Missing arg"); + + let department = await models.Department.findByPk(req.query.name); + if (!department) + return error(req, res, "Can't edit department !", 400, "Department not found"); + + res.render("admin/departments/edit", { + title: "SOD - Departments administration", + user: req.session.user, + department: department + }); +}).post("/", sessionCheck(3), async (req, res) => { + if (!req.body.name) + return error(req, res, "Fail to edit department !", 400, "Missing arg"); + + let department = await models.Department.findByPk(req.body.name); + if (!department) + return error(req, res, "Fail to edit department !", 400, "Invalid department name"); + + await department.save(); + if (req.body.newName && req.body.newName !== department.name) + await models.Department.update({name: req.body.newName}, {where: {name: req.body.name}}); + + res.redirect("/admin/departments"); +}); + +module.exports = router; diff --git a/routes/admin/departments/index.js b/routes/admin/departments/index.js new file mode 100644 index 0000000..ef4eaa1 --- /dev/null +++ b/routes/admin/departments/index.js @@ -0,0 +1,29 @@ +let express = require("express"); +let router = express.Router(); +let sessionCheck = require("../../../middlewares/sessionCheck"); +let models = require("../../../models"); +let error = require("../../utils/error"); + + +router.get("/", sessionCheck(3), async (req, res) => { + res.render("admin/departments/index", { + title: "SOD - Departments administration", + user: req.session.user, + departments: await models.Department.findAll() + }); +}) + .use("/edit", require("./edit")) + .use("/add", require("./add")) + .get("/delete", sessionCheck(3), async (req, res) => { + if (!req.query.name) + return error(req, res, "Can't remove department !", 400, "Missing arg"); + + let department = await models.Department.findByPk(req.query.name); + if (!department) + return error(req, res, "Can't remove department !", 400, "Invalid department"); + + await department.destroy(); + res.redirect("/admin/departments"); + }); + +module.exports = router; diff --git a/routes/admin/index.js b/routes/admin/index.js index f705b26..b296b5e 100644 --- a/routes/admin/index.js +++ b/routes/admin/index.js @@ -13,6 +13,7 @@ router.get("/", sessionCheck(3), async (req, res) => { }); }) .use("/orders", require("./orders")) - .use("/sandwiches", require("./sandwiches")); + .use("/sandwiches", require("./sandwiches")) + .use("/departments", require("./departments")); module.exports = router; diff --git a/views/admin/departments/add.pug b/views/admin/departments/add.pug new file mode 100644 index 0000000..150c50c --- /dev/null +++ b/views/admin/departments/add.pug @@ -0,0 +1,11 @@ +extends ../../layout + +block content + div.card#departmentsManagement + h1=__("admin.new")+" "+__("department") + form(action="/admin/departments/add" method="POST") + div.field + label(for="name")=__("admin.name")+":" + input#name(type="text" name="name" required) + div.field + input(type="submit" value=__("admin.add")) diff --git a/views/admin/departments/edit.pug b/views/admin/departments/edit.pug new file mode 100644 index 0000000..ef54167 --- /dev/null +++ b/views/admin/departments/edit.pug @@ -0,0 +1,12 @@ +extends ../../layout + +block content + div.card#departmentsManagement + h1=__("admin.edit")+" "+department.name + form(action="/admin/departments/edit" method="POST") + input.hide(type="text" name="name" value=department.name required) + div.field + label(for="name")=__("admin.name")+":" + input#name(type="text" name="newName" value=department.name required) + div.field + input(type="submit" value=__("save")) diff --git a/views/admin/departments/index.pug b/views/admin/departments/index.pug new file mode 100644 index 0000000..fdd70b7 --- /dev/null +++ b/views/admin/departments/index.pug @@ -0,0 +1,18 @@ +extends ../../layout + +block content + div.card#departmentsManagement + h1=__("admin.departmentsManagement") + a.add(href="/admin/departments/add") + button=__("admin.add") + div + each department in departments + div.department + h2=department.name + div.buttons + a.edit(href="/admin/departments/edit?name="+department.name) + button=__("admin.edit") + a.remove(href="/admin/departments/delete?name="+department.name) + button=__("admin.remove") + + script(src="/javascripts/admin/departments.js") diff --git a/views/admin/index.pug b/views/admin/index.pug index 0c09ce2..5afa682 100644 --- a/views/admin/index.pug +++ b/views/admin/index.pug @@ -17,6 +17,12 @@ block content a(href="/admin/sandwiches") button=__("admin.manageSandwiches") + div + h2=__("admin.departmentsManagement") + div.buttons + a(href="/admin/departments") + button=__("admin.manageDepartments") + div h2=__("admin.userManagement") div.buttons