Legt de bestaande wijzigingen in de hoofdworktree vast voordat de laatste worktree wordt gemerged.\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
180 lines
4.7 KiB
JavaScript
180 lines
4.7 KiB
JavaScript
const ShoppingListRepository = require("../repositories/ShoppingListRepository");
|
|
const ProductRepository = require("../repositories/ProductRepository");
|
|
|
|
class ShoppingListController {
|
|
|
|
index(req, res) {
|
|
|
|
const lists = ShoppingListRepository.getByHousehold(
|
|
req.user.household_id
|
|
);
|
|
|
|
res.locals.title = "Boodschappenlijsten";
|
|
|
|
res.render("lists/index", {
|
|
lists,
|
|
user: req.user
|
|
});
|
|
|
|
}
|
|
|
|
create(req, res) {
|
|
|
|
const { name } = req.body;
|
|
|
|
if (!name || name.trim().length === 0) {
|
|
req.flash("error", "Geef een naam op");
|
|
return res.redirect("/lists");
|
|
}
|
|
|
|
// Check if user has a household
|
|
if (!req.user || !req.user.household_id) {
|
|
req.flash("error", "Je bent niet aan een huishouden gekoppeld");
|
|
return res.redirect("/lists");
|
|
}
|
|
|
|
ShoppingListRepository.create({
|
|
household_id: req.user.household_id,
|
|
name: name.trim()
|
|
});
|
|
|
|
req.flash("success", "Boodschappenlijst aangemaakt");
|
|
res.redirect("/lists");
|
|
|
|
}
|
|
|
|
show(req, res) {
|
|
|
|
const { id } = req.params;
|
|
|
|
const list = ShoppingListRepository.getById(id);
|
|
|
|
if (!list) {
|
|
return res.status(404).render("errors/404");
|
|
}
|
|
|
|
if (list.household_id !== req.user.household_id) {
|
|
return res.status(403).render("errors/404");
|
|
}
|
|
|
|
const items = ShoppingListRepository.getItems(id);
|
|
|
|
res.locals.title = list.name;
|
|
|
|
res.render("lists/show", {
|
|
list,
|
|
items,
|
|
user: req.user
|
|
});
|
|
|
|
}
|
|
|
|
addItem(req, res) {
|
|
|
|
const { id } = req.params;
|
|
const { product_id, amount } = req.body;
|
|
|
|
const list = ShoppingListRepository.getById(id);
|
|
|
|
if (!list) {
|
|
return res.status(404).json({ error: "Lijst niet gevonden" });
|
|
}
|
|
|
|
if (list.household_id !== req.user.household_id) {
|
|
return res.status(403).json({ error: "Niet gemachtigd" });
|
|
}
|
|
|
|
const product = ProductRepository.findById(product_id);
|
|
|
|
if (!product) {
|
|
return res.status(404).json({ error: "Product niet gevonden" });
|
|
}
|
|
|
|
const quantity = Number(amount);
|
|
if (!Number.isFinite(quantity) || quantity <= 0 || quantity > 999) {
|
|
req.flash("error", "Vul een geldige hoeveelheid in");
|
|
return res.redirect(`/lists/${id}`);
|
|
}
|
|
|
|
const item = ShoppingListRepository.addItem({
|
|
list_id: id,
|
|
product_id,
|
|
amount: quantity
|
|
});
|
|
|
|
req.app.locals.io.to(`household:${req.user.household_id}`).emit("item:added", {
|
|
listId: Number(id)
|
|
});
|
|
|
|
req.flash("success", "Product toegevoegd");
|
|
|
|
if (req.xhr || req.headers.accept?.includes("application/json")) {
|
|
return res.json({ success: true, item });
|
|
}
|
|
|
|
res.redirect(`/lists/${id}`);
|
|
|
|
}
|
|
|
|
toggleItem(req, res) {
|
|
|
|
const { id, itemId } = req.params;
|
|
|
|
const list = ShoppingListRepository.getById(id);
|
|
|
|
if (!list || list.household_id !== req.user.household_id) {
|
|
return res.status(403).json({ error: "Niet gemachtigd" });
|
|
}
|
|
|
|
if (!ShoppingListRepository.findItem(id, itemId)) {
|
|
return res.status(404).json({ error: "Item niet gevonden" });
|
|
}
|
|
|
|
ShoppingListRepository.toggleItem(itemId);
|
|
|
|
req.app.locals.io.to(`household:${req.user.household_id}`).emit("item:toggled", {
|
|
listId: Number(id), itemId: Number(itemId)
|
|
});
|
|
|
|
if (req.xhr || req.headers.accept?.includes("application/json")) {
|
|
return res.json({ success: true });
|
|
}
|
|
|
|
res.redirect(`/lists/${id}`);
|
|
|
|
}
|
|
|
|
deleteItem(req, res) {
|
|
|
|
const { id, itemId } = req.params;
|
|
|
|
const list = ShoppingListRepository.getById(id);
|
|
|
|
if (!list || list.household_id !== req.user.household_id) {
|
|
return res.status(403).json({ error: "Niet gemachtigd" });
|
|
}
|
|
|
|
if (!ShoppingListRepository.findItem(id, itemId)) {
|
|
return res.status(404).json({ error: "Item niet gevonden" });
|
|
}
|
|
|
|
ShoppingListRepository.deleteItem(itemId);
|
|
|
|
req.app.locals.io.to(`household:${req.user.household_id}`).emit("item:removed", {
|
|
listId: Number(id), itemId: Number(itemId)
|
|
});
|
|
|
|
req.flash("success", "Product verwijderd");
|
|
|
|
if (req.method === "DELETE" || req.xhr || req.headers.accept?.includes("application/json")) {
|
|
return res.json({ success: true });
|
|
}
|
|
|
|
res.redirect(`/lists/${id}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = new ShoppingListController();
|