-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
180 lines (173 loc) · 3.98 KB
/
db.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const data = {
books: [
{
title: "Harry Potter and the Sorcerer's Stone",
coverPath: "/images/book-covers/harry1.jpg"
},
{
title: "Harry Potter and the Chamber of Secrets",
coverPath: "/images/book-covers/harry2.jpg"
},
{
title: "Harry Potter and the Prisoner of Azkaban",
coverPath: "/images/book-covers/harry3.jpg"
},
{
title: "Harry Potter and the Goblet of Fire",
coverPath: "/images/book-covers/harry4.jpg"
},
{
title: "Harry Potter and the Order of the Phoenix",
coverPath: "/images/book-covers/harry5.jpg"
},
{
title: "Harry Potter and the Half-Blood Prince",
coverPath: "/images/book-covers/harry6.jpg"
},
{
title: "Harry Potter and the Deathly Hallows",
coverPath: "/images/book-covers/harry7.jpg"
},
{
title: "Leviathan Wakes",
coverPath: "/images/book-covers/expanse1.jpg"
},
{
title: "Caliban's War",
coverPath: "/images/book-covers/expanse2.jpg"
},
{
title: "Abaddon's Gate",
coverPath: "/images/book-covers/expanse3.jpg"
},
{
title: "Cibola Burn",
coverPath: "/images/book-covers/expanse4.jpg"
},
{
title: "Nemesis Games",
coverPath: "/images/book-covers/expanse5.jpg"
},
{
title: "Babylon's Ashes",
coverPath: "/images/book-covers/expanse6.jpg"
},
{
title: "Persepolis Rising",
coverPath: "/images/book-covers/expanse7.jpg"
},
{
title: "Tiamat's Wrath",
coverPath: "/images/book-covers/expanse8.jpg"
},
{
title: "Blood of Elves",
coverPath: "/images/book-covers/witcher1.jpg"
},
{
title: "Time of contempt",
coverPath: "/images/book-covers/witcher2.jpg"
},
{
title: "Baptism of fire",
coverPath: "/images/book-covers/witcher3.jpg"
},
{
title: "The tower of the swallow",
coverPath: "/images/book-covers/witcher4.jpg"
},
{
title: "The lady of the lake",
coverPath: "/images/book-covers/witcher5.jpg"
}
],
authors: [
{
name: "J. K. Rowling",
photoPath: "/images/book-authors/j-k-rowling.jpg"
},
{
name: "James S. A. Corey",
photoPath: "/images/book-authors/james-s-a-corey.jpg"
},
{
name: "Andrzej Sapkowski",
photoPath: "/images/book-authors/andrzej-sapkowski.jpg"
}
],
users: [
{
name: "Alice",
avatar: {
imagePath: "/images/avatars/w13.png",
color: "yellow"
}
},
{
name: "Bob",
avatar: {
imagePath: "/images/avatars/m10.png",
color: "green"
}
},
{
name: "Celine",
avatar: {
imagePath: "/images/avatars/w2.png",
color: "red"
}
},
{
name: "Dan",
avatar: {
imagePath: "/images/avatars/m25.png",
color: "blue"
}
}
],
bookIdsByAuthorId: {
1: [1, 2, 3, 4, 5, 6, 7],
2: [8, 9, 10, 11, 12, 13, 14],
3: [15, 16, 17, 18, 19, 20]
}
};
const getAuthorIdByBookId = bookId =>
parseInt(
Object.entries(data.bookIdsByAuthorId).find(([authorId, bookIds]) =>
bookIds.includes(bookId)
)[0],
10
);
const getBookById = id => ({
...data.books[id - 1],
id,
authorId: getAuthorIdByBookId(id)
});
const getAllBooks = () =>
data.books.map((book, index) => getBookById(index + 1));
const getAuthorById = id => ({
...data.authors[id - 1],
id,
bookIds: data.bookIdsByAuthorId[id]
});
const getAllAuthors = () =>
data.authors.map((author, index) => getAuthorById(index + 1));
const getUserById = id => ({
...data.users[id - 1],
id
});
const getAllUsers = () =>
data.users.map((user, index) => getUserById(index + 1));
const db = {
getAllBooks,
getAllAuthors,
getAllUsers,
getBookById,
getAuthorById,
getUserById
};
module.exports = db;