forked from emil10001/AWS-NodeJS-AngularJS-Demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.js
64 lines (57 loc) · 1.99 KB
/
users.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
/**
* Created by ejf3 on 12/28/13.
*/
var c = require('../constants')
, converter = require('../utils/dynamo_to_json.js')
, mysql = require('mysql');
Users = function (rds, conf, rdsMedia) {
this.rds = rds;
this.connection = mysql.createConnection(conf);
this.rdsMedia = rdsMedia;
var self = this;
this.connection.connect(function(err){
if (err)
console.error("couldn't connect",err);
else
console.log("mysql connected");
});
this.getAll = function(socket){
var query = this.connection.query('select * from users;', function(err,result){
if (err){
console.error("failed to get",err);
socket.emit(c.RDS_GET_USERS, c.ERROR);
} else {
console.log("got",result);
socket.emit(c.RDS_GET_USERS, result);
}
});
console.log(query.sql);
};
this.addUpdateUser = function(user, socket){
var query = this.connection.query('INSERT INTO users SET ?', user, function(err, result) {
if (err) {
console.error("failed to insert",err);
socket.emit(c.RDS_UPDATE_USER, c.ERROR);
} else {
console.log("inserted",result);
socket.emit(c.RDS_UPDATE_USER, result);
}
});
console.log(query.sql);
};
this.deleteUser = function(userId, socket){
self.rdsMedia.deleteUserMedia(userId, function(){
var query = self.connection.query('DELETE FROM users WHERE id = ?', userId, function(err, result) {
if (err) {
console.error("failed to delete",err);
socket.emit(c.RDS_DELETE_USER, c.ERROR);
} else {
console.log("deleted",result);
socket.emit(c.RDS_DELETE_USER, result);
}
});
console.log(query.sql);
}, socket);
};
};
module.exports = Users;