Add department management to routes
This commit is contained in:
parent
83ed17efc4
commit
02429d6fde
12 changed files with 148 additions and 4 deletions
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
5
public/javascripts/admin/departments.js
Normal file
5
public/javascripts/admin/departments.js
Normal file
|
@ -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();
|
||||
}));
|
|
@ -217,7 +217,7 @@ p.before-link a::before {
|
|||
cursor: pointer;
|
||||
}
|
||||
|
||||
#sandwichesManagement>a>button {
|
||||
#sandwichesManagement>a>button, #departmentsManagement>a>button {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
|
|
23
routes/admin/departments/add.js
Normal file
23
routes/admin/departments/add.js
Normal file
|
@ -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;
|
35
routes/admin/departments/edit.js
Normal file
35
routes/admin/departments/edit.js
Normal file
|
@ -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;
|
29
routes/admin/departments/index.js
Normal file
29
routes/admin/departments/index.js
Normal file
|
@ -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;
|
|
@ -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;
|
||||
|
|
11
views/admin/departments/add.pug
Normal file
11
views/admin/departments/add.pug
Normal file
|
@ -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"))
|
12
views/admin/departments/edit.pug
Normal file
12
views/admin/departments/edit.pug
Normal file
|
@ -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"))
|
18
views/admin/departments/index.pug
Normal file
18
views/admin/departments/index.pug
Normal file
|
@ -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")
|
|
@ -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
|
||||
|
|
Reference in a new issue