Archived
1
0
Fork 0

Make command route working with database to process orders

This commit is contained in:
Ethanell 2020-08-15 20:17:36 +02:00
parent 9c0c14a34b
commit f650e77c03
8 changed files with 95 additions and 21 deletions

View file

@ -5,7 +5,7 @@ const {
module.exports = (sequelize, DataTypes) => {
class Command extends Model {
static associate(models) {
Command.belongsToMany(models.Sandwich, {through: "SandwichCommand"});
Command.belongsToMany(models.Sandwich, {through: {model: models.SandwichCommand, unique: false}});
Command.belongsTo(models.Department);
}
}
@ -19,9 +19,13 @@ module.exports = (sequelize, DataTypes) => {
allowNull: false
},
date: {
type: DataTypes.DATEONLY,
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
allowNull: false
},
price: {
type: DataTypes.FLOAT,
allowNull: false
}
}, {
sequelize,

View file

@ -11,8 +11,7 @@ module.exports = (sequelize, DataTypes) => {
Department.init({
name: {
type: DataTypes.STRING,
unique: true,
allowNull: false
primaryKey: true
}
}, {
sequelize,

View file

@ -5,14 +5,13 @@ const {
module.exports = (sequelize, DataTypes) => {
class Sandwich extends Model {
static associate(models) {
Sandwich.belongsToMany(models.Command, {through: "SandwichCommand"});
Sandwich.belongsToMany(models.Command, {through: {model: models.SandwichCommand, unique: false}});
}
}
Sandwich.init({
name: {
type: DataTypes.STRING,
allowNull: false,
unique: true
primaryKey: true
},
price: {
type: DataTypes.FLOAT,

26
models/sandwichCommand.js Normal file
View file

@ -0,0 +1,26 @@
"use strict";
const {
Model
} = require("sequelize");
module.exports = (sequelize, DataTypes) => {
class SandwichCommand extends Model {
static associate(models) {
}
}
SandwichCommand.init({
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
date: {
type: DataTypes.DATEONLY,
defaultValue: DataTypes.NOW,
allowNull: false
}
}, {
sequelize,
modelName: "SandwichCommand",
});
return SandwichCommand;
};

View file

@ -16,11 +16,11 @@ document.getElementById("add-command").addEventListener("click", () => {
<h2>Command ${id}</h2>
<div class="field">
<label for="sandwich">Sandwich:</label>
<input id="sandwich" type="list" list="sandwich-list" name="sandwich${id}" required>
<input id="sandwich" type="list" list="sandwich-list" name="sandwich${id}" autocomplete="off" required>
</div>
<div class="field">
<label for="day">Day:</label>
<input id="day" type="list" list="date-list" name="date${id}" required>
<input id="day" type="date" name="date${id}" required>
</div>
</div>`);
document.getElementById("command"+lastCommandId()).scrollIntoView({behavior: "smooth"});

View file

@ -62,7 +62,7 @@ a {
font-size: 100%;
}
.field input[type="text"], .field input[type="list"] {
.field input[type="text"], .field input[type="list"], .field input[type="date"] {
height: 2.5vh;
border-top: none;
border-left: none;

View file

@ -1,9 +1,57 @@
let express = require("express");
let router = express.Router();
let models = require("../models");
/* GET home page. */
router.post("/", (req, res) => {
console.log(req.body);
router.post("/", async (req, res) => {
if (!req.body.department || !req.body.firstName || !req.body.lastName || !req.body.sandwich1 || !req.body.date1) {
res.render("error", {message: "Invalid command !", "error": {status: "Missing arguments"}});
return;
}
let department = await models.Department.findByPk(req.body.department);
if (!department) {
res.render("error", {message: "Invalid command !", error: {status: "Invalid department"}});
return;
}
let sandwichs = [];
let price = 0;
for (let i = 1; req.body["sandwich" + i] !== undefined; i++) {
if (req.body["date" + i] === undefined) {
res.render("error", {message: "Invalid command !", error: {status: "Sandwich without date"}});
return;
}
let sandwich = await models.Sandwich.findByPk(req.body["sandwich" + i]);
if (!sandwich) {
res.render("error", {
message: "Invalid command !",
error: {status: "Invalid sandwich: "+req.body["sandwich" + i]}
});
return;
}
sandwichs.push([sandwich.name, req.body["date" + i]]);
price += sandwich.price;
}
let command = await models.Command.create({
firstName: req.body.firstName,
lastName: req.body.lastName,
price: price
});
await command.setDepartment(department);
for (let data of sandwichs)
try {
console.log(command.id);
console.log(data);
await models.SandwichCommand.create({CommandId: command.id, SandwichName: data[0], date: data[1]});
} catch (e) {
await command.destroy();
res.render("error", {message: "Invalid command !", error: {status: "Invalid date"}});
throw e;
}
res.send("Ok");
});
module.exports = router;

View file

@ -6,27 +6,27 @@ block content
p Welcome to Sandwiches Order Doua
form#command(action="/command" method="POST")
div.field
label(for="sandwich") Department:
input#department(type="list" list="department-list" name="department" required)
label(for="department") Department:
input#department(type="list" list="department-list" name="department" autocomplete="off" required)
datalist#department-list
each department in departments
option(value=department.name)
div.field
label(for="firstname") First name:
input#firstname(type="text" name="firstname" required)
input#firstname(type="text" name="firstName" required)
div.field
label(for="lastname") Last name:
input#lastname(type="text" name="lastname" required)
input#lastname(type="text" name="lastName" required)
div#command1.command
h2 Command 1
div.field
label(for="sandwich") Sandwich:
input#sandwich(type="list" list="sandwich-list" name="sandwich1" required)
input#sandwich(type="list" list="sandwich-list" name="sandwich1" autocomplete="off" required)
div.field
label(for="day") Day:
input#day(type="list" list="date-list" name="date1" required)
input#day(type="date" name="date1" required)
div#command-action
a#add-command +
@ -38,8 +38,6 @@ block content
datalist#sandwich-list
each sandwich in sandwichs
option(value=sandwich.name)
datalist#date-list
option(value="14/08/2020")
div#more
a About