Git Repository Public Repository

YouAndWeb_TwoToc

URLs

Copy to Clipboard
 
a98e8d4c8be3c9266abcdd007f47a6b1c3ed9599
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
 * Using Rails-like standard naming convention for endpoints.
 * GET     /api/test              ->  index
 * POST    /api/test              ->  create
 * GET     /api/test/:id          ->  show
 * PUT     /api/test/:id          ->  update
 * DELETE  /api/test/:id          ->  destroy
 */

'use strict';

var _ = require('lodash');
var Message = require('./message.model');

function handleError(res, statusCode) {
  statusCode = statusCode || 500;
  return function(err) {
    res.status(statusCode).send(err);
  };
}

function responseWithResult(res, statusCode) {
  statusCode = statusCode || 200;
  return function(entity) {
    if (entity) {
      res.status(statusCode).json(entity);
    }
  };
}

function handleEntityNotFound(res) {
  return function(entity) {
    if (!entity) {
      res.status(404).end();
      return null;
    }
    return entity;
  };
}

function saveUpdates(updates) {
  return function(entity) {
    var updated = _.merge(entity, updates);
    return updated.saveAsync()
      .spread(function(updated) {
        return updated;
      });
  };
}

function removeEntity(res) {
  return function(entity) {
    if (entity) {
      return entity.removeAsync()
        .then(function() {
          res.status(204).end();
        });
    }
  };
}

// Gets a list of Messages
exports.index = function(req, res) {
  Message.findAsync()
    .then(responseWithResult(res))
    .catch(handleError(res));
};

// Gets a single Message from the DB
exports.show = function(req, res) {
  Message.findByIdAsync(req.params.id)
    .then(handleEntityNotFound(res))
    .then(responseWithResult(res))
    .catch(handleError(res));
};

// Creates a new Message in the DB
exports.create = function(req, res) {
  Message.createAsync(req.body)
    .then(responseWithResult(res, 201))
    .catch(handleError(res));
};

// Updates an existing Message in the DB
exports.update = function(req, res) {
  if (req.body._id) {
    delete req.body._id;
  }
  Message.findByIdAsync(req.params.id)
    .then(handleEntityNotFound(res))
    .then(saveUpdates(req.body))
    .then(responseWithResult(res))
    .catch(handleError(res));
};

// Deletes a Message from the DB
exports.destroy = function(req, res) {
  Message.findByIdAsync(req.params.id)
    .then(handleEntityNotFound(res))
    .then(removeEntity(res))
    .catch(handleError(res));
};

Commits for YouAndWeb_TwoToc.fr-KzWVa7/twotoc/server/api/message/message.controller.js

Diff revisions: vs.
Revision Author Commited Message
a98e8d ... FSinnona picture FSinnona Thu 26 Nov, 2015 13:26:45 +0000

Creazione organizza