diff --git a/models/command.js b/models/command.js index 1704123..924a5fa 100644 --- a/models/command.js +++ b/models/command.js @@ -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, diff --git a/models/department.js b/models/department.js index 6483b79..4a29790 100644 --- a/models/department.js +++ b/models/department.js @@ -11,8 +11,7 @@ module.exports = (sequelize, DataTypes) => { Department.init({ name: { type: DataTypes.STRING, - unique: true, - allowNull: false + primaryKey: true } }, { sequelize, diff --git a/models/sandwich.js b/models/sandwich.js index 57d5a7d..8d1f1a7 100644 --- a/models/sandwich.js +++ b/models/sandwich.js @@ -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, diff --git a/models/sandwichCommand.js b/models/sandwichCommand.js new file mode 100644 index 0000000..3e5ac9a --- /dev/null +++ b/models/sandwichCommand.js @@ -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; +}; diff --git a/public/javascripts/index.js b/public/javascripts/index.js index 197670e..d79dd83 100644 --- a/public/javascripts/index.js +++ b/public/javascripts/index.js @@ -16,11 +16,11 @@ document.getElementById("add-command").addEventListener("click", () => {

Command ${id}

- +
- +
`); document.getElementById("command"+lastCommandId()).scrollIntoView({behavior: "smooth"}); diff --git a/public/stylesheets/style.css b/public/stylesheets/style.css index 7e02997..3a2305c 100644 --- a/public/stylesheets/style.css +++ b/public/stylesheets/style.css @@ -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; diff --git a/routes/command.js b/routes/command.js index ae7b5dd..5a8aff0 100644 --- a/routes/command.js +++ b/routes/command.js @@ -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; diff --git a/views/index.pug b/views/index.pug index f215b47..9c8aa07 100644 --- a/views/index.pug +++ b/views/index.pug @@ -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