-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
48 lines (42 loc) · 1.13 KB
/
script.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
class FabricateAPI {
constructor() {
this.data = [];
this.id = 0;
}
// Get all items from the mock API
getAll() {
return JSON.parse(localStorage.getItem('fabricateAPI')) || this.data;
}
// Get a specific item by ID
get(id) {
const items = this.getAll();
return items.find((item) => item.id === id);
}
// Add a new item to the mock API
add(item) {
const items = this.getAll();
item.id = ++this.id;
items.push(item);
localStorage.setItem('fabricateAPI', JSON.stringify(items));
}
// Update an existing item in the mock API
update(id, item) {
const items = this.getAll();
const index = items.findIndex((item) => item.id === id);
if (index !== -1) {
item.id = id;
items[index] = item;
localStorage.setItem('fabricateAPI', JSON.stringify(items));
}
}
// Delete an item from the mock API
delete(id) {
const items = this.getAll();
const index = items.findIndex((item) => item.id === id);
if (index !== -1) {
items.splice(index, 1);
localStorage.setItem('fabricateAPI', JSON.stringify(items));
}
}
}
export default FabricateAPI;