-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdummy-database.js
More file actions
128 lines (120 loc) · 2.91 KB
/
Copy pathdummy-database.js
File metadata and controls
128 lines (120 loc) · 2.91 KB
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
118
119
120
121
122
123
124
125
126
127
128
/**
* Our dummy database has two tables: tasks and users
* You DO NOT need to edit this file. But please do read it, and understand the methods you can use.
* For instance, if you want to create a new task, you can do:
* const task = Task.create({
* title: "Fold Laundry",
* description: "Fold all the laundry in the laundry room",
* completed: false,
* userId: 1,
* });
* If you want to delete a task, you can do:
* Task.delete(1);
*/
/**
* Task.findAll() -> returns all tasks
* Task.findByPk(id) -> returns a single task by id
* Task.create(task) -> creates a new task
* Task.update(id, task) -> updates a task by id
* Task.delete(id) -> deletes a task by id
*
* User.findAll() -> returns all users
* User.findByPk(id) -> returns a single user by id
* User.create(user) -> creates a new user
* User.update(id, user) -> updates a user by id
* User.delete(id) -> deletes a user by id
*/
/**
* Each task has:
* - id
* - title
* - description
* - completed
* - userId
*/
/**
* Each user has:
* - id
* - name
*/
const tasks = [
{
id: 1,
title: "Fold Laundry",
description: "Fold all the laundry in the laundry room",
completed: false,
userId: 1,
},
{
id: 2,
title: "Wash Dishes",
description: "The dishes are starting to gain sentience",
completed: true,
userId: 2,
},
];
let nextTaskId = tasks.length + 1;
const users = [
{ id: 1, name: "Finn" },
{ id: 2, name: "Shahid" },
];
let nextUserId = users.length + 1;
const Task = {
findAll: function () {
return tasks;
},
findByPk: function (id) {
return tasks.find((task) => task.id === id);
},
create: function (task) {
task.id = nextTaskId++;
tasks.push(task);
return task;
},
update: function (id, task) {
const index = tasks.findIndex((task) => task.id === id);
if (index === -1) {
throw new Error("Task not found");
}
console.log("Updating task", tasks[index]);
console.log("With task", task);
tasks[index] = { ...tasks[index], ...task };
return task;
},
delete: function (id) {
const index = tasks.findIndex((task) => task.id === id);
if (index === -1) {
throw new Error("Task not found");
}
tasks.splice(index, 1);
},
};
const User = {
findAll: function () {
return users;
},
findByPk: function (id) {
return users.find((user) => user.id === id);
},
create: function (user) {
user.id = nextUserId++;
users.push(user);
return user;
},
update: function (id, user) {
const index = users.findIndex((user) => user.id === id);
if (index === -1) {
throw new Error("User not found");
}
users[index] = { ...users[index], ...user };
return user;
},
delete: function (id) {
const index = users.findIndex((user) => user.id === id);
if (index === -1) {
throw new Error("User not found");
}
users.splice(index, 1);
},
};
module.exports = { Task, User };