-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.js
More file actions
96 lines (82 loc) · 2.19 KB
/
service.js
File metadata and controls
96 lines (82 loc) · 2.19 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
class Service {
constructor(repo, collection) {
this.repo_ = repo;
this.collection_ = collection;
}
/// CRUD operations for simple repo
async serviceAddBook(book) {
try {
await this.repo_.repoAddBook(book);
}
catch (error) {
console.error("Error adding book : ", error.message);
}
}
async serviceRemoveBook(book) {
try {
await this.repo_.repoRemoveBook(book);
}
catch (error) {
console.error("Error removing book : ", error.message);
}
}
async serviceUpdateBook(oldBook, newBook) {
try {
await this.repo_.repoUpdateBook(oldBook, newBook);
}
catch (error) {
console.error("Error updating book : ", error.message);
}
}
async serviceFindBook(predicateFn) {
const books = await this.serviceGetAll();
return books.find(predicateFn);
}
async serviceGetAll() {
const books = await this.repo_.repoGetAll();
return books;
}
async serviceFilterBooks(filterFn) {
const books = await this.repo_.repoGetAll();
return books.filter(filterFn);
}
async serviceSortBooks(compareFn) {
const books = await this.repo_.repoGetAll();
return books.sort(compareFn);
}
/// CRUD for Collection repo - colectie / cos
async serviceCollectionAdd(book) {
try {
this.collection_.collectionAddBook(book);
}
catch (error) {
console.error("Error adding to collection : ", error.message);
}
}
async serviceCollectionRemove(book) {
try {
this.collection_.collectionRemoveBook(book);
}
catch (error) {
console.error("Error removing from the collection : ", error.message);
}
}
async serviceCollectionUpdate(oldBook, newBook) {
try {
this.collection_.collectionUpdateBook(oldBook, newBook);
}
catch (error) {
console.error("Error updating the collection : ", error.message);
}
}
async serviceCollectionFindBook(predicateFn) {
return this.collection_.collectionFindBook(predicateFn);
}
async serviceCollectionGetAll() {
return this.collection_.collectionGetAll();
}
async serviceCollectionAddRandom(number) {
this.collection_.generateNumberOfBooks(number);
}
}
module.exports = Service;