Versie 1.0
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
const db = require("../config/database");
|
||||
|
||||
|
||||
class CategoryRepository {
|
||||
|
||||
|
||||
getAll() {
|
||||
|
||||
return db.prepare(`
|
||||
|
||||
SELECT categories.*, COUNT(products.id) AS product_count
|
||||
|
||||
FROM categories
|
||||
|
||||
LEFT JOIN products ON products.category_id = categories.id
|
||||
|
||||
GROUP BY categories.id
|
||||
|
||||
ORDER BY categories.name
|
||||
|
||||
`).all();
|
||||
|
||||
}
|
||||
|
||||
|
||||
findById(id) {
|
||||
|
||||
return db.prepare(`
|
||||
|
||||
SELECT * FROM categories WHERE id = ?
|
||||
|
||||
`).get(id);
|
||||
|
||||
}
|
||||
|
||||
|
||||
create(data) {
|
||||
|
||||
return db.prepare(`
|
||||
|
||||
INSERT INTO categories (name, icon)
|
||||
|
||||
VALUES (?, ?)
|
||||
|
||||
`).run(data.name, data.icon || null);
|
||||
|
||||
}
|
||||
|
||||
|
||||
update(id, data) {
|
||||
|
||||
return db.prepare(`
|
||||
|
||||
UPDATE categories
|
||||
|
||||
SET name = ?, icon = ?
|
||||
|
||||
WHERE id = ?
|
||||
|
||||
`).run(data.name, data.icon || null, id);
|
||||
|
||||
}
|
||||
|
||||
|
||||
hasProducts(id) {
|
||||
|
||||
return db.prepare(`
|
||||
|
||||
SELECT 1 FROM products WHERE category_id = ? LIMIT 1
|
||||
|
||||
`).get(id) !== undefined;
|
||||
|
||||
}
|
||||
|
||||
|
||||
delete(id) {
|
||||
|
||||
return db.prepare(`
|
||||
|
||||
DELETE FROM categories WHERE id = ?
|
||||
|
||||
`).run(id);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
module.exports = new CategoryRepository();
|
||||
Reference in New Issue
Block a user