Merge laatste worktree in master
Neemt de laatste PantryHub-implementatie over in de hoofdbranch.\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -1,108 +1,71 @@
|
||||
const db = require("../config/database");
|
||||
|
||||
class ShoppingListRepository {
|
||||
|
||||
getByHousehold(householdId) {
|
||||
|
||||
return db.prepare(`
|
||||
SELECT
|
||||
shopping_lists.*,
|
||||
COUNT(shopping_list_items.id) as item_count,
|
||||
SUM(CASE WHEN shopping_list_items.checked = 1 THEN 1 ELSE 0 END) as checked_count
|
||||
SELECT shopping_lists.*,
|
||||
COUNT(shopping_list_items.id) AS item_count,
|
||||
COALESCE(SUM(shopping_list_items.checked), 0) AS checked_count
|
||||
FROM shopping_lists
|
||||
LEFT JOIN shopping_list_items
|
||||
ON shopping_lists.id = shopping_list_items.list_id
|
||||
WHERE shopping_lists.household_id = ?
|
||||
AND shopping_lists.archived = 0
|
||||
LEFT JOIN shopping_list_items
|
||||
ON shopping_list_items.list_id = shopping_lists.id
|
||||
WHERE shopping_lists.household_id = ? AND shopping_lists.archived = 0
|
||||
GROUP BY shopping_lists.id
|
||||
ORDER BY shopping_lists.created_at DESC
|
||||
ORDER BY shopping_lists.created_at DESC, shopping_lists.id DESC
|
||||
`).all(householdId);
|
||||
|
||||
}
|
||||
|
||||
getById(id) {
|
||||
|
||||
findById(id, householdId) {
|
||||
return db.prepare(`
|
||||
SELECT * FROM shopping_lists WHERE id = ?
|
||||
`).get(id);
|
||||
|
||||
}
|
||||
|
||||
create(data) {
|
||||
|
||||
return db.prepare(`
|
||||
INSERT INTO shopping_lists
|
||||
(household_id, name)
|
||||
VALUES (?, ?)
|
||||
`).run(data.household_id, data.name);
|
||||
|
||||
SELECT * FROM shopping_lists
|
||||
WHERE id = ? AND household_id = ?
|
||||
`).get(id, householdId);
|
||||
}
|
||||
|
||||
getItems(listId) {
|
||||
|
||||
return db.prepare(`
|
||||
SELECT
|
||||
shopping_list_items.*,
|
||||
products.name as product_name,
|
||||
categories.name as category_name,
|
||||
categories.icon as category_icon
|
||||
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 = ?
|
||||
ORDER BY shopping_list_items.checked ASC, shopping_list_items.sort_order ASC
|
||||
ORDER BY shopping_list_items.checked ASC,
|
||||
shopping_list_items.sort_order ASC,
|
||||
shopping_list_items.created_at ASC
|
||||
`).all(listId);
|
||||
|
||||
}
|
||||
|
||||
addItem(data) {
|
||||
|
||||
create(householdId, name) {
|
||||
return db.prepare(`
|
||||
INSERT INTO shopping_list_items
|
||||
(list_id, product_id, amount)
|
||||
VALUES (?, ?, ?)
|
||||
`).run(data.list_id, data.product_id, data.amount);
|
||||
|
||||
INSERT INTO shopping_lists (household_id, name) VALUES (?, ?)
|
||||
`).run(householdId, name);
|
||||
}
|
||||
|
||||
findItem(listId, itemId) {
|
||||
|
||||
addItem(listId, productId, amount) {
|
||||
const nextSortOrder = db.prepare(`
|
||||
SELECT COALESCE(MAX(sort_order), -1) + 1 AS value
|
||||
FROM shopping_list_items WHERE list_id = ?
|
||||
`).get(listId).value;
|
||||
return db.prepare(`
|
||||
SELECT id FROM shopping_list_items
|
||||
WHERE id = ? AND list_id = ?
|
||||
`).get(itemId, listId);
|
||||
|
||||
INSERT INTO shopping_list_items (list_id, product_id, amount, sort_order)
|
||||
VALUES (?, ?, ?, ?)
|
||||
`).run(listId, productId, amount, nextSortOrder);
|
||||
}
|
||||
|
||||
toggleItem(itemId) {
|
||||
|
||||
toggleItem(listId, itemId) {
|
||||
return db.prepare(`
|
||||
UPDATE shopping_list_items
|
||||
SET checked = CASE WHEN checked = 1 THEN 0 ELSE 1 END
|
||||
WHERE id = ?
|
||||
`).run(itemId);
|
||||
|
||||
SET checked = CASE checked WHEN 0 THEN 1 ELSE 0 END
|
||||
WHERE id = ? AND list_id = ?
|
||||
`).run(itemId, listId);
|
||||
}
|
||||
|
||||
deleteItem(itemId) {
|
||||
|
||||
deleteItem(listId, itemId) {
|
||||
return db.prepare(`
|
||||
DELETE FROM shopping_list_items
|
||||
WHERE id = ?
|
||||
`).run(itemId);
|
||||
|
||||
DELETE FROM shopping_list_items WHERE id = ? AND list_id = ?
|
||||
`).run(itemId, listId);
|
||||
}
|
||||
|
||||
archiveList(listId) {
|
||||
|
||||
return db.prepare(`
|
||||
UPDATE shopping_lists
|
||||
SET archived = 1
|
||||
WHERE id = ?
|
||||
`).run(listId);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = new ShoppingListRepository();
|
||||
|
||||
Reference in New Issue
Block a user