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();