Você está na página 1de 42

http://www.luiztools.com.

br

Node.js e
MongoDB
CRONOGRAMA
MongoDB - Movies
Server - Movies
Api - Movies
Index - Movies
MongoDB -
Movies
MongoDB - Movies

Repositório

nano /cinema-microservice/movies-
service/src/repository/repository.js

nano /cinema-microservice/movies-
service/src/repository/repository.test.js
MongoDB - Movies

repository.js

const mongodb = require("../config/mongodb");

function getAllMovies(callback){
mongodb.connect((err, db) => {
db.collection("movies").find().toArray(callback);
})
}
MongoDB - Movies

repository.js

function getMovieById(id, callback){


mongodb.connect((err, db) => {
db.collection("movies").findOne({_id:
require("mongodb").ObjectId(id)}, callback);
});
}
MongoDB - Movies

repository.js

function getMoviePremiers(callback){
var monthAgo = new Date();
monthAgo.setMonth(monthAgo.getMonth() - 1);
monthAgo.setHours(0, 0, 0);
monthAgo.setMilliseconds(0);

mongodb.connect((err, db) => {


db.collection("movies").find({ dataLancamento: {
$gte: monthAgo } }).toArray(callback);
});
}
MongoDB - Movies

repository.js

function disconnect(){
return mongodb.disconnect();
}

module.exports = { getAllMovies, getMovieById,


getMoviePremiers, disconnect }
MongoDB - Movies

repository.test.js

const test = require('tape');


const repository = require('./repository');

function runTests(){
var id = null;
//tests here
}

module.exports = { runTests }
MongoDB - Movies

repository.test.js
var id = null;

test('Repository GetAllMovies', (t) => {


repository.getAllMovies((err, movies) => {
if(movies && movies.length > 0) id =
movies[0]._id;

t.assert(!err && movies && movies.length > 0,


"All Movies Returned");
t.end();
});
})
MongoDB - Movies

repository.test.js
test('Repository GetMovieById', (t) => {
if(!id) {
t.assert(false, "Movie by Id Returned");
t.end();
return;
}

repository.getMovieById(id, (err, movie) => {


t.assert(!err && movie, "Movie by Id
Returned");
t.end();
});
})
MongoDB - Movies

repository.test.js
test('Repository GetMoviePremiers', (t) => {
repository.getMoviePremiers((err, movies) => {
t.assert(!err && movies && movies.length > 0,
"Movie Premiers Returned");
t.end();
});
})
MongoDB - Movies

repository.test.js
test('Repository Disconnect', (t) => {
t.assert(repository.disconnect(), "Disconnect
Ok");
t.end();
})
MongoDB - Movies

index.test.js

require("dotenv-safe").load();
require("./config/mongodb.test").runTests();
require("./repository/repository.test").runTests();
MongoDB - Movies
Server -
Movies
Server - Movies

server.js

nano /cinema-microservice/movies-
service/src/server/server.js

const express = require('express');


const morgan = require('morgan');
const helmet = require('helmet');
var server = null;
Server - Movies

server.js
function start(api, repository, callback){
const app = express();
app.use(morgan('dev'));
app.use(helmet());
app.use((err, req, res, next) => {
callback(new Error('Something went wrong!, err:' +
err), null);
res.status(500).send('Something went wrong!');
})

api(app, repository);
server = app.listen(parseInt(process.env.PORT), () =>
callback(null, server));
}
Server - Movies

server.js

function stop(){
if(server) server.close();
return true;
}

module.exports = { start, stop }


Server - Movies

server.test.js
nano /cinema-microservice/movies-
service/src/server/server.test.js
Server - Movies

server.test.js
const test = require('tape');
const server = require('./server');

function apiMock(app, repo){


console.log("do nothing");
}

function runTests(){
//tests here
}

module.exports = { runTests }
Server - Movies

server.test.js
test('Server Start', (t) => {
server.start(apiMock, null, (err, srv) => {
t.assert(!err && srv, "Server started");
t.end();
});
})

test('Server Stop', (t) => {


t.assert(server.stop(), "Server stopped");
t.end();
})
Tape
API -
Movies
API - Movies

movies.js

nano /cinema-microservice/movies-
service/src/api/movies.js
API - Movies

movies.js

module.exports = (app, repository) => {

app.get('/movies', (req, res, next) => {


repository.getAllMovies((err, movies) => {
if(err) return next(err);
res.json(movies);
});
})
API - Movies

movies.js

app.get('/movies/premieres', (req, res, next) => {


repository.getMoviePremiers((err, movies) => {
if(err) return next(err);
res.json(movies)
});
})
API - Movies

movies.js

app.get('/movies/:id', (req, res, next) => {


repository.getMovieById(req.params.id, (err, movie)
=> {
if(err) return next(err);
res.json(movie)
});
})
}
Server - Movies

movies.test.js
npm install supertest

nano /cinema-microservice/movies-
service/src/api/movies.test.js
Server - Movies

movies.test.js
npm install supertest

nano /cinema-microservice/movies-
service/src/api/movies.test.js

const test = require('tape');


const supertest = require('supertest');
const movies = require('./movies');
const server = require("../server/server");
const repository = require("../repository/repository");
Server - Movies

movies.test.js
function runTests(){
var app = null;
server.start(movies, repository, (err, app) => {

//tests here

server.stop();
})
}

module.exports = { runTests }
Server - Movies

movies.test.js
var id = null;
test('GET /movies', (t) => {
supertest(app)
.get('/movies')
.expect('Content-Type', /json/)
.expect(200)
.end((err, res) =>{
if(res.body && res.body.length > 0)
id = res.body[0]._id;
t.assert(res.body && res.body.length > 0,
"All Movies returned")
t.end()
})
})
Server - Movies

movies.test.js
test('GET /movies/:id', (t) => {
if(!id) {
t.assert(false, "Movie by Id Returned");
t.end();
return;
}
supertest(app)
.get('/movies/' + id)
.expect('Content-Type', /json/)
.expect(200)
.end((err, res) =>{
t.assert(res.body, "Movies By Idreturned")
t.end()
})
})
Server - Movies

movies.test.js
test('GET /movies/premieres', (t) => {
supertest(app)
.get('/movies/premieres')
.expect('Content-Type', /json/)
.expect(200)
.end((err, res) =>{
t.assert(res.body && res.body.length
> 0, "Premiere Movies returned")
t.end()
})
})
Server - Movies
Index -
Movies
Index - Movies

index.js
nano /cinema-microservice/movies-service/src/index.js

require("dotenv-safe").load();
const movies = require('./api/movies');
const server = require("./server/server");
const repository = require("./repository/repository");

server.start(movies, repository, (err, app) => {


console.log("just started");
});
Index - Movies
Dúvidas?
Exercícios
1 Adicione busca por nome no microservice

2 Adicione paginação no getAllMovies

3 Adicione o restante do CRUD ao microservice


Obrigado!