Files
PantryHub/repositories/ProductRepository.js
T
2026-07-26 22:18:52 +02:00

85 lines
1001 B
JavaScript

const db = require("../config/database");
class ProductRepository {
getAll() {
return db.prepare(`
SELECT
products.*,
categories.name AS category_name,
categories.icon AS category_icon
FROM products
LEFT JOIN categories
ON categories.id = products.category_id
ORDER BY products.name
`).all();
}
findById(id) {
return db.prepare(`
SELECT *
FROM products
WHERE id = ?
`).get(id);
}
create(data) {
return db.prepare(`
INSERT INTO products
(
name,
category_id,
barcode
)
VALUES (?, ?, ?)
`).run(
data.name,
data.category_id || null,
data.barcode || null
);
}
}
module.exports = new ProductRepository();