Favorieten toegevoegd aan de navigatiebalk en sidebar.
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
const FavoriteService = require("../services/FavoriteService");
|
||||
|
||||
class FavoriteController {
|
||||
index(req, res) {
|
||||
const favorites = FavoriteService.getFavorites(req.user.id, req.user.household_id);
|
||||
|
||||
res.locals.title = "Favorieten";
|
||||
res.render("favorites/index", {
|
||||
favorites,
|
||||
user: req.user
|
||||
});
|
||||
}
|
||||
|
||||
toggle(req, res) {
|
||||
const productId = Number.parseInt(req.params.id, 10);
|
||||
|
||||
if (!Number.isInteger(productId)) {
|
||||
req.flash("error", "Product niet gevonden.");
|
||||
return res.redirect("/products");
|
||||
}
|
||||
|
||||
const isFavorite = FavoriteService.toggleFavorite(req.user.id, productId, req.user.household_id);
|
||||
if (!isFavorite && !FavoriteService.getFavoriteIds(req.user.id, req.user.household_id).has(productId)) {
|
||||
req.flash("error", "Product niet gevonden.");
|
||||
return res.redirect("/products");
|
||||
}
|
||||
|
||||
req.flash("success", isFavorite ? "Product toegevoegd aan favorieten." : "Product verwijderd uit favorieten.");
|
||||
res.redirect("/products");
|
||||
}
|
||||
|
||||
remove(req, res) {
|
||||
const productId = Number.parseInt(req.params.productId, 10);
|
||||
|
||||
if (!Number.isInteger(productId)) {
|
||||
req.flash("error", "Product niet gevonden.");
|
||||
return res.redirect("/favorites");
|
||||
}
|
||||
|
||||
const removed = FavoriteService.removeFavorite(req.user.id, productId, req.user.household_id);
|
||||
if (!removed) {
|
||||
req.flash("error", "Product niet gevonden in favorieten.");
|
||||
return res.redirect("/favorites");
|
||||
}
|
||||
|
||||
req.flash("success", "Product verwijderd uit favorieten.");
|
||||
res.redirect("/favorites");
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new FavoriteController();
|
||||
@@ -2,6 +2,7 @@ const ProductService =
|
||||
require("../services/ProductService");
|
||||
|
||||
const CategoryService = require("../services/CategoryService");
|
||||
const FavoriteService = require("../services/FavoriteService");
|
||||
|
||||
const parseCsv = require("../utils/parseCsv");
|
||||
|
||||
@@ -10,16 +11,18 @@ class ProductController {
|
||||
|
||||
index(req, res) {
|
||||
|
||||
const products =
|
||||
ProductService.getProducts(req.user.household_id);
|
||||
|
||||
const products = ProductService.getProducts(req.user.household_id);
|
||||
const favoriteIds = FavoriteService.getFavoriteIds(req.user.id, req.user.household_id);
|
||||
const categories = CategoryService.getCategories(req.user.household_id);
|
||||
|
||||
res.render(
|
||||
"products/index",
|
||||
{
|
||||
title:"Producten",
|
||||
products,
|
||||
products: products.map(product => ({
|
||||
...product,
|
||||
is_favorite: favoriteIds.has(product.id)
|
||||
})),
|
||||
categories
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const ProductRepository = require("../repositories/ProductRepository");
|
||||
const ShoppingListRepository = require("../repositories/ShoppingListRepository");
|
||||
const FavoriteService = require("../services/FavoriteService");
|
||||
|
||||
class ShoppingListController {
|
||||
index(req, res) {
|
||||
@@ -63,6 +64,40 @@ class ShoppingListController {
|
||||
res.redirect(`/lists/${list.id}`);
|
||||
}
|
||||
|
||||
clearItems(req, res) {
|
||||
const list = ShoppingListRepository.findById(req.params.id, req.user.household_id);
|
||||
if (!list) return res.status(404).render("errors/404");
|
||||
|
||||
ShoppingListRepository.clearItems(list.id);
|
||||
this.broadcastUpdate(req, list.id);
|
||||
req.flash("success", "Alle producten zijn verwijderd uit de lijst.");
|
||||
res.redirect(`/lists/${list.id}`);
|
||||
}
|
||||
|
||||
addFavoriteProducts(req, res) {
|
||||
const list = ShoppingListRepository.findById(req.params.id, req.user.household_id);
|
||||
if (!list) return res.status(404).render("errors/404");
|
||||
|
||||
const favorites = FavoriteService.getFavorites(req.user.id, req.user.household_id);
|
||||
const items = favorites.map((favorite) => ({
|
||||
list_id: list.id,
|
||||
product_id: favorite.id,
|
||||
amount: 1,
|
||||
checked: 0,
|
||||
sort_order: 0
|
||||
}));
|
||||
|
||||
if (items.length === 0) {
|
||||
req.flash("info", "Je hebt nog geen favorieten om toe te voegen.");
|
||||
return res.redirect(`/lists/${list.id}`);
|
||||
}
|
||||
|
||||
ShoppingListRepository.addItems(list.id, items);
|
||||
this.broadcastUpdate(req, list.id);
|
||||
req.flash("success", `${items.length} favoriete producten toegevoegd aan de lijst.`);
|
||||
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" });
|
||||
|
||||
Reference in New Issue
Block a user