Archived
1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
SOD/routes/admin/sandwiches/edit.js

40 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-08-20 11:48:28 +02:00
let express = require("express");
let router = express.Router();
let sessionCheck = require("../../../middlewares/sessionCheck");
let models = require("../../../models");
2020-08-20 12:14:38 +02:00
let error = require("../../utils/error");
2020-08-20 11:48:28 +02:00
router.get("/", sessionCheck(3), async (req, res) => {
if (!req.query.name)
2020-08-20 12:14:38 +02:00
return error(req, res, "Can't edit sandwich !", 400, "Missing arg");
let sandwich = await models.Sandwich.findByPk(req.query.name);
if (!sandwich)
return error(req, res, "Can't edit sandwich !", 400, "Sandwich not found");
res.render("admin/sandwiches/edit", {
title: "SOD - Sandwiches administration",
sandwich: sandwich
});
2020-08-20 11:48:28 +02:00
}).post("/", sessionCheck(3), async (req, res) => {
if (!req.body.name)
2020-08-20 12:14:38 +02:00
return error(req, res, "Fail to edit sandwich !", 400, "Missing arg");
let sandwich = await models.Sandwich.findByPk(req.body.name);
if (!sandwich)
return error(req, res, "Fail to edit sandwich !", 400, "Invalid sandwich name");
if (req.body.price && req.body.price !== sandwich.price)
sandwich.price = req.body.price;
if(req.body.enable !== sandwich.enable)
sandwich.enable = !!req.body.enable;
2020-08-20 12:14:38 +02:00
await sandwich.save();
2020-08-20 12:14:38 +02:00
if (req.body.newName && req.body.newName !== sandwich.name)
await models.Sandwich.update({name: req.body.newName}, {where: {name: req.body.name}});
2020-08-20 12:14:38 +02:00
res.redirect("/admin/sandwiches");
2020-08-20 11:48:28 +02:00
});
module.exports = router;