Archived
1
0
Fork 0

Add orders to routes

This commit is contained in:
Ethanell 2020-08-16 12:48:43 +02:00
parent f650e77c03
commit fe7adb5457
8 changed files with 99 additions and 7 deletions

4
app.js
View file

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

View file

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

View file

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

View file

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

View file

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

28
routes/orders.js Normal file
View file

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

View file

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

22
views/orders.pug Normal file
View file

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