From 83ed17efc48b76151566c974f926cd11d2be1544 Mon Sep 17 00:00:00 2001 From: flifloo Date: Thu, 20 Aug 2020 13:11:25 +0200 Subject: [PATCH] Add sandwich administration add to routes --- app.js | 2 +- locales/en.json | 7 ++++--- locales/fr.json | 7 ++++--- public/stylesheets/style.css | 4 ++++ routes/admin/sandwiches/add.js | 23 +++++++++++++++++++++++ routes/admin/sandwiches/index.js | 1 + views/admin/sandwiches/add.pug | 14 ++++++++++++++ views/admin/sandwiches/index.pug | 3 ++- views/index.pug | 2 +- 9 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 routes/admin/sandwiches/add.js create mode 100644 views/admin/sandwiches/add.pug diff --git a/app.js b/app.js index afac2d8..6d99b94 100644 --- a/app.js +++ b/app.js @@ -58,7 +58,7 @@ app.use("/admin", adminRouter); // catch 404 and forward to error handler app.use((req, res) => { res.status(404); - res.render("error", {message: "Page not found", "error": {}}) + res.render("error", {title: "SOD - Page not found", user: req.session.user, message: "Page not found", "error": {}}) }); // error handler diff --git a/locales/en.json b/locales/en.json index 9c8a5c1..2b5b93a 100644 --- a/locales/en.json +++ b/locales/en.json @@ -18,7 +18,6 @@ }, "index": { "welcome": "Welcome to Sandwiches Order Doua", - "sandwich": "Sandwich", "day": "Day", "pay": "Pay" }, @@ -47,7 +46,8 @@ "edit": "Edit", "remove": "Remove", "name": "Name,", - "price": "Price" + "price": "Price", + "new": "New" }, "firstName": "First name", "lastName": "Last name", @@ -59,5 +59,6 @@ "sandwiches": "Sandwiches", "date": "Date", "order": "Order", - "save": "Save" + "save": "Save", + "sandwich": "Sandwich" } diff --git a/locales/fr.json b/locales/fr.json index e895b95..0fc99c1 100644 --- a/locales/fr.json +++ b/locales/fr.json @@ -18,7 +18,6 @@ }, "index": { "welcome": "Bienvenue sur Sandwiches Order Doua", - "sandwich": "Sandwich", "day": "Jour", "pay": "Payer" }, @@ -47,7 +46,8 @@ "edit": "Éditer", "remove": "Supprimer", "name": "Nom", - "price": "Prix" + "price": "Prix", + "new": "Nouveau" }, "firstName": "Prénom", "lastName": "Nom de famille", @@ -59,5 +59,6 @@ "sandwiches": "Sandwichs", "date": "Date", "order": "Commande", - "save": "Enregistrer" + "save": "Enregistrer", + "sandwich": "Sandwich" } \ No newline at end of file diff --git a/public/stylesheets/style.css b/public/stylesheets/style.css index 9cef70e..b4e7aa3 100644 --- a/public/stylesheets/style.css +++ b/public/stylesheets/style.css @@ -217,6 +217,10 @@ p.before-link a::before { cursor: pointer; } +#sandwichesManagement>a>button { + width: 100%; +} + @media (hover: none) and (pointer: coarse) { body { font-size: xx-large; diff --git a/routes/admin/sandwiches/add.js b/routes/admin/sandwiches/add.js new file mode 100644 index 0000000..3ad2800 --- /dev/null +++ b/routes/admin/sandwiches/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/sandwiches/add", { + title: "SOD - Sandwiches administration", + user: req.session.user + }); +}).post("/", sessionCheck(3), async (req, res) => { + if (!req.body.name || !req.body.price) + return error(req, res, "Fail to add sandwich !", 400, "Missing arg"); + + if (await models.Sandwich.findByPk(req.body.name)) + return error(req, res, "Fail to add sandwich", 400, "Name already used"); + + await models.Sandwich.create({name: req.body.name, price: req.body.price}); + res.redirect("/admin/sandwiches"); +}); + +module.exports = router; diff --git a/routes/admin/sandwiches/index.js b/routes/admin/sandwiches/index.js index 3ead76a..2f1f323 100644 --- a/routes/admin/sandwiches/index.js +++ b/routes/admin/sandwiches/index.js @@ -13,6 +13,7 @@ router.get("/", sessionCheck(3), async (req, res) => { }); }) .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 sandwich !", 400, "Missing arg"); diff --git a/views/admin/sandwiches/add.pug b/views/admin/sandwiches/add.pug new file mode 100644 index 0000000..226f746 --- /dev/null +++ b/views/admin/sandwiches/add.pug @@ -0,0 +1,14 @@ +extends ../../layout + +block content + div.card#sandwichesManagement + h1=__("admin.new")+" "+__("sandwich") + form(action="/admin/sandwiches/add" method="POST") + div.field + label(for="name")=__("admin.name")+":" + input#name(type="text" name="name" required) + div.field + label(for="price")=__("admin.price") + ":" + input#price(type="number" min="0" step="any" name="price" value="0" required) + div.field + input(type="submit" value=__("admin.add")) diff --git a/views/admin/sandwiches/index.pug b/views/admin/sandwiches/index.pug index 843b25f..abeab8d 100644 --- a/views/admin/sandwiches/index.pug +++ b/views/admin/sandwiches/index.pug @@ -3,7 +3,8 @@ extends ../../layout block content div.card#sandwichesManagement h1=__("admin.sandwichManagement") - input.add(type="button" value=__("admin.add")) + a.add(href="/admin/sandwiches/add") + button=__("admin.add") div each sandwich in sandwiches div.sandwich diff --git a/views/index.pug b/views/index.pug index dbc99ef..ef2aac7 100644 --- a/views/index.pug +++ b/views/index.pug @@ -22,7 +22,7 @@ block content div#order1.order h2=__("order")+" 1" div.field - label(for="sandwich1")=__("index.sandwich")+":" + label(for="sandwich1")=__("sandwich")+":" input#sandwich1(type="list" list="sandwich-list" name="sandwich1" autocomplete="off" required) div.field label(for="day1")=__("index.day")+":"