-
-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb647f7
commit 2393abd
Showing
15 changed files
with
4,441 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const mongoose = require('mongoose') | ||
|
||
mongoose.model("Book", { | ||
title: { | ||
type: String, | ||
required: true | ||
}, | ||
author: { | ||
type: String, | ||
required: true | ||
}, | ||
numberPages: { | ||
type: Number, | ||
required: false | ||
}, | ||
publisher: { | ||
type: String, | ||
required: false | ||
} | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FROM node:10 | ||
WORKDIR /bookContainer/ | ||
COPY package*.json ./ | ||
RUN npm install | ||
COPY . . | ||
EXPOSE 4545 | ||
CMD ["node", "books.js"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
const express = require("express"); | ||
const app = express(); | ||
const mongoose = require('mongoose'); | ||
const bodyParser = require('body-parser'); | ||
app.use(bodyParser.json()); | ||
const path = require('path'); | ||
|
||
const mw = require("../mwMongo.js"); | ||
app.use("/", mw.microCom(path.basename(__filename))); | ||
|
||
//Required to get rid of deprecation warnings | ||
mongoose.set("useUnifiedTopology", true); | ||
mongoose.set("useNewUrlParser", true); | ||
|
||
require('./Book') | ||
const Book = mongoose.model("Book") | ||
|
||
//Connects our bookservice db to cloud db | ||
mongoose.connect("mongodb+srv://numanzor:Nu121692.@microservice-tutorial-hq75f.mongodb.net/booksservice", () => { | ||
console.log("Books database is connected...") | ||
}); | ||
|
||
app.get('/', (req,res,next) => { | ||
return res.status(200).send("This is our main endpoint!"); | ||
}) | ||
|
||
|
||
//Create functionality | ||
app.post('/book', (req,res,next) => { | ||
const newBook = { | ||
title: req.body.title, | ||
author: req.body.author, | ||
numberPages: req.body.numberPages, | ||
publisher: req.body.publisher | ||
} | ||
//create a new Book with the above attributes | ||
const book = new Book(newBook) | ||
|
||
//save our book to collection | ||
|
||
book.save().then(() => { | ||
res.send("Book successfully saved to the database"); | ||
next(); | ||
}) | ||
.catch(err => { | ||
Promise.reject(err); | ||
}); | ||
}) | ||
|
||
|
||
app.get('/books', (req, res,next) => { | ||
Book.find().then((books) => { | ||
res.json(books) | ||
next(); | ||
}).catch((err) => { | ||
Promise.reject(err) | ||
}) | ||
}) | ||
|
||
//Get a book by its id | ||
app.get('/book/:id', (req,res,next) => { | ||
Book.findById(req.params.id) | ||
.then((book) => { | ||
if (book) { | ||
res.json(book) | ||
next(); | ||
} else { | ||
Promise.reject(err); | ||
} | ||
}) | ||
}) | ||
|
||
|
||
app.delete('/book/:id', (req,res,next) => { | ||
Book.findOneAndRemove(req.params.id) | ||
.then(() => { | ||
res.send('Book successfully deleted') | ||
next(); | ||
}).catch((err) => { | ||
if (err) { | ||
throw err; | ||
} | ||
}) | ||
}) | ||
|
||
//Open an express server | ||
app.listen(4545, () => { | ||
console.log('Book server running on port 4545 ...'); | ||
}) |
Oops, something went wrong.