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
'use strict' | |
const express = require('express') | |
const morgan = require('morgan') | |
const helmet = require('helmet') | |
const movieAPI = require('../api/movies') | |
const start = (options) => { | |
return new Promise((resolve, reject) => { | |
// we need to verify if we have a repository added and a server port | |
if (!options.repo) { |
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
'use strict' | |
const status = require('http-status') | |
module.exports = (app, options) => { | |
const {repo} = options | |
// here we get all the movies | |
app.get('/movies', (req, res, next) => { | |
repo.getAllMovies().then(movies => { | |
res.status(status.OK).json(movies) |
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
const MongoClient = require('mongodb') | |
// here we create the url connection string that the driver needs | |
const getMongoURL = (options) => { | |
const url = options.servers | |
.reduce((prev, cur) => prev + `${cur.ip}:${cur.port},`, 'mongodb://') | |
return `${url.substr(0, url.length - 1)}/${options.db}` | |
} |
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
// simple configuration file | |
// database parameters | |
const dbSettings = { | |
db: process.env.DB || 'movies', | |
user: process.env.DB_USER || 'cristian', | |
pass: process.env.DB_PASS || 'cristianPassword2017', | |
repl: process.env.DB_REPLS || 'rs1', | |
servers: (process.env.DB_SERVERS) ? process.env.DB_SERVERS.split(' ') : [ | |
'192.168.99.100:27017', |
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
'use strict' | |
// we load all the depencies we need | |
const {EventEmitter} = require('events') | |
const server = require('./server/server') | |
const repository = require('./repository/repository') | |
const config = require('./config/') | |
const mediator = new EventEmitter() | |
// verbose logging when we are starting the server | |
console.log('--- Movies Service ---') |
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
// repository.js | |
'use strict' | |
// factory function, that holds an open connection to the db, | |
// and exposes some functions for accessing the data. | |
const repository = (db) => { | |
// since this is the movies-service, we already know | |
// that we are going to query the `movies` collection | |
// in all of our functions. | |
const collection = db.collection('movies') |
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
#%RAML 1.0 | |
title: cinema | |
version: v1 | |
baseUri: / | |
types: | |
Movie: | |
properties: | |
id: string | |
title: string |
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
// more code above | |
const getCinemasByCity = (cityId) => { | |
return new Promise((resolve, reject) => { | |
const cinemas = [] | |
const query = {city_id: cityId} | |
const projection = {_id: 1, name: 1} | |
// example of making a find query to mongoDB, | |
// passign a query and projection objects. | |
const cursor = db.collection('cinemas').find(query, projection) |
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
'use strict' | |
const status = require('http-status') | |
module.exports = (app, options) => { | |
const {repo} = options | |
app.get('/cinemas', (req, res, next) => { | |
repo.getCinemasByCity(req.query.cityId) | |
.then(cinemas => { | |
res.status(status.OK).json(cinemas) |
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
/* eslint-env mocha */ | |
const request = require('supertest') | |
const server = require('../server/server') | |
process.env.NODE = 'test' | |
describe('Movies API', () => { | |
let app = null | |
const testCinemasCity = [{ | |
'_id': '588ac3a02d029a6d15d0b5c4', | |
'name': 'Plaza Morelia' |
OlderNewer