0.0.5 | Built motion from commit b4141bc.
[motion.git] / server / api / update / update.controller.js
1 'use strict';
2
3 var _ = require('lodash');
4 var path = require("path");
5 var config = require('../../config/environment');
6
7 var Git = require('simple-git')(path.join(config.root));
8
9 var Update = require('../../models').Update;
10
11 // Get list of updates
12 exports.index = function(req, res) {
13
14   Git
15     .fetch('origin', 'develop', function(err) {
16       if (err) {
17         return handleError(res, err);
18       }
19
20       Git
21         .log(function(err, logs) {
22           if (err) {
23             return handleError(res, err);
24           }
25
26           logs.all = _.take(logs.all, 50);
27           console.log("Latest available tag: %s", logs.latest);
28           return res.status(200).send(logs);
29         });
30     });
31 };
32
33 // Get a single update
34 exports.show = function(req, res) {
35
36 };
37
38 // Get a single update
39 exports.pull = function(req, res) {
40   Git
41     .reset('hard', function(err, update) {
42       console.log('update', update);
43       if (err) {
44         return handleError(res, err);
45       }
46       return res.status(201).send(update);
47     });
48   // Git
49   //   .pull(function(err, update) {
50   //     if (err) {
51   //       return handleError(res, err);
52   //     }
53   //     return res.status(201).send(update);
54   //   });
55 };
56
57 // Get a single update
58 exports.checkout = function(req, res) {
59   console.log('req.params.hash', req.params.hash);
60   Git
61     .checkout(req.params.hash, function(err, update) {
62       if (err) {
63         return handleError(res, err);
64       }
65       return res.status(201).send(update);
66     });
67 };
68
69 // Creates a new update in the DB.
70 exports.create = function(req, res) {
71   Update
72     .create(req.body)
73     .then(function(update) {
74       return res.status(201).send(update);
75     })
76     .catch(function(err) {
77       return handleError(res, err);
78     });
79 };
80
81 // Updates an existing update in the DB.
82 exports.update = function(req, res) {
83   if (req.body.id) {
84     delete req.body.id;
85   }
86   Update
87     .find({
88       where: {
89         id: req.params.id
90       }
91     })
92     .then(function(update) {
93       if (!update) {
94         return res.sendStatus(404);
95       }
96       var updated = _.merge(update, req.body);
97       updated.save()
98         .then(function() {
99           return res.status(200).send(update);
100         })
101         .catch(function(err) {
102           return handleError(res, err);
103         });
104     })
105     .catch(function(err) {
106       return handleError(res, err);
107     });
108 };
109
110 // Deletes a update from the DB.
111 exports.destroy = function(req, res) {
112   Update
113     .find({
114       where: {
115         id: req.params.id
116       }
117     })
118     .then(function(update) {
119       if (!update) {
120         return res.sendStatus(404);
121       }
122       update.destroy()
123         .then(function() {
124           return res.sendStatus(204);
125         })
126         .catch(function(err) {
127           return handleError(res, err);
128         });
129     })
130     .catch(function(err) {
131       return handleError(res, err);
132     });
133 };
134
135 function handleError(res, err) {
136   return res.status(500).send(err);
137 }