const { splitSite } = require("../middlewares"); const db = require("../models"); const Lunch = db.lunch; // Create a new Lunch Item exports.createNewLunch = (req, res) => { // Validate request if (!req.body.customer_id) { res.status(400).send({ message: "Content can not be empty!" }); return; } const site = splitSite.findSiteNumber(req); // Create an Lunch Item const lunch = new Lunch({ customer_id: req.body.customer_id, customer_name: req.body.customer_name, has_lunch: req.body.has_lunch, create_by: req.body.create_by, create_date: req.body.create_date, edit_history: req.body.edit_history, date: req.body.date, site }); // Save lunch Item in the database lunch .save(lunch) .then(data => { res.send(data); }) .catch(err => { res.status(500).send({ message: err.message || "Some error occurred while creating the Lunch Record." }); }); }; // Retrive all Lunch Records from database. exports.getAllLunches = (req, res) => { var params = req.query; var condition = {}; if (params.date) { condition.date = params.date; } condition = splitSite.splitSiteGet(req, condition); Lunch.find(condition) .then(data => { res.send(data); }) .catch(err => { res.status(500).send({ message: err.message || "Some error occurred while retrieving Lunches." }); }); }; // Get One Event by Id // exports.getEvent = (req, res) => { // const id = req.params.id; // CalendarEvent.findById(id) // .then(data => { // if (!data) // res.status(404).send({ message: "Not found Event with id " + id }); // else res.send(data); // }) // .catch(err => { // res // .status(500) // .send({ message: "Error retrieving Event with id=" + id }); // }); // }; // Update a Lunch by the id in the request exports.updateLunch = (req, res) => { if (!req.body) { return res.status(400).send({ message: "Data to update can not be empty!" }); } const id = req.params.id; Lunch.findByIdAndUpdate(id, req.body, { useFindAndModify: false }) .then(data => { if (!data) { res.status(404).send({ message: `Cannot update Lunch with id=${id}. Maybe Lunch was not found!` }); } else res.send({ success: true, message: "Lunch was updated successfully." }); }) .catch(err => { res.status(500).send({ success: false, message: "Error updating Lunch with id=" + id }); }); }; // Delete a Lunch by id exports.deleteLunch= (req, res) => { const id = req.params.id; Lunch.findByIdAndRemove(id) .then(data => { if (!data) { res.status(404).send({ message: `Cannot delete Lunch with id=${id}. Maybe Lunch was not found!` }); } else { res.send({ message: "Lunch was deleted successfully!" }); } }) .catch(err => { res.status(500).send({ message: "Could not delete Lunch with id=" + id }); }); };