40 lines
652 B
JavaScript
40 lines
652 B
JavaScript
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;
|