This commit is contained in:
39
webseite/backend/model/ProductModel.js
Normal file
39
webseite/backend/model/ProductModel.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import fs from 'node:fs';
|
||||
|
||||
// Factoryfunction
|
||||
const ProductModel = () => {
|
||||
let products = [];
|
||||
|
||||
const load = () => {
|
||||
try {
|
||||
products = JSON.parse(fs.readFileSync('./data/products.json', 'utf-8'));
|
||||
} catch (error) {
|
||||
console.log('Something went wrong: ', error);
|
||||
products = [];
|
||||
}
|
||||
};
|
||||
|
||||
const getAll = () => {
|
||||
return products;
|
||||
};
|
||||
|
||||
const get = (id) => {
|
||||
const product = products.find((product) => {
|
||||
return product._id === id;
|
||||
});
|
||||
|
||||
// Guard
|
||||
if (!product) return null;
|
||||
|
||||
return product;
|
||||
};
|
||||
|
||||
return {
|
||||
// load: load
|
||||
load,
|
||||
getAll,
|
||||
get,
|
||||
};
|
||||
};
|
||||
|
||||
export default ProductModel;
|
||||
Reference in New Issue
Block a user