Skip to content

Commit

Permalink
Added dummy microservice files
Browse files Browse the repository at this point in the history
  • Loading branch information
nmczormick authored Dec 13, 2019
1 parent fb647f7 commit 2393abd
Show file tree
Hide file tree
Showing 15 changed files with 4,441 additions and 0 deletions.
21 changes: 21 additions & 0 deletions books/Book.js
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
}
})

7 changes: 7 additions & 0 deletions books/Dockerfile
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"]
89 changes: 89 additions & 0 deletions books/books.js
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 ...');
})
Loading

0 comments on commit 2393abd

Please sign in to comment.