-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
205 lines (151 loc) · 4.66 KB
/
index.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const express = require("express");
//intializing express
const booky = express();
//importing database
const database = require("./database");
/*
Route /
Description Get all the books
Access PUBLIC
Parameter NONE
Methods GET
*/
booky.get("/", (req, res)=>{
res.json({data: database.books});
});
/*
Route /is
Description Get specific book with ISBN
Access PUBLIC
Parameter isbn
Methods GET
*/
booky.get("/is/:isbn", (req, res)=>{
const getSpecificBook = database.books.filter(
(book)=>book.ISBN === req.params.isbn
);
if(getSpecificBook.length !== 0){
return res.json({book:getSpecificBook});
}
return res.json({error:`The Book with ISBN: ${req.params.isbn} has not found in the database`});
});
/*
Route /c
Description Get specific book on category
Access PUBLIC
Parameter category
Methods GET
*/
booky.get("/c/:category", (req, res)=>{
const getSpecificBook = database.books.filter(
(book) => book.category.includes(req.params.category)
);
if(getSpecificBook.length !== 0){
return res.json({book:getSpecificBook});
}
return res.json({error:`Book with category '${req.params.category}' has not found in the database`});
});
/*
Route /l
Description Get specific book on language
Access PUBLIC
Parameter lang
Methods GET
*/
booky.get("/l/:lang", (req, res)=>{
const getSpecificBook = database.books.filter(
(book) => book.language.includes(req.params.lang)
);
if(getSpecificBook.length !== 0){
return res.json({book:getSpecificBook});
}
return res.json({error:`Book with Language '${req.params.lang}' has not found in the database`});
});
/*
Route /author
Description Get all authors
Access PUBLIC
Parameter NONE
Methods GET
*/
booky.get("/author", (req, res)=>{
return res.json({authors : database.author});
});
/*
Route /author/book
Description Get all authors based on Book
Access PUBLIC
Parameter isbn
Methods GET
*/
booky.get("/author/book/:isbn", (req, res)=>{
const getSpecificAuthor = database.author.filter(
(author) => author.books.includes(req.params.isbn)
);
if(getSpecificAuthor.length !== 0){
res.json({author : getSpecificAuthor});
}
return res.json({error:`No Author found for the book with ISBN:${req.params.isbn} `});
});
/*
Route /author/id
Description Get authors based on id
Access PUBLIC
Parameter id
Methods GET
*/
booky.get("/author/id/:id", (req, res)=>{
const getSpecificAuthor = database.author.filter(
(author) => author.id === parseInt(req.params.id)
);
if(getSpecificAuthor.length !== 0){
return res.json({Author : getSpecificAuthor});
}
return res.json({error : `No author Found with ID : ${req.params.id}`});
});
/*
Route /publications
Description Get all publications
Access PUBLIC
Parameter NONE
Methods GET
*/
booky.get("/publications", (req, res)=> {
return res.json({publications: database.publication});
});
/*
Route /publications/id
Description Get Specific publications
Access PUBLIC
Parameter id
Methods GET
*/
booky.get("/publications/id/:id", (req, res)=>{
const getSpecificPublication = database.publication.filter(
(publication) => publication.id == parseInt(req.params.id)
);
if(getSpecificPublication.length !== 0){
return res.json({publication: getSpecificPublication});
}
return res.json({error : `No Publication found with id : ${req.params.id}`});
});
/*
Route /publications/book
Description Get Specific publications based on a book
Access PUBLIC
Parameter isbn
Methods GET
*/
booky.get("/publications/book/:isbn", (req, res)=>{
const getSpecificPublication = database.publication.filter(
(publication) => publication.books.includes(req.params.isbn)
);
if(getSpecificPublication.length !== 0){
return res.json({publications : getSpecificPublication});
}
return res.json({error : `No Publication found based on Book with ISBN : ${req.params.isbn}`});
});
//service listening on port 3000
booky.listen(3000, ()=>{
console.log("Server is UP and RUNNING");
});