-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudents.js
79 lines (57 loc) · 1.43 KB
/
Students.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
const db = {
students: [
{id: 0, firstName: "Virgil", lastName: "Bugnariu"},
{id: 1, firstName: "Adrian", lastName: "Mihai"},
]
}
let lastId = 1;
class Students {
getAll() {
return db.students;
}
get(id) {
const studentId = parseInt(id);
const result = db.students.find((student) => student.id === studentId)
return result;
}
create(firstName, lastName) {
lastId += 1;
const newStudent = {
id: lastId,
firstName,
lastName,
};
db.students.push(newStudent);
return newStudent;
}
delete(id) {
const studentId = parseInt(id);
const student = db.students.find(student => student.id === studentId);
if(!student) {
return false;
}
const updatedStudentsList = db.students.filter((student) => student.id !== studentId);
db.students = updatedStudentsList;
return true;
}
update(id, firstName, lastName) {
const student = db.students.find(student => student.id === parseInt(id));
if(!student) {
return null;
}
const updatedStudent = {
...student,
firstName,
lastName,
}
db.students = db.students.map(student => {
if(student.id === parseInt(id, 10)) {
return updatedStudent;
} else {
return student;
}
});
return updatedStudent;
}
}
module.exports = Students;