-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathstore.js
More file actions
39 lines (30 loc) · 837 Bytes
/
Copy pathstore.js
File metadata and controls
39 lines (30 loc) · 837 Bytes
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
// A tiny in-memory data store. It stands in for a real database so the
// project stays easy to run. Data is not persisted — it resets every time
// the server restarts.
let users = [
{ id: 1, name: "Ada Lovelace", email: "ada@example.com" },
{ id: 2, name: "Alan Turing", email: "alan@example.com" },
];
let nextId = 3;
function getAllUsers() {
return users;
}
function getUserById(id) {
return users.find((user) => user.id === id);
}
function createUser({ name, email }) {
const user = { id: nextId, name, email };
nextId += 1;
users.push(user);
return user;
}
function updateUser(id, { name, email }) {
const user = getUserById(id);
if (!user) {
return undefined;
}
user.name = name;
user.email = email;
return user;
}
module.exports = { getAllUsers, getUserById, createUser, updateUser };