Archived
1
0
Fork 0

Add sandwiches management to routes

This commit is contained in:
Ethanell 2020-08-20 11:48:28 +02:00
parent 152ac5cc15
commit 0b20b10f5f
13 changed files with 184 additions and 68 deletions

2
app.js
View file

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

View file

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

View file

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

View file

@ -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();
}));

View file

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

View file

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

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

View file

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

View file

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

View file

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

View file

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

View file

@ -10,4 +10,4 @@ block content
div.sandwich
h2 #{sandwich.SandwichName}: #{sandwich.dataValues.number}
script(src="/javascripts/sandwiches.js")
script(src="/javascripts/index.js")