Built motion from commit 5e31ea4.|0.0.32
[motion.git] / server / api / ami / ami.controller.js
1 'use strict';
2
3 var _ = require('lodash');
4 var Ami = require('../../models').Ami;
5
6 // Get list of amis
7 exports.index = function(req, res) {
8   Ami
9     .findAll()
10     .then(function(amis) {
11       return res.status(200).send(amis);
12     })
13     .catch(function(err) {
14       return handleError(res, err);
15     });
16 };
17
18 // Get a single ami
19 exports.show = function(req, res) {
20   Ami
21     .findById(req.params.id)
22     .then(function(ami) {
23       if (!ami) {
24         return res.sendStatus(404);
25       }
26       return res.send(ami);
27     })
28     .catch(function(err) {
29       return handleError(res, err);
30     });
31 };
32
33 // Creates a new ami in the DB.
34 exports.create = function(req, res) {
35
36   req.body.data = JSON.stringify(req.body);
37   req.body.UserId = req.user.id;
38
39   require('./ami.ami').action(req.body, function(err, result) {
40     if (err) {
41       handleError(res, err);
42     } else {
43       return res.status(201).send(result);
44     }
45   });
46
47 };
48
49 // Updates an existing ami in the DB.
50 exports.update = function(req, res) {
51   if (req.body.id) {
52     delete req.body.id;
53   }
54   Ami
55     .findById(req.params.id)
56     .then(function(ami) {
57       if (!ami) {
58         return res.sendStatus(404);
59       }
60       var updated = _.merge(ami, req.body);
61       updated.save()
62         .then(function() {
63           return res.status(200).send(ami);
64         })
65         .catch(function(err) {
66           return handleError(res, err);
67         });
68     })
69     .catch(function(err) {
70       return handleError(res, err);
71     });
72 };
73
74 // Deletes a ami from the DB.
75 exports.destroy = function(req, res) {
76   Ami
77     .find({
78       where: {
79         id: req.params.id
80       }
81     })
82     .then(function(ami) {
83       if (!ami) {
84         return res.sendStatus(404);
85       }
86       ami.destroy()
87         .then(function() {
88           return res.sendStatus(204);
89         })
90         .catch(function(err) {
91           return handleError(res, err);
92         });
93     })
94     .catch(function(err) {
95       return handleError(res, err);
96     });
97 };
98
99 function handleError(res, err) {
100   return res.status(500).send(err);
101 }