swipe, zoeken op producten, beter toevoegen in lijsten
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
const FavoriteService = require("../services/FavoriteService");
|
||||
const CategoryService = require("../services/CategoryService");
|
||||
|
||||
class FavoriteController {
|
||||
index(req, res) {
|
||||
@@ -7,6 +8,7 @@ class FavoriteController {
|
||||
res.locals.title = "Favorieten";
|
||||
res.render("favorites/index", {
|
||||
favorites,
|
||||
categories: CategoryService.getCategories(req.user.household_id),
|
||||
user: req.user
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
const ProductRepository = require("../repositories/ProductRepository");
|
||||
const ShoppingListRepository = require("../repositories/ShoppingListRepository");
|
||||
const FavoriteService = require("../services/FavoriteService");
|
||||
const CategoryService = require("../services/CategoryService");
|
||||
|
||||
class ShoppingListController {
|
||||
index(req, res) {
|
||||
@@ -23,11 +24,15 @@ class ShoppingListController {
|
||||
show(req, res) {
|
||||
const list = ShoppingListRepository.findById(req.params.id, req.user.household_id);
|
||||
if (!list) return res.status(404).render("errors/404");
|
||||
const items = ShoppingListRepository.getItems(list.id, req.user.household_id);
|
||||
const existingProductIds = new Set(items.map(item => item.product_id));
|
||||
res.locals.title = list.name;
|
||||
res.render("lists/show", {
|
||||
list,
|
||||
items: ShoppingListRepository.getItems(list.id, req.user.household_id),
|
||||
products: ProductRepository.getAll(req.user.household_id),
|
||||
items,
|
||||
products: ProductRepository.getAll(req.user.household_id)
|
||||
.filter(product => !existingProductIds.has(product.id)),
|
||||
categories: CategoryService.getCategories(req.user.household_id),
|
||||
user: req.user
|
||||
});
|
||||
}
|
||||
@@ -39,7 +44,8 @@ class ShoppingListController {
|
||||
const product = Number.isInteger(productId)
|
||||
? ProductRepository.findById(productId, req.user.household_id)
|
||||
: null;
|
||||
if (!list || !product || !Number.isFinite(amount) || amount <= 0) {
|
||||
if (!list || !product || ShoppingListRepository.hasProduct(list.id, product.id) ||
|
||||
!Number.isFinite(amount) || amount <= 0) {
|
||||
req.flash("error", "Ongeldig product of ongeldige hoeveelheid");
|
||||
return res.redirect(`/lists/${req.params.id}`);
|
||||
}
|
||||
@@ -47,6 +53,25 @@ class ShoppingListController {
|
||||
res.redirect(`/lists/${list.id}`);
|
||||
}
|
||||
|
||||
updateItemAmount(req, res) {
|
||||
const list = ShoppingListRepository.findById(req.params.id, req.user.household_id);
|
||||
const amount = Number.parseFloat(req.body.amount || "");
|
||||
if (!list || !Number.isFinite(amount) || amount <= 0) {
|
||||
req.flash("error", "Geef een geldige hoeveelheid op.");
|
||||
return res.redirect(`/lists/${req.params.id}`);
|
||||
}
|
||||
|
||||
const result = ShoppingListRepository.updateItemAmount(list.id, req.params.itemId, amount);
|
||||
if (result.changes === 0) {
|
||||
req.flash("error", "Product niet gevonden op deze lijst.");
|
||||
return res.redirect(`/lists/${req.params.id}`);
|
||||
}
|
||||
|
||||
this.broadcastUpdate(req, list.id);
|
||||
req.flash("success", "Hoeveelheid gewijzigd.");
|
||||
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");
|
||||
|
||||
Reference in New Issue
Block a user