-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
296 lines (232 loc) · 9.75 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
const db = require('./database/index.js');
require('dotenv').config()
const BookModel = require('./database/books');
const AuthorModel = require('./database/authors');
const PublicationModel = require('./database/publications');
// const mongoose = require('mongoose');
const express = require('express');
const { MongoClient } = require('mongodb');
const app = express();
app.use(express.json());
//Import the mongoose module
var mongoose = require('mongoose');
//Set up default mongoose connection
var mongoDB = process.env.MONGODB_URI;
mongoose.connect(mongoDB, {useNewUrlParser: true, useUnifiedTopology: true}).then(()=>console.log("CONNECTION ESTABLISHED"));
// const uri = "mongodb+srv://kumaran_bk:[email protected]/book-company?retryWrites=true&w=majority";
// const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
// client.connect(err => {
// const bcollection = client.db("book-company").collection("books").findOne({ISBN : "12345ONE"});
// bcollection.then((data) => console.log(data)).catch((err) => console.log(err));
// });
// client.close();
// async function listDatabases(client){
// databaseList = await client.db().admin().listDatabases();
// console.log("THE DATABASES ARE");
// databaseList.databases.forEach(db=> console.log(db.name));
// }
// async function main() {
// const uri = "mongodb+srv://kumaran_bk:[email protected]/book-company?retryWrites=true&w=majority";
// const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
// try {
// await client.connect();
// const results = await client.db("book-company").collection("books").findOne();
// console.log(results);
// // await listDatabases(client);
// }
// catch(err) {
// console.log(err);
// }
// finally {
// await client.close();
// }
// }
// main();
// http://localhost:3000/
app.get("/", (req, res) => {
return res.json({"WELCOME": `to my Backend Software for the Book Company`});
});
// http://localhost:3000/
app.get('/books', async (req, res) => {
const getAllBooks = await BookModel.find();
return res.json(getAllBooks);
// res.json(getAllBooks);
// res.send(getAllBooks);
});
// http://localhost:3000/book-isbn/12345TWO
app.get("/book-isbn/:isbn", async (req, res) => {
// console.log(req.params);
// const isbn = req.params.isbn; // We can also use as like this
const { isbn } = req.params;
// console.log(isbn);
const getSpecificBook = await BookModel.findOne( {ISBN: isbn} );
// console.log(getSpecificBook);
// console.log(getSpecificBook.length);
if (getSpecificBook=== null) {
return res.json({ "error": `No Book found for the ISBN of ${isbn}` });
}
return res.json(getSpecificBook);
});
// http://localhost:3000/book-category/programming
app.get("/book-category/:category", async (req, res) => {
// console.log(req.params);
const { category } = req.params;
// console.log(category);
const getSpecificBooks = await BookModel.find( {category: category} );
// console.log(getSpecificBook);
// console.log(getSpecificBook.length);
if (getSpecificBooks.length === 0) {
return res.json({ "error": `No Books found for the category of ${category}` });
}
return res.json(getSpecificBooks);
});
// http://localhost:3000/authors
app.get("/authors", async (req, res) => {
// console.log(req.body);
// console.log(req.params);
const getAllAuthors = await AuthorModel.find();
return res.json(getAllAuthors);
});
// http://localhost:3000/author-id/1
app.get("/author-id/:id", async (req, res) => {
// console.log(req.body);
// console.log(req.params);
const { id } = req.params;
const getSpecificAuthor = await AuthorModel.findOne( {id: id} );
// console.log(getSpecificBook);
// console.log(getSpecificBook.length);
if (getSpecificAuthor === null) {
return res.json({ "error": `No Author found for the Id of ${id}` });
}
return res.json(getSpecificAuthor);
});
// http://localhost:3000/author-isbn/12345TWO
app.get("/author-isbn/:isbn", async (req, res) => {
// console.log(req.body);
// console.log(req.params);
const { isbn } = req.params;
const getSpecificAuthor = await AuthorModel.findOne( {ISBN: isbn} );
if (getSpecificAuthor === null) {
return res.json({ "error": `No Author found for the ISBN of ${isbn}` });
}
return res.json(getSpecificAuthor);
});
// http://localhost:3000/publications
app.get("/publications", async (req, res) => {
// console.log(req.body);
// console.log(req.params);
const getAllPublications = await PublicationModel.find();
return res.json(getAllPublications);
});
// http://localhost:3000/publication-isbn/12345TWO
app.get("/publication-isbn/:isbn", async (req, res) => {
// console.log(req.body);
// console.log(req.params);
const { isbn } = req.params;
// console.log(isbn);
const getSpecificPublication = await PublicationModel.findOne( {ISBN: isbn} );
if (getSpecificPublication === null) {
return res.json({ "error": `No Publication found for the ISBN of ${isbn}` });
}
return res.json(getSpecificPublication);
});
// http://localhost:3000/book
app.post("/book", async (req, res) => {
// console.log(req.body);
// console.log(req.params);
const addNewBook = await BookModel.create(req.body);
return res.json({ bookAdded: addNewBook, message: "Book added successfully!" });
});
// http://localhost:3000/author
app.post("/author", async (req, res) => {
// console.log(req.body);
// console.log(req.params);
const addNewAuthor = await AuthorModel.create(req.body);
return res.json({ AuthorAdded: addNewAuthor, message: "Author added successfully!" });
});
// http://localhost:3000/publication
app.post("/publication", async (req, res) => {
// console.log(req.body);
// console.log(req.params);
const addNewPublication = await PublicationModel.create(req.body);
return res.json({ PublicationAdded: addNewPublication, message: "Publication added successfully!" });
});
// http://localhost:3000/book-update/12345TWO
app.put("/book-update/:isbn", async (req, res) => {
// console.log(req.body);
// console.log(req.params);
const { isbn } = req.params;
const updateBook = await BookModel.findOneAndUpdate( { ISBN : isbn }, req.body, { new : true } );
return res.json({ bookupdated: updateBook, message: "Successfully Book Updated!" });
});
// http://localhost:3000/author-update/1
app.put("/author-update/:id", async (req, res) => {
// console.log(req.body);
// console.log(req.params);
const { id } = req.params;
const updateAuthor = await AuthorModel.findOneAndUpdate( { id : id }, req.body, { new : true } );
return res.json({ Authorupdated: updateAuthor, message: "Successfully Author Updated!" });
});
// http://localhost:3000/publication-update/1
app.put("/publication-update/:id", async (req, res) => {
// console.log(req.body);
// console.log(req.params);
const { id } = req.params;
const updatePublication = await PublicationModel.findOneAndUpdate( { id : id }, req.body, { new : true } );
return res.json({ Publicationupdated: updatePublication, message: "Successfully Publication Updated!" });
});
// http://localhost:3000/book-delete/12345TWO
app.delete("/book-delete/:isbn", async (req, res) => {
// console.log(req.body);
// console.log(req.params);
const { isbn } = req.params;
const deleteBook = await BookModel.deleteOne( { ISBN : isbn });
return res.json({ bookdeleted: deleteBook, message: "Successfully Book deleted!" });
});
// http://localhost:3000/author-delete/1
app.delete("/author-delete/:id", async (req, res) => {
// console.log(req.body);
// console.log(req.params);
const { id } = req.params;
const deleteAuthor = await AuthorModel.deleteOne( { id : id });
return res.json({ Authordeleted: deleteAuthor, message: "Successfully Author deleted!" });
});
// http://localhost:3000/publication-delete/1
app.delete("/publication-delete/:id", async (req, res) => {
// console.log(req.body);
// console.log(req.params);
const { id } = req.params;
const deletePublication = await PublicationModel.deleteOne( { id : id });
return res.json({ Publicationdeleted: deletePublication, message: "Successfully Publication deleted!" });
});
// http://localhost:3000/book-author-delete/12345Three/1
app.delete("/book-author-delete/:isbn/:id", async (req, res) => {
// console.log(req.params);
const {isbn, id} = req.params;
let getSpecificBook = await BookModel.findOne({ISBN: isbn});
if(getSpecificBook===null) {
return res.json({"error": `No Book found for the ISBN of ${isbn}`});
}
else {
getSpecificBook.authors.remove(id);
const updateBook = await BookModel.findOneAndUpdate({ISBN: isbn}, getSpecificBook, {new: true});
return res.json( {bookUpdated: updateBook, message: "Author was Deleted from the Book !!!"} );
}
});
// http://localhost:3000/author-book-delete/1/12345ONE
app.delete("/author-book-delete/:id/:isbn", async (req, res) => {
// console.log(req.params);
const {isbn, id} = req.params;
let getSpecificAuthor = await AuthorModel.findOne({id: id});
if(getSpecificAuthor===null) {
return res.json({"error": `No Author found for the id of ${id}`});
}
else {
getSpecificAuthor.books.remove(isbn);
const updateAuthor = await AuthorModel.findOneAndUpdate({id: id}, getSpecificAuthor, {new: true});
return res.json( {bookUpdated: updateAuthor, message: "Book was Deleted from the Book !!!"} );
}
});
app.listen(3000, () => {
console.log("My Express APP is running at localhost:3000");
})