-
Notifications
You must be signed in to change notification settings - Fork 0
/
store-service.js
117 lines (100 loc) · 3.71 KB
/
store-service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*********************************************************************************
WEB322 – Assignment 02
I declare that this assignment is my own work in accordance with Seneca Academic Policy.
No part of this assignment has been copied manually or electronically from any other source
(including 3rd party web sites) or distributed to other students.
Name: Agenor Dionizio da Silva Junior
Date: Oct 31, 2024
Student ID: 138121223
Replit Web App URL:
GitHub Repository URL: https://github.com/Agenor-Junior/web322-app.git
********************************************************************************/
const fs = require("fs");
let itemsArray = [];
let categoriesArray = [];
const pathToItems = "./data/items.json";
const pathToCat = "./data/categories.json";
function initialize() {
return new Promise((resolve, reject) => {
fs.readFile(pathToItems, "utf8", (err, jsonString) => {
if (err) {
return reject("Unable to read ITEMS file");
}
try {
// Parse the items file
itemsArray = JSON.parse(jsonString);
fs.readFile(pathToCat, "utf-8", (err, jsonString) => {
if (err) {
return reject("Unable to read CATEGORIES file");
}
try {
// Parse the categories file
categoriesArray = JSON.parse(jsonString);
resolve("Initialization successful!");
} catch (error) {
reject("Unable to parse CATEGORIES file");
}
});
} catch (error) {
reject("Unable to parse ITEMS file");
}
});
});
}
function getAllItems() {
return new Promise((resolve, reject) => {
itemsArray.length === 0 ? reject("no results returned") : resolve(itemsArray);
});
}
function getPublishedItems() {
return new Promise((resolve, reject) => {
const publishedItems = itemsArray.filter((item) => item.published);
publishedItems.length === 0 ? reject("no results returned") : resolve(publishedItems);
});
}
function getCategories() {
return new Promise((resolve, reject) => {
categoriesArray.length === 0 ? reject("no results returned") : resolve(categoriesArray);
});
}
function addItem(itemData) {
return new Promise((resolve, reject) => {
itemData.published = itemData.published ? true : false;
itemData.id = itemsArray.length + 1;
itemsArray.push(itemData);
resolve(itemData);
});
}
// Step 1: Function to get items by category
function getItemsByCategory(category) {
return new Promise((resolve, reject) => {
const filteredItems = itemsArray.filter(item => item.category === category);
filteredItems.length ? resolve(filteredItems) : reject("no results returned");
});
}
// Step 2: Function to get items by minimum date
function getItemsByMinDate(minDateStr) {
return new Promise((resolve, reject) => {
const minDate = new Date(minDateStr);
const filteredItems = itemsArray.filter(item => new Date(item.postDate) >= minDate);
filteredItems.length ? resolve(filteredItems) : reject("no results returned");
});
}
// Step 3: Function to get item by ID
function getItemById(id) {
return new Promise((resolve, reject) => {
const item = itemsArray.find(item => item.id === parseInt(id));
item ? resolve(item) : reject("no result returned");
});
}
// Exporting the functions
module.exports = {
initialize,
getAllItems,
getPublishedItems,
getCategories,
addItem,
getItemsByCategory,
getItemsByMinDate,
getItemById
};