Multi tenant DB, registratie per huishouden V1.2
This commit is contained in:
@@ -4,7 +4,7 @@ const db = require("../config/database");
|
||||
class CategoryRepository {
|
||||
|
||||
|
||||
getAll() {
|
||||
getAll(householdId) {
|
||||
|
||||
return db.prepare(`
|
||||
|
||||
@@ -13,23 +13,26 @@ class CategoryRepository {
|
||||
FROM categories
|
||||
|
||||
LEFT JOIN products ON products.category_id = categories.id
|
||||
AND products.household_id = categories.household_id
|
||||
|
||||
WHERE categories.household_id = ?
|
||||
|
||||
GROUP BY categories.id
|
||||
|
||||
ORDER BY categories.name
|
||||
|
||||
`).all();
|
||||
`).all(householdId);
|
||||
|
||||
}
|
||||
|
||||
|
||||
findById(id) {
|
||||
findById(id, householdId) {
|
||||
|
||||
return db.prepare(`
|
||||
|
||||
SELECT * FROM categories WHERE id = ?
|
||||
SELECT * FROM categories WHERE id = ? AND household_id = ?
|
||||
|
||||
`).get(id);
|
||||
`).get(id, householdId);
|
||||
|
||||
}
|
||||
|
||||
@@ -38,16 +41,16 @@ class CategoryRepository {
|
||||
|
||||
return db.prepare(`
|
||||
|
||||
INSERT INTO categories (name, icon)
|
||||
INSERT INTO categories (name, icon, household_id)
|
||||
|
||||
VALUES (?, ?)
|
||||
VALUES (?, ?, ?)
|
||||
|
||||
`).run(data.name, data.icon || null);
|
||||
`).run(data.name, data.icon || null, data.household_id);
|
||||
|
||||
}
|
||||
|
||||
|
||||
update(id, data) {
|
||||
update(id, householdId, data) {
|
||||
|
||||
return db.prepare(`
|
||||
|
||||
@@ -55,31 +58,31 @@ class CategoryRepository {
|
||||
|
||||
SET name = ?, icon = ?
|
||||
|
||||
WHERE id = ?
|
||||
WHERE id = ? AND household_id = ?
|
||||
|
||||
`).run(data.name, data.icon || null, id);
|
||||
`).run(data.name, data.icon || null, id, householdId);
|
||||
|
||||
}
|
||||
|
||||
|
||||
hasProducts(id) {
|
||||
hasProducts(id, householdId) {
|
||||
|
||||
return db.prepare(`
|
||||
|
||||
SELECT 1 FROM products WHERE category_id = ? LIMIT 1
|
||||
SELECT 1 FROM products WHERE category_id = ? AND household_id = ? LIMIT 1
|
||||
|
||||
`).get(id) !== undefined;
|
||||
`).get(id, householdId) !== undefined;
|
||||
|
||||
}
|
||||
|
||||
|
||||
delete(id) {
|
||||
delete(id, householdId) {
|
||||
|
||||
return db.prepare(`
|
||||
|
||||
DELETE FROM categories WHERE id = ?
|
||||
DELETE FROM categories WHERE id = ? AND household_id = ?
|
||||
|
||||
`).run(id);
|
||||
`).run(id, householdId);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ const db = require("../config/database");
|
||||
class ProductRepository {
|
||||
|
||||
|
||||
getAll() {
|
||||
getAll(householdId) {
|
||||
|
||||
return db.prepare(`
|
||||
|
||||
@@ -24,16 +24,18 @@ class ProductRepository {
|
||||
|
||||
ON categories.id = products.category_id
|
||||
|
||||
WHERE products.household_id = ?
|
||||
|
||||
|
||||
ORDER BY products.name
|
||||
|
||||
`).all();
|
||||
`).all(householdId);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
findById(id) {
|
||||
findById(id, householdId) {
|
||||
|
||||
return db.prepare(`
|
||||
|
||||
@@ -41,9 +43,9 @@ class ProductRepository {
|
||||
|
||||
FROM products
|
||||
|
||||
WHERE id = ?
|
||||
WHERE id = ? AND household_id = ?
|
||||
|
||||
`).get(id);
|
||||
`).get(id, householdId);
|
||||
|
||||
}
|
||||
|
||||
@@ -59,10 +61,11 @@ class ProductRepository {
|
||||
(
|
||||
name,
|
||||
category_id,
|
||||
barcode
|
||||
barcode,
|
||||
household_id
|
||||
)
|
||||
|
||||
VALUES (?, ?, ?)
|
||||
VALUES (?, ?, ?, ?)
|
||||
|
||||
`).run(
|
||||
|
||||
@@ -70,7 +73,8 @@ class ProductRepository {
|
||||
|
||||
data.category_id || null,
|
||||
|
||||
data.barcode || null
|
||||
data.barcode || null,
|
||||
data.household_id
|
||||
|
||||
);
|
||||
|
||||
@@ -81,9 +85,9 @@ class ProductRepository {
|
||||
createMany(products) {
|
||||
const insert = db.prepare(`
|
||||
|
||||
INSERT INTO products (name, category_id, barcode)
|
||||
INSERT INTO products (name, category_id, barcode, household_id)
|
||||
|
||||
VALUES (?, ?, ?)
|
||||
VALUES (?, ?, ?, ?)
|
||||
|
||||
`);
|
||||
|
||||
@@ -92,7 +96,8 @@ class ProductRepository {
|
||||
insert.run(
|
||||
product.name,
|
||||
product.category_id || null,
|
||||
product.barcode || null
|
||||
product.barcode || null,
|
||||
product.household_id
|
||||
);
|
||||
}
|
||||
});
|
||||
@@ -102,7 +107,7 @@ class ProductRepository {
|
||||
}
|
||||
|
||||
|
||||
update(id, data) {
|
||||
update(id, householdId, data) {
|
||||
|
||||
return db.prepare(`
|
||||
|
||||
@@ -110,7 +115,7 @@ class ProductRepository {
|
||||
|
||||
SET name = ?, category_id = ?
|
||||
|
||||
WHERE id = ?
|
||||
WHERE id = ? AND household_id = ?
|
||||
|
||||
`).run(
|
||||
|
||||
@@ -118,14 +123,15 @@ class ProductRepository {
|
||||
|
||||
data.category_id || null,
|
||||
|
||||
id
|
||||
id,
|
||||
householdId
|
||||
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
|
||||
hasListItems(id) {
|
||||
hasListItems(id, householdId) {
|
||||
|
||||
return db.prepare(`
|
||||
|
||||
@@ -133,24 +139,28 @@ class ProductRepository {
|
||||
|
||||
FROM shopping_list_items
|
||||
|
||||
WHERE product_id = ?
|
||||
WHERE product_id = ? AND EXISTS (
|
||||
SELECT 1 FROM shopping_lists
|
||||
WHERE shopping_lists.id = shopping_list_items.list_id
|
||||
AND shopping_lists.household_id = ?
|
||||
)
|
||||
|
||||
LIMIT 1
|
||||
|
||||
`).get(id) !== undefined;
|
||||
`).get(id, householdId) !== undefined;
|
||||
|
||||
}
|
||||
|
||||
|
||||
delete(id) {
|
||||
delete(id, householdId) {
|
||||
|
||||
return db.prepare(`
|
||||
|
||||
DELETE FROM products
|
||||
|
||||
WHERE id = ?
|
||||
WHERE id = ? AND household_id = ?
|
||||
|
||||
`).run(id);
|
||||
`).run(id, householdId);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -22,18 +22,19 @@ class ShoppingListRepository {
|
||||
`).get(id, householdId);
|
||||
}
|
||||
|
||||
getItems(listId) {
|
||||
getItems(listId, householdId) {
|
||||
return db.prepare(`
|
||||
SELECT shopping_list_items.*, products.name AS product_name,
|
||||
categories.name AS category_name, categories.icon AS category_icon
|
||||
FROM shopping_list_items
|
||||
JOIN products ON products.id = shopping_list_items.product_id
|
||||
LEFT JOIN categories ON categories.id = products.category_id
|
||||
WHERE shopping_list_items.list_id = ?
|
||||
WHERE shopping_list_items.list_id = ?
|
||||
AND products.household_id = ?
|
||||
ORDER BY shopping_list_items.checked ASC,
|
||||
shopping_list_items.sort_order ASC,
|
||||
shopping_list_items.created_at ASC
|
||||
`).all(listId);
|
||||
`).all(listId, householdId);
|
||||
}
|
||||
|
||||
create(householdId, name) {
|
||||
|
||||
Reference in New Issue
Block a user