From fe7adb54571dd08057b938e3b51bc3e367065272 Mon Sep 17 00:00:00 2001 From: flifloo Date: Sun, 16 Aug 2020 12:48:43 +0200 Subject: [PATCH] Add orders to routes --- app.js | 4 +++- public/javascripts/orders.js | 25 +++++++++++++++++++++++++ public/stylesheets/style.css | 15 +++++++++++++++ routes/command.js | 6 +++--- routes/index.js | 4 ++-- routes/orders.js | 28 ++++++++++++++++++++++++++++ views/index.pug | 2 +- views/orders.pug | 22 ++++++++++++++++++++++ 8 files changed, 99 insertions(+), 7 deletions(-) create mode 100644 public/javascripts/orders.js create mode 100644 routes/orders.js create mode 100644 views/orders.pug diff --git a/app.js b/app.js index 04f9feb..4e7c41d 100644 --- a/app.js +++ b/app.js @@ -4,7 +4,8 @@ let cookieParser = require("cookie-parser"); let logger = require("morgan"); let indexRouter = require("./routes/index"); -let commandRouter = require("./routes/command") +let commandRouter = require("./routes/command"); +let ordersRouter = require("./routes/orders"); let app = express(); @@ -20,6 +21,7 @@ app.use(express.static(path.join(__dirname, "public"))); app.use("/", indexRouter); app.use("/command", commandRouter); +app.use("/orders", ordersRouter); // catch 404 and forward to error handler app.use((req, res) => { diff --git a/public/javascripts/orders.js b/public/javascripts/orders.js new file mode 100644 index 0000000..7ef5581 --- /dev/null +++ b/public/javascripts/orders.js @@ -0,0 +1,25 @@ +const date = document.getElementById("date"); + + +function collapse(e, subDiv) { + e.addEventListener("click", ev => { + ev.stopPropagation(); + let action; + if (e.classList.contains("collapse")) + action = "remove"; + else + action = "add"; + + e.querySelectorAll("."+subDiv).forEach(e => e.classList[action]("hide")); + e.classList[action]("collapse"); + }) +} + + +document.querySelectorAll(".department").forEach(e => collapse(e, "user")); + +document.querySelectorAll(".user").forEach(e => collapse(e, "order")); + +document.querySelectorAll(".order").forEach(e => collapse(e, "sandwich")) + +date.addEventListener("change", () => window.location.href = "?date="+date.value); diff --git a/public/stylesheets/style.css b/public/stylesheets/style.css index 3a2305c..46ac2e0 100644 --- a/public/stylesheets/style.css +++ b/public/stylesheets/style.css @@ -144,3 +144,18 @@ p.before-link a::before { height: 100%; background-color: rgba(52, 55, 55, .4); } + +#date-selector { + position: fixed; + top: 0; + left: 0; + border-radius: 0 0 0.5em 0.5em; +} + +.department { + box-shadow: 0.3em 0.3em 0.3em rgba(0, 0, 255, .2); +} + +.sandwich { + margin-left: 1em; +} diff --git a/routes/command.js b/routes/command.js index 5a8aff0..f62df3e 100644 --- a/routes/command.js +++ b/routes/command.js @@ -15,7 +15,7 @@ router.post("/", async (req, res) => { return; } - let sandwichs = []; + let sandwiches = []; let price = 0; for (let i = 1; req.body["sandwich" + i] !== undefined; i++) { if (req.body["date" + i] === undefined) { @@ -31,7 +31,7 @@ router.post("/", async (req, res) => { }); return; } - sandwichs.push([sandwich.name, req.body["date" + i]]); + sandwiches.push([sandwich.name, req.body["date" + i]]); price += sandwich.price; } @@ -41,7 +41,7 @@ router.post("/", async (req, res) => { price: price }); await command.setDepartment(department); - for (let data of sandwichs) + for (let data of sandwiches) try { console.log(command.id); console.log(data); diff --git a/routes/index.js b/routes/index.js index 06782a0..5ae3fda 100644 --- a/routes/index.js +++ b/routes/index.js @@ -4,8 +4,8 @@ let models = require("../models"); router.get("/", async (req, res) => { let departments = await models.Department.findAll(); - let sandwichs = await models.Sandwich.findAll(); - res.render("index", { title: "SOD", departments: departments, sandwichs: sandwichs }); + let sandwiches = await models.Sandwich.findAll(); + res.render("index", { title: "SOD", departments: departments, sandwiches: sandwiches }); }); module.exports = router; diff --git a/routes/orders.js b/routes/orders.js new file mode 100644 index 0000000..58c21ef --- /dev/null +++ b/routes/orders.js @@ -0,0 +1,28 @@ +let express = require("express"); +let router = express.Router(); +let models = require("../models"); + + +router.get("/", async (req, res) => { + let date = req.query.date ? req.query.date : (new Date()).toISOString().substring(0,10); + + let commands = {}; + for (let i of await models.SandwichCommand.findAll({where: {date: date}})) { + i.Command = await models.Command.findByPk(i.CommandId); + i.Sandwich = await models.Sandwich.findByPk(i.SandwichName); + let name = i.Command.firstName + " " + i.Command.lastName; + + if (!(i.Command.DepartmentName in commands)) + commands[i.Command.DepartmentName] = {}; + + if (!(name in commands[i.Command.DepartmentName])) + commands[i.Command.DepartmentName][name] = {}; + + if (!(i.Command.id in commands[i.Command.DepartmentName][name])) + commands[i.Command.DepartmentName][name][i.Command.id] = [] + commands[i.Command.DepartmentName][name][i.Command.id].push(i); + } + res.render("orders", {title: "SOD", commands: commands, date: date}); +}); + +module.exports = router; diff --git a/views/index.pug b/views/index.pug index 9c8aa07..47dacc4 100644 --- a/views/index.pug +++ b/views/index.pug @@ -36,7 +36,7 @@ block content input#send(type="submit" value="Pay") datalist#sandwich-list - each sandwich in sandwichs + each sandwich in sandwiches option(value=sandwich.name) div#more diff --git a/views/orders.pug b/views/orders.pug new file mode 100644 index 0000000..facee85 --- /dev/null +++ b/views/orders.pug @@ -0,0 +1,22 @@ +extends layout + +block content + div#date-selector.card + label(for="date") Date + input#date(type="date" value=date) + div#orders.card + h1 Orders + each user, department in commands + div.department + h2= department + each command, name in user + div.user + h3= name + each order, id in command + div.order + h4 Command N°#{id} + each sandwich in order + div.sandwich + h4= sandwich.Sandwich.name + + script(src="javascripts/orders.js")