-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredisDao.js
80 lines (71 loc) · 2.41 KB
/
redisDao.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
var express = require('express');
var router = express.Router();
var redis = require('redis');
var client = redis.createClient();
//REST API V2 calls go here.
router.get('/api/v2/entries.json', function(req, res) {
client.keys('*', function (err, keys) {
if (err) throw err;
var rows = [];
var setStatus = function () {
res.status(200).json(rows);
};
keys.forEach(function(key, index) {
client.hgetall(key, function (err, row) {
if (err) throw err;
rows.push(row);
if (index == keys.length - 1)
setStatus(); // Don't set our status and return until the rows have been populated
});
});
if (keys.length == 0)
setStatus(); // Set our status if there are no keys
});
});
// Create
router.post('/api/v2/entries.json', function(req, res){
// Store new entry and return id.
// {"subject":"Something else","contents":"This is the contents for 'Something else'"}
var id = Date.now().toString();
var subject = req.body.subject;
var contents = req.body.contents;
var jsonObj = {'_id': id, 'subject': subject, 'contents': contents};
console.log(`created: ${id}, ${subject}, ${contents}`);
client.hmset(id, jsonObj, function(err, result){
if(err) throw err;
res.status(201).json(id);
});
});
// Read
router.get('/api/v2/entries/:id.json', function(req, res){
var id = req.params.id;
console.log(`select id, subject, contents from entries_table where id = ${id}`);
client.hgetall(id, function(err, result){
if(err) throw err;
res.status(200).json(result);
});
});
// Update
router.put('/api/v2/entries/:id.json', function(req, res){
var id = req.params.id;
var subject = req.body.subject;
var contents = req.body.contents;
var jsonObj = {'_id': id, 'subject': subject, 'contents': contents};
console.log(`updated: ${id}, ${subject}, ${contents}`);
client.hmset(id, jsonObj, function(err, result){
if(err) throw err;
});
console.log('Update called');
res.sendStatus(204);
});
// Delete
router.delete('/api/v2/entries/:id', function(req, res){
var id = req.params.id;
client.del(id, function(err, result){
if(err) throw err;
});
console.log('Delete called');
res.sendStatus(204);
});
module.exports = router;
// END REAST API V2 CALLS.