Skip to content

Instantly share code, notes, and snippets.

View crizstian's full-sized avatar

Cristian Ramirez crizstian

View GitHub Profile
@crizstian
crizstian / cinema-catalog-service.raml
Last active January 29, 2017 22:02
Cinema Catalog Service API example
#%RAML 1.0
title: Cinema Catalog Service
version: v1
baseUri: /
uses:
object: types.raml
stack: ../movies-service/api.raml
types:
Cinemas: object.Cinema []
@crizstian
crizstian / cinema-catalog-repo.js
Created January 29, 2017 20:03
Example of retrieving data from mongoDB
// 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)
@crizstian
crizstian / cinema-catalog-service-api.js
Created January 29, 2017 20:16
Example of express routes
'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)
@crizstian
crizstian / cinema-catalog-api-test.js
Created January 29, 2017 20:23
Example of mocha testing
/* 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'
@crizstian
crizstian / dependency-injection-index.js
Created February 1, 2017 19:00
Example of dependency injection
// more code
mediator.on('db.ready', (db) => {
let rep
// here we are making DI to the repository
// we are injecting the database object and the ObjectID object
repository.connect({
db,
ObjectID: config.ObjectID
})
@crizstian
crizstian / booking-service-api.raml
Last active February 17, 2018 11:36
Example of a raml file
#%RAML 1.0
title: Booking Service
version: v1
baseUri: /
types:
Booking:
properties:
city: string
cinema: string
@crizstian
crizstian / booking.model.js
Created February 2, 2017 18:19
Example of schema validation with joi
const bookingSchema = (joi) => ({
bookingSchema: joi.object().keys({
city: joi.string(),
schedule: joi.date().min('now'),
movie: joi.string(),
cinemaRoom: joi.number(),
seats: joi.array().items(joi.string()).single(),
totalAmount: joi.number()
})
})
const joi = require('joi')
const user = require('./user.model')(joi)
const booking = require('./booking.model')(joi)
const ticket = require('./ticket.model')(joi)
const schemas = Object.create({user, booking, ticket})
const schemaValidator = (object, type) => {
return new Promise((resolve, reject) => {
if (!object) {
@crizstian
crizstian / validate-joi-models.spec.js
Last active July 17, 2020 14:39
Example of testing joi validation
/* eslint-env mocha */
const test = require('assert')
const {validate} = require('./')
console.log(Object.getPrototypeOf(validate))
describe('Schemas Validation', () => {
it('can validate a booking object', (done) => {
const now = new Date()
now.setDate(now.getDate() + 1)
@crizstian
crizstian / booking.api.js
Last active February 6, 2017 00:31
Example of API
'use strict'
const status = require('http-status')
module.exports = ({repo}, app) => {
app.post('/booking', (req, res, next) => {
// we grab the dependencies need it for this route
const validate = req.container.resolve('validate')
const paymentService = req.container.resolve('paymentService')
const notificationService = req.container.resolve('notificationService')