-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
106 lines (93 loc) · 2.69 KB
/
app.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
var express = require('express');
var redis = require('redis');
var bodyParser = require('body-parser');
var engine = require('ejs-locals');
var path = require('path');
var q = require('q');
var db = redis.createClient();
var app = express();
app.engine('ejs', engine);
app.set('views', __dirname + '/templates');
app.set('view engine', 'ejs');
app.set('template_engine', 'ejs');
app.use(express.static('public'));
// app.use(express.static('assets'));
// app.use(express.static('bower_components'));
// app.use(express.static('javascript'));
app.use(bodyParser.json());
app.get('/', function(req, res) {
res.render('index');
})
// GET
// get name at cube no
app.get('/cube/:id', function(req, res, next) {
db.hget(String(req.params.id), 'name', function(err, obj) {
res.send(obj);
});
});
// get cube no from name
app.get('/cube', function(req, res, next) {
db.get(req.query.name, function(err, reply) {
res.send(reply);
});
});
// get names and cube nos
app.get('/names', function(req, res, next) {
db.keys('*', function(err, reply) {
res.send(reply);
});
});
// returns hash of cube no and name
app.get('/chart', function(req, res, next) {
var chart = {};
var promises = [];
db.keys('*', function(err, reply) {
reply.forEach(function(key, i) {
if (!isNaN(key)) {
// promises.push(q.ninvoke(db, 'hgetall', key));
promises.push(q.ninvoke(db, 'hgetall', key).then(function(data) {
// db.hgetall(key, function(err, reply) {
chart[key] = data;
// });
}));
}
});
q.all(promises).spread(function(results) {
res.send(chart);
});
});
});
// returns hash of cube no and name at some floor
app.get('/chart/:floor', function(req, res, next) {
//TODO
});
// ADD/UPDATE
//add seat no and name
app.post('/cube', function(req, res, next) {
db.hset(String(req.body.seatNo), 'name', req.body.name, redis.print);
// db.hset(String(req.body.seatNo), req.body.floorNo, redis.print); // floor?
db.set(req.body.name, String(req.body.seatNo), redis.print);
db.hgetall(req.body.seatNo, function(err, obj) {
res.send(obj);
});
});
// update seat no and name
app.put('/cube', function(req, res, next) {
db.hset(String(req.body.seatNo), 'name', req.body.name, redis.print);
// db.hset(String(req.body.seatNo), req.body.floorNo, redis.print); // floor?
db.set(req.body.name, String(req.body.seatNo), redis.print);
db.hgetall(req.body.seatNo, function(err, obj) {
res.send(obj);
});
});
// DELETE
app.delete('/cube', function(req, res, next) {
var seatNo = db.get(req.body.name);
db.hset(String(req.body.seatNo), 'name', "");
db.del(req.body.name);
db.get(req.body.name, function(err, reply) {
res.send(reply);
});
});
app.listen(3000);
console.log('listening on port 3000...');