-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
277 lines (212 loc) · 7.02 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
const {initializeDatabase} = require("./db/db.connect")
const fs = require("fs");
const Books = require("./models/books.models");
const express = require("express");
const { error } = require("console");
const { json } = require("stream/consumers");
const app = express();
initializeDatabase();
//const jsonData = fs.readFileSync("books.json", "utf-8");
//const booksData = JSON.parse(jsonData)
app.use(express.json());
function seedData(){
try {
for(bookData of booksData ){
const newBook = new Books({
title: bookData.title,
author: bookData.author,
publishedYear: bookData.publishedYear,
genre: bookData.genre,
language: bookData.language,
country: bookData.country,
rating: bookData.rating,
summary: bookData.summary,
coverImageUrl: bookData.coverImageUrl
})
//console.log(newBook.title)
newBook.save()
}
} catch (error) {
console.log("Error seeding the data", error)
}
}
//seedData()
// const theBook = {
// "title": "Lean In",
// "author": "Sheryl Sandberg",
// "publishedYear": 2012,
// "genre": ["Non-fiction", "Business"],
// "language": "English",
// "country": "United States",
// "rating": 4.1,
// "summary": "A book about empowering women in the workplace and achieving leadership roles.",
// "coverImageUrl": "https://example.com/lean_in.jpg"
// };
async function createBook(theBook){
try {
const book = new Books(theBook)
const saveBook = await book.save()
//console.log("New Book data:" , saveBook)
return saveBook
} catch (error) {
throw error
}
}
//createBook(theBook)
app.post("/books", async (req, res) => {
try {
// console.log("New Book data:" , saveBook)
//console.log(req.body)
const letsSaveBook = await createBook(req.body)
//console.log(req.body)
res.status(200).json({message: "Book added successfully.",theBook: letsSaveBook})
} catch (error) {
res.status(500).json({error:"Failed to fetch books", error})
}
} )
async function toGetAllBooks() {
try {
const allTheBooks = await Books.find()
return allTheBooks
} catch (error) {
throw error
}
}
app.get("/", async(req, res) => {
try {
const theActualAllBooks = await toGetAllBooks()
if(theActualAllBooks.length !=0){
res.status(200).json({message: "All the Books", allBooks : theActualAllBooks })
}else{
res.status(404).json({error: "No book found."})
}
} catch (error) {
res.status(500).json({error: "Failed to fetch the books", error})
}
})
async function toGetBookDetailsByName(theTitle) {
try {
const theBookByTitle = await Books.findOne({title: theTitle} )
return theBookByTitle
} catch (error) {
console.log(error)
}
}
app.get("/books/:title", async(req, res) => {
try {
const finalBooksByTitle = await(toGetBookDetailsByName(req.params.title))
if(finalBooksByTitle){
res.status(200).json(finalBooksByTitle)
}else{
res.status(404).json({error: "No book found."})
}
} catch (error) {
res.status(500).json({error: "Failed to Fetch Books", error})
}
})
async function toGetBooksByAuther(theAuther) {
try {
const booksByAuther = await Books.findOne({author: theAuther })
return booksByAuther
} catch (error) {
console.log(error)
}
}
app.get("/books/author/:author", async(req, res) => {
try {
const actualBookByAuther = await toGetBooksByAuther(req.params.author)
if(actualBookByAuther){
res.status(200).json(actualBookByAuther)
}else{
res.status(404).json({error: "Book not found.", error})
}
} catch (error) {
res.status(500).json({error: "Failed to fetch books."})
}
})
async function toGetBookReleasedInYear(releaseYear) {
try {
const theBookReleasedInYear = await Books.findOne({publishedYear: releaseYear })
return theBookReleasedInYear
} catch (error) {
console.log(error)
}
}
app.get("/books/publishedYear/:thepublishedYear", async(req, res) => {
try {
const theActualBookReleasedInYear = await toGetBookReleasedInYear(req.params.thepublishedYear)
if(theActualBookReleasedInYear){
res.status(202).json(theActualBookReleasedInYear)
}else{
res.status(404).json({error: "Book not found."})
}
} catch (error) {
res.status(500).json({error: "Failed to fetch Books", error})
}
})
async function toUpdateRating(theBookId, dataToUpdate) {
try {
const perticularBookById = Books.findByIdAndUpdate(theBookId, dataToUpdate, {new: true} )
return perticularBookById
} catch (error) {
console.log(error)
}
}
app.post("/books/:thePerticularId", async(req, res) => {
try {
const letsUpdatePerticularIdRating = await toUpdateRating(req.params.thePerticularId, req.body)
if(letsUpdatePerticularIdRating){
res.status(200).json(letsUpdatePerticularIdRating)
}else{
res.status(404).json({error:"Book not found."})
}
}catch{
res.status(500).json({error: "Failed to update rating."})
}
})
async function toUpdateBooks(theBookTitle, updationData) {
try {
const updatedBook = await Books.findOneAndUpdate({title: theBookTitle}, updationData , {new : true})
return updatedBook
} catch (error) {
console.log(error)
}
}
app.post("/books/title/:perticularTitle", async(req, res) => {
try {
const actualUpdatedBookData = await toUpdateBooks(req.params.perticularTitle, req.body)
//console.log(actualUpdatedBookData)
// console.log(JSON.parse(req.params.perticularTitle))
if(actualUpdatedBookData){
res.status(200).json(actualUpdatedBookData)
}else{
res.status(404).json({error: "Book not found."})
}
} catch (error) {
res.status(500).json({error: "Failed to fetch books", error})
}
})
async function toDeleteTheBook(theBookId) {
try {
const perticularBookById = await Books.findByIdAndDelete(theBookId)
return perticularBookById
} catch (error) {
console.log(error)
}
}
app.delete("/books/:bookId", async(req, res) => {
try {
const actualToDeleteBook = await toDeleteTheBook(req.params.bookId)
if(actualToDeleteBook){
res.status(202).json({message: "Book deleted succesfully."})
}else{
res.status(404).json({error: "Book not found."})
}
} catch (error) {
res.status(500).json({error: "Failed to delete Book"})
}
})
const PORT = 3000
app.listen(PORT, () => {
console.log("Server is running on port", PORT);
});