-
Notifications
You must be signed in to change notification settings - Fork 0
/
requestHandlers.js
118 lines (104 loc) · 2.87 KB
/
requestHandlers.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
110
111
112
113
114
115
116
117
118
var client = require("redis").createClient();
var async = require('async');
var qs = require('querystring');
var url= require("url");
var req_url=null;
function addUser(id, email, nombre_com,req_url,response){
if(client.hlen(id)==0){
client.hset(id, "email", email);
client.hset(id, "nombre_com", nombre_com);
client.incr("num_users");
client.rpush("users",id);
response.writeHead(201, {"Content-Type": "text/plain", "Content-Location": req_url + id });
response.write("201 CREATED");
response.end();
}else{
response.writeHead(403, {"Content-Type": "text/plain"});
response.write("403 Forbidden");
response.end();
}
}
function getUser(response,id){
client.hgetall(id,function (err, replies) {
if(replies==null){
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not found");
response.end();
}else{
var contacto = new Object();
contacto.id=id;
contacto.email=replies["email"];
contacto.nombre_completo=replies["nombre_com"];
var repuestaJSON=JSON.stringify(contacto);
response.writeHead(200, {"Content-Type": "text/plain"});
response.write(repuestaJSON);
response.end();
}
});
}
function getUsers(from, to, response){
var id;
if (from==null) from=0;
async.waterfall([
function (callback) {
if (to==null) {
client.llen("users", function(err, reply){
to=reply;
callback(null,to);
});
}
},
function(to, callback){
client.lrange("users",from,to,function(err,reply){
usuarios=reply;
callback(null,usuarios);
});
},
function(usuarios,callback){
debugger;
var respuestas = new Array();
var nresponses = 0;
for (id in usuarios) {
client.hgetall(usuarios[id], function (err, replies) {
debugger;
respuestas = respuestas.concat(replies);
console.log(replies);
if (++nresponses === usuarios.length) {
callback(null, respuestas);
}
});
}
}],function(err, replies) {
if(replies==null){
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not found");
response.end();
}else{
response.writeHead(200, {"Content-Type": "text/plain"});
response.write(replies[0]["email"]);
response.end();
}
});
}
function becarios(request,response) {
req_url = url.parse(request.url,true);
var method=request.method;
if(method=='GET'){
var usuario = req_url.query['id'];
var from = req_url.query['from'];
var to = req_url.query['to'];
if (usuario!=null) getUser(response,usuario);
else getUsers(from,to,response);
}
if(method=='POST'){
var body = '';
request.on('data', function (data) {
body += data;
});
request.on('end', function () {
var posted = qs.parse(body);
addUser(posted["id"],posted["email"],posted["nombre_com"],req_url,response);
});
}
}
exports.becarios = becarios;