-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.js
110 lines (99 loc) · 3.48 KB
/
controller.js
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
103
104
105
106
107
108
109
'use strict';
var promise = require('bluebird');
var options = {
promiseLib: promise
};
var pgp = require('pg-promise')(options);
var connectionString = 'postgres://nyuyiemzmlswco:7fdc0f4044583567f588f8283d44b55c6755cfa81ad8ef988013859d49f83cfe@ec2-46-137-97-169.eu-west-1.compute.amazonaws.com:5432/d674efht8ffv86'; // startrek is an example database name
var db = pgp(connectionString);
exports.getUserDetails = function(req, res, next) {
console.log("User Details: " + req.body);
db.one('SELECT * FROM users WHERE id = ${id}', req.body)
.then(function(data) {
res.status(200)
.json(data);
})
.catch(function (err) {
res.status(500).json(err);
});
}
exports.getItemDetails = function(req, res, next) {
console.log("Item Details: " + req.body);
db.one('SELECT * FROM item WHERE barcode = ${barcode}', req.body)
.then(function (data) {
res.status(200)
.json(data);
})
.catch(function (err) {
res.status(500).json(err);
});
}
exports.createNewItem = function(req, res, next) {
console.log("Create Item: " + req.body);
db.none('INSERT INTO item(barcode, calories, energy, fat, saturates, carbohydrates, sugars, protein, salt)' +
' values(${barcode}, ${calories}, ${energy}, ${fat}, ${saturates}, ${carbohydrates}, ${sugars}, ${protein}, ${salt})',
req.body)
.then(function () {
res.status(200)
.json({
status: 'success',
message: 'Created an item'
});
})
.catch(function (err) {
res.status(500).json(err);
});
}
exports.createNewUser = function(req, res, next) {
console.log("Create User: " + req.body);
db.none('INSERT INTO users(id, email, name, calories_g, energy_g, fat_g, saturates_g, carbohydrates_g, sugars_g, protein_g, salt_g)' +
' values (${id}, ${email}, ${name}, ${calories_g}, ${energy_g}, ${fat_g}, ${saturates_g}, ${carbohydrates_g}, ${sugars_g}, ${protein_g}, ${salt_g})',
req.body)
.then(function () {
res.status(200)
.json({
status: 'success',
message: 'Created a user'
});
})
.catch(function (err) {
res.status(500).json(err);
});
}
exports.consumeItem = function(req, res, next) {
console.log("Consume Item: " + req.body);
let date = new Date();
db.none('INSERT INTO consumed_item(id, barcode, date) ' +
' values(${id}, ${barcode}, \'' + date.toISOString() + '\')',
req.body)
.then(function () {
res.status(200)
.json({
status: 'success',
message: 'Added to user\'s database'
});
})
.catch(function (err) {
res.status(500).json(err);
});
}
exports.getDailyConsumption = function(req, res, next) {
console.log("Daily Consumption: " + req.body);
let date = new Date();
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
let date2 = new Date();
date2.setHours(23);
date2.setMinutes(59);
date2.setSeconds(59);
db.one('SELECT SUM(calories) AS calories, SUM(energy) AS energy, SUM(fat) AS fat, SUM(saturates) AS saturates, SUM(carbohydrates) AS carbohydrates, SUM(sugars) AS sugars, SUM(protein) AS protein, SUM(salt) AS salt FROM ((consumed_item NATURAL JOIN item) NATURAL JOIN users) WHERE id = ${id} AND date < \'' + date2.toISOString() + '\' AND date > \'' + date.toISOString() + '\';' ,
req.body)
.then(function (data) {
res.status(200)
.json(data);
})
.catch(function (err) {
res.status(500).json(err);
});
}