Skip to content

Instantly share code, notes, and snippets.

@crizstian
Last active February 6, 2017 00:31
Show Gist options
  • Save crizstian/ea7f112a92243b6ff64027dd54e0b59a to your computer and use it in GitHub Desktop.
Save crizstian/ea7f112a92243b6ff64027dd54e0b59a to your computer and use it in GitHub Desktop.
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')
Promise.all([
validate(req.body.user, 'user'),
validate(req.body.booking, 'booking')
])
.then(([user, booking]) => {
const payment = {
userName: user.name + ' ' + user.lastName,
currency: 'mxn',
number: user.creditCard.number,
cvc: user.creditCard.cvc,
exp_month: user.creditCard.exp_month,
exp_year: user.creditCard.exp_year,
amount: booking.amount,
description: `
Tickect(s) for movie ${booking.movie},
with seat(s) ${booking.seats.toString()}
at time ${booking.schedule}`
}
return Promise.all([
// we call the payment service
paymentService(payment),
Promise.resolve(user),
Promise.resolve(booking)
])
})
.then(([paid, user, booking]) => {
return Promise.all([
repo.makeBooking(user, booking),
repo.generateTicket(paid, booking)
])
})
.then(([booking, ticket]) => {
// we call the notification service
notificationService({booking, ticket})
res.status(status.OK).json(ticket)
})
.catch(next)
})
app.get('/booking/verify/:orderId', (req, res, next) => {
repo.getOrderById(req.params.orderId)
.then(order => {
res.status(status.OK).json(order)
})
.catch(next)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment