Files
PantryHub/controllers/ProductController.js
T
jurgenr ce68182c08 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>
2026-08-07 20:44:05 +02:00

56 lines
1.2 KiB
JavaScript

const ProductService =
require("../services/ProductService");
const db = require("../config/database");
class ProductController {
index(req, res) {
const products =
ProductService.getProducts();
const categories = db.prepare(
"SELECT * FROM categories ORDER BY name"
).all();
res.render(
"products/index",
{
title:"Producten",
products,
categories
}
);
}
create(req, res) {
const name = (req.body.name || "").trim();
const categoryId = req.body.category_id
? Number.parseInt(req.body.category_id, 10)
: null;
if (!name || (categoryId !== null && !Number.isInteger(categoryId))) {
req.flash("error", "Geef een geldige productnaam en categorie op.");
return res.redirect("/products");
}
ProductService.createProduct({
name,
category_id: categoryId
});
req.flash("success", "Product toegevoegd.");
res.redirect("/products");
}
}
module.exports =
new ProductController();