Werk hoofdapplicatie bij
Legt de bestaande wijzigingen in de hoofdworktree vast voordat de laatste worktree wordt gemerged.\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -10,20 +10,23 @@ class AuthController {
|
||||
|
||||
registerForm(req, res) {
|
||||
|
||||
res.render("auth/register", {
|
||||
title: "Registreren"
|
||||
});
|
||||
res.locals.title = "Registreren";
|
||||
|
||||
res.render("auth/register");
|
||||
|
||||
}
|
||||
|
||||
|
||||
async register(req, res) {
|
||||
|
||||
const {
|
||||
name,
|
||||
email,
|
||||
password
|
||||
} = req.body;
|
||||
const name = (req.body.name || "").trim();
|
||||
const email = (req.body.email || "").trim().toLowerCase();
|
||||
const password = req.body.password || "";
|
||||
|
||||
if (name.length < 2 || !/^\S+@\S+\.\S+$/.test(email) || password.length < 8) {
|
||||
req.flash("error", "Vul een naam, geldig e-mailadres en een wachtwoord van minimaal 8 tekens in.");
|
||||
return res.redirect("/register");
|
||||
}
|
||||
|
||||
|
||||
const existingUser = db.prepare(
|
||||
@@ -67,7 +70,7 @@ class AuthController {
|
||||
);
|
||||
|
||||
|
||||
const userId = result.lastInsertRowid;
|
||||
const userId = result.lastInsertRowid;
|
||||
|
||||
|
||||
HouseholdService.createForUser(
|
||||
@@ -78,6 +81,10 @@ class AuthController {
|
||||
|
||||
req.session.userId = userId;
|
||||
|
||||
req.flash(
|
||||
"success",
|
||||
"Welkom bij PantryHub!"
|
||||
);
|
||||
|
||||
res.redirect("/");
|
||||
|
||||
@@ -87,9 +94,18 @@ class AuthController {
|
||||
|
||||
loginForm(req, res) {
|
||||
|
||||
res.render("auth/login", {
|
||||
title: "Inloggen"
|
||||
});
|
||||
res.locals.title = "Inloggen";
|
||||
|
||||
if (req.query.logout) {
|
||||
|
||||
req.flash(
|
||||
"info",
|
||||
"Je bent uitgelogd."
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
res.render("auth/login");
|
||||
|
||||
}
|
||||
|
||||
@@ -97,10 +113,8 @@ class AuthController {
|
||||
|
||||
async login(req, res) {
|
||||
|
||||
const {
|
||||
email,
|
||||
password
|
||||
} = req.body;
|
||||
const email = (req.body.email || "").trim().toLowerCase();
|
||||
const password = req.body.password || "";
|
||||
|
||||
|
||||
const user = db.prepare(
|
||||
@@ -141,6 +155,10 @@ class AuthController {
|
||||
|
||||
req.session.userId = user.id;
|
||||
|
||||
req.flash(
|
||||
"success",
|
||||
`Welkom terug ${user.name}!`
|
||||
);
|
||||
|
||||
res.redirect("/");
|
||||
|
||||
@@ -152,7 +170,9 @@ class AuthController {
|
||||
|
||||
req.session.destroy(() => {
|
||||
|
||||
res.redirect("/login");
|
||||
res.clearCookie("connect.sid");
|
||||
|
||||
res.redirect("/login?logout=1");
|
||||
|
||||
});
|
||||
|
||||
@@ -162,4 +182,4 @@ class AuthController {
|
||||
}
|
||||
|
||||
|
||||
module.exports = new AuthController();
|
||||
module.exports = new AuthController();
|
||||
|
||||
@@ -1,25 +1,28 @@
|
||||
class DashboardController {
|
||||
const ShoppingListRepository = require("../repositories/ShoppingListRepository");
|
||||
|
||||
class DashboardController {
|
||||
|
||||
index(req, res) {
|
||||
|
||||
const lists = ShoppingListRepository.getByHousehold(
|
||||
req.user.household_id
|
||||
);
|
||||
|
||||
const recentLists = lists.slice(0, 5);
|
||||
|
||||
res.locals.title = "Dashboard";
|
||||
|
||||
res.render(
|
||||
"dashboard/index",
|
||||
{
|
||||
|
||||
title: "Dashboard",
|
||||
|
||||
user: req.user
|
||||
|
||||
user: req.user,
|
||||
lists: recentLists,
|
||||
totalLists: lists.length
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
module.exports = new DashboardController();
|
||||
@@ -2,11 +2,9 @@ class HomeController {
|
||||
|
||||
index(req, res) {
|
||||
|
||||
res.render("home/index", {
|
||||
res.locals.title = "PantryHub";
|
||||
|
||||
title: "PantryHub"
|
||||
|
||||
});
|
||||
res.render("home/index");
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
const UserRepository = require("../repositories/UserRepository");
|
||||
|
||||
class HouseholdController {
|
||||
|
||||
index(req, res) {
|
||||
|
||||
index(req,res){
|
||||
res.locals.title = "Huishouden";
|
||||
|
||||
res.render(
|
||||
"household/index",
|
||||
{
|
||||
title:"Huishouden",
|
||||
user:req.user
|
||||
user: req.user,
|
||||
members: UserRepository.getHouseholdMembers(req.user.household_id)
|
||||
}
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
module.exports =
|
||||
new HouseholdController();
|
||||
module.exports = new HouseholdController();
|
||||
|
||||
@@ -1,35 +1,53 @@
|
||||
const ProductService =
|
||||
require("../services/ProductService");
|
||||
|
||||
const db = require("../config/database");
|
||||
|
||||
|
||||
class ProductController {
|
||||
|
||||
|
||||
index(req,res) {
|
||||
|
||||
index(req, res) {
|
||||
|
||||
const products =
|
||||
ProductService.getProducts();
|
||||
|
||||
const categories = db.prepare(`
|
||||
SELECT * FROM categories ORDER BY name
|
||||
`).all();
|
||||
|
||||
res.locals.title = "Producten";
|
||||
|
||||
res.render(
|
||||
"products/index",
|
||||
{
|
||||
|
||||
title:"Producten",
|
||||
|
||||
products
|
||||
|
||||
products,
|
||||
categories,
|
||||
user: req.user
|
||||
}
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
create(req, res) {
|
||||
|
||||
const { name, category_id } = req.body;
|
||||
|
||||
if (!name || name.trim().length === 0) {
|
||||
req.flash("error", "Geef een productnaam op");
|
||||
return res.redirect("/products");
|
||||
}
|
||||
|
||||
ProductService.createProduct({
|
||||
name: name.trim(),
|
||||
category_id: category_id || null
|
||||
});
|
||||
|
||||
req.flash("success", "Product toegevoegd");
|
||||
res.redirect("/products");
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
module.exports =
|
||||
new ProductController();
|
||||
@@ -0,0 +1,179 @@
|
||||
const ShoppingListRepository = require("../repositories/ShoppingListRepository");
|
||||
const ProductRepository = require("../repositories/ProductRepository");
|
||||
|
||||
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;
|
||||
|
||||
if (!name || name.trim().length === 0) {
|
||||
req.flash("error", "Geef een naam op");
|
||||
return res.redirect("/lists");
|
||||
}
|
||||
|
||||
// Check if user has a household
|
||||
if (!req.user || !req.user.household_id) {
|
||||
req.flash("error", "Je bent niet aan een huishouden gekoppeld");
|
||||
return res.redirect("/lists");
|
||||
}
|
||||
|
||||
ShoppingListRepository.create({
|
||||
household_id: req.user.household_id,
|
||||
name: name.trim()
|
||||
});
|
||||
|
||||
req.flash("success", "Boodschappenlijst aangemaakt");
|
||||
res.redirect("/lists");
|
||||
|
||||
}
|
||||
|
||||
show(req, res) {
|
||||
|
||||
const { id } = req.params;
|
||||
|
||||
const list = ShoppingListRepository.getById(id);
|
||||
|
||||
if (!list) {
|
||||
return res.status(404).render("errors/404");
|
||||
}
|
||||
|
||||
if (list.household_id !== req.user.household_id) {
|
||||
return res.status(403).render("errors/404");
|
||||
}
|
||||
|
||||
const items = ShoppingListRepository.getItems(id);
|
||||
|
||||
res.locals.title = list.name;
|
||||
|
||||
res.render("lists/show", {
|
||||
list,
|
||||
items,
|
||||
user: req.user
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
addItem(req, res) {
|
||||
|
||||
const { id } = req.params;
|
||||
const { product_id, amount } = req.body;
|
||||
|
||||
const list = ShoppingListRepository.getById(id);
|
||||
|
||||
if (!list) {
|
||||
return res.status(404).json({ error: "Lijst niet gevonden" });
|
||||
}
|
||||
|
||||
if (list.household_id !== req.user.household_id) {
|
||||
return res.status(403).json({ error: "Niet gemachtigd" });
|
||||
}
|
||||
|
||||
const product = ProductRepository.findById(product_id);
|
||||
|
||||
if (!product) {
|
||||
return res.status(404).json({ error: "Product niet gevonden" });
|
||||
}
|
||||
|
||||
const quantity = Number(amount);
|
||||
if (!Number.isFinite(quantity) || quantity <= 0 || quantity > 999) {
|
||||
req.flash("error", "Vul een geldige hoeveelheid in");
|
||||
return res.redirect(`/lists/${id}`);
|
||||
}
|
||||
|
||||
const item = ShoppingListRepository.addItem({
|
||||
list_id: id,
|
||||
product_id,
|
||||
amount: quantity
|
||||
});
|
||||
|
||||
req.app.locals.io.to(`household:${req.user.household_id}`).emit("item:added", {
|
||||
listId: Number(id)
|
||||
});
|
||||
|
||||
req.flash("success", "Product toegevoegd");
|
||||
|
||||
if (req.xhr || req.headers.accept?.includes("application/json")) {
|
||||
return res.json({ success: true, item });
|
||||
}
|
||||
|
||||
res.redirect(`/lists/${id}`);
|
||||
|
||||
}
|
||||
|
||||
toggleItem(req, res) {
|
||||
|
||||
const { id, itemId } = req.params;
|
||||
|
||||
const list = ShoppingListRepository.getById(id);
|
||||
|
||||
if (!list || list.household_id !== req.user.household_id) {
|
||||
return res.status(403).json({ error: "Niet gemachtigd" });
|
||||
}
|
||||
|
||||
if (!ShoppingListRepository.findItem(id, itemId)) {
|
||||
return res.status(404).json({ error: "Item niet gevonden" });
|
||||
}
|
||||
|
||||
ShoppingListRepository.toggleItem(itemId);
|
||||
|
||||
req.app.locals.io.to(`household:${req.user.household_id}`).emit("item:toggled", {
|
||||
listId: Number(id), itemId: Number(itemId)
|
||||
});
|
||||
|
||||
if (req.xhr || req.headers.accept?.includes("application/json")) {
|
||||
return res.json({ success: true });
|
||||
}
|
||||
|
||||
res.redirect(`/lists/${id}`);
|
||||
|
||||
}
|
||||
|
||||
deleteItem(req, res) {
|
||||
|
||||
const { id, itemId } = req.params;
|
||||
|
||||
const list = ShoppingListRepository.getById(id);
|
||||
|
||||
if (!list || list.household_id !== req.user.household_id) {
|
||||
return res.status(403).json({ error: "Niet gemachtigd" });
|
||||
}
|
||||
|
||||
if (!ShoppingListRepository.findItem(id, itemId)) {
|
||||
return res.status(404).json({ error: "Item niet gevonden" });
|
||||
}
|
||||
|
||||
ShoppingListRepository.deleteItem(itemId);
|
||||
|
||||
req.app.locals.io.to(`household:${req.user.household_id}`).emit("item:removed", {
|
||||
listId: Number(id), itemId: Number(itemId)
|
||||
});
|
||||
|
||||
req.flash("success", "Product verwijderd");
|
||||
|
||||
if (req.method === "DELETE" || req.xhr || req.headers.accept?.includes("application/json")) {
|
||||
return res.json({ success: true });
|
||||
}
|
||||
|
||||
res.redirect(`/lists/${id}`);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = new ShoppingListController();
|
||||
Reference in New Issue
Block a user