-
Notifications
You must be signed in to change notification settings - Fork 0
/
book.js
44 lines (38 loc) · 967 Bytes
/
book.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
const books = [
{ id: '1', title: 'Book 1', author: 'Author 1' },
{ id: '2', title: 'Book 2', author: 'Author 2' },
];
function getAll() {
return books;
}
function getById(id) {
return books.find(book => book.id === id);
}
function create(book) {
const newBook = { id: (books.length + 1).toString(), ...book };
books.push(newBook);
return newBook;
}
function update(id, bookData) {
const index = books.findIndex(book => book.id === id);
if (index !== -1) {
books[index] = { ...books[index], ...bookData };
return books[index];
}
return null;
}
function deleteBook(id) {
const index = books.findIndex(book => book.id === id);
if (index !== -1) {
books.splice(index, 1);
return true;
}
return false;
}
module.exports = {
getAll,
getById,
create,
update,
delete: deleteBook,
};