2 * Using Rails-like standard naming convention for endpoints.
3 * GET /api/shows -> index
4 * POST /api/shows -> create
5 * GET /api/shows/:id -> show
6 * PUT /api/shows/:id -> update
7 * DELETE /api/shows/:id -> destroy
12 var _ = require('lodash');
13 var Show = require('./show.model');
14 var User = require('../user/user.model');
15 // var Promise_ = require('promise');
17 function handleError(res, statusCode) {
18 statusCode = statusCode || 500;
19 return function(err) {
20 res.status(statusCode).send(err);
24 function responseWithResult(res, dig, statusCode) {
25 statusCode = statusCode || 200;
27 return function(entity) {
30 entity.forEach(function(obj, index) {
31 promise.push(new Promise(function(resolve, reject) {
32 User.findById(obj.userId, function(err, userObj) {
38 Promise.all(promise).then(function(data) {
40 res.status(statusCode).json(data);
43 User.findById(entity.userId, function(err, userObj) {
44 entity.user = userObj;
45 res.status(statusCode).json(entity);
52 function handleEntityNotFound(res) {
53 return function(entity) {
55 res.status(404).end();
62 function saveUpdates(updates) {
63 return function(entity) {
64 var updated = _.merge(entity, updates);
65 return updated.saveAsync()
66 .spread(function(updated) {
72 function removeEntity(res) {
73 return function(entity) {
75 return entity.removeAsync()
77 res.status(204).end();
83 // Gets a list of Shows
84 exports.index = function(req, res) {
85 var limit = req.query.limit;
87 var date = req.query.date;
88 var category = req.query.category;
89 var fulltext = req.query.fulltext;
90 var lat = req.query.lat;
91 var lng = req.query.lng;
99 sq.categoryId = category
104 $maxDistance: maxDistance
108 var words = fulltext.replace(/[.,-\/#!$%\^&\*;:{}=\-_`~()]/g,"").split(/\s+/);
110 for (let word of words) {
111 pattern += "(?=.*\\b" + word + "\\b)";
113 sq.title = new RegExp("^" + pattern + ".*$", 'i');
115 Show.find(sq).lean().limit(limit)
116 .then(responseWithResult(res, true), handleError(res));
117 /*.catch(handleError(res));*/
121 // Gets a single Show from the DB
122 exports.show = function(req, res) {
123 Show.findById(req.params.id).lean()
124 .then(handleEntityNotFound(res))
125 .then(responseWithResult(res), handleError(res));
126 /*.catch(handleError(res));*/
129 // Creates a new Show in the DB
130 exports.create = function(req, res) {
131 Show.createAsync(req.body)
132 .then(responseWithResult(res, 201), handleError(res));
133 /*.catch(handleError(res));*/
136 // Updates an existing Show in the DB
137 exports.update = function(req, res) {
141 Show.findByIdAsync(req.params.id)
142 .then(handleEntityNotFound(res))
143 .then(saveUpdates(req.body))
144 .then(responseWithResult(res))
145 .catch(handleError(res));
148 // Deletes a Show from the DB
149 exports.destroy = function(req, res) {
150 Show.findByIdAsync(req.params.id)
151 .then(handleEntityNotFound(res))
152 .then(removeEntity(res))
153 .catch(handleError(res));