Applicatie afmaken en GUI-flow herstellen
Voegt boodschappenlijsten, productbeheer, realtime synchronisatie en browsercache-updates toe. Herstelt login- en sessiegedrag en maakt database-seeding herhaalbaar.\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
const ProductRepository = require("../repositories/ProductRepository");
|
||||
const ShoppingListRepository = require("../repositories/ShoppingListRepository");
|
||||
|
||||
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.name || "").trim();
|
||||
if (!name) {
|
||||
req.flash("error", "Geef een lijstnaam op");
|
||||
return res.redirect("/lists");
|
||||
}
|
||||
ShoppingListRepository.create(req.user.household_id, name);
|
||||
req.flash("success", "Boodschappenlijst aangemaakt");
|
||||
res.redirect("/lists");
|
||||
}
|
||||
|
||||
show(req, res) {
|
||||
const list = ShoppingListRepository.findById(req.params.id, req.user.household_id);
|
||||
if (!list) return res.status(404).render("errors/404");
|
||||
res.locals.title = list.name;
|
||||
res.render("lists/show", {
|
||||
list,
|
||||
items: ShoppingListRepository.getItems(list.id),
|
||||
products: ProductRepository.getAll(),
|
||||
user: req.user
|
||||
});
|
||||
}
|
||||
|
||||
addItem(req, res) {
|
||||
const list = ShoppingListRepository.findById(req.params.id, req.user.household_id);
|
||||
const productId = Number.parseInt(req.body.product_id, 10);
|
||||
const amount = Number.parseFloat(req.body.amount || "1");
|
||||
const product = Number.isInteger(productId) ? ProductRepository.findById(productId) : null;
|
||||
if (!list || !product || !Number.isFinite(amount) || amount <= 0) {
|
||||
req.flash("error", "Ongeldig product of ongeldige hoeveelheid");
|
||||
return res.redirect(`/lists/${req.params.id}`);
|
||||
}
|
||||
ShoppingListRepository.addItem(list.id, product.id, amount);
|
||||
res.redirect(`/lists/${list.id}`);
|
||||
}
|
||||
|
||||
toggleItem(req, res) {
|
||||
const list = ShoppingListRepository.findById(req.params.id, req.user.household_id);
|
||||
if (!list) return res.status(404).render("errors/404");
|
||||
ShoppingListRepository.toggleItem(list.id, req.params.itemId);
|
||||
this.broadcastUpdate(req, list.id);
|
||||
res.redirect(`/lists/${list.id}`);
|
||||
}
|
||||
|
||||
deleteItem(req, res) {
|
||||
const list = ShoppingListRepository.findById(req.params.id, req.user.household_id);
|
||||
if (!list) return res.status(404).json({ error: "Lijst niet gevonden" });
|
||||
const result = ShoppingListRepository.deleteItem(list.id, req.params.itemId);
|
||||
if (result.changes === 0) return res.status(404).json({ error: "Product niet gevonden" });
|
||||
this.broadcastUpdate(req, list.id);
|
||||
res.json({ success: true });
|
||||
}
|
||||
|
||||
broadcastUpdate(req, listId) {
|
||||
if (req.app.locals.io) {
|
||||
req.app.locals.io.to(`list:${listId}`).emit("list:updated", { listId });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new ShoppingListController();
|
||||
Reference in New Issue
Block a user