-
Notifications
You must be signed in to change notification settings - Fork 1
/
mysql_user.js
90 lines (79 loc) · 2.04 KB
/
mysql_user.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
var mysql = require('./mysql.js');
var User = require('./user.js');
var Promise = require("bluebird");
var mysqluser = function() {
}
mysqluser.prototype.getUserByEmail = function (email, callback) {
mysql.getConnection(function(err, conn) {
if(err) {
callback(err, null);
return;
}
var sql = "select * from user where email like ?";
conn.query(sql, [email], function(err, rows, field){
if(err) {
callback(err, null);
} else if(rows.length == 0) {
callback(null, null);
} else {
var user = new User();
user.fromObject(rows[0]);
callback(null, user);
}
conn.release();
});
});
}
mysqluser.prototype.userSignUp = function (user, callback) {
mysql.getConnection(function(err, conn) {
if(err) {
callback(err, null);
return;
}
var sql = "insert into user set ? ";
conn.query(sql, user, function(err, rows, field){
if(err) {
callback(err, null);
} else {
callback(null, rows.insertId);
}
conn.release();
});
});
}
mysqluser.prototype.updateUserPassword = function (user, callback) {
mysql.getConnection(function(err, conn) {
if(err) {
callback(err, null);
return;
}
var sql = "UPDATE `user` SET `password` = ? WHERE `user`.`userid` = ?;";
console.log(user)
conn.query(sql, [user.password, user.userid], function(err, rows, field){
callback(err);
conn.release();
});
});
}
mysqluser.prototype.getUserByID = function (userid, callback) {
mysql.getConnection(function(err, conn) {
if(err) {
callback(err, null);
return;
}
var sql = "select * from user where userid = ?;";
conn.query(sql, [userid], function(err, rows, field){
if(err) {
callback(err, null);
} else if(rows.length == 0) {
callback(null, null);
} else {
var user = new User();
user.fromObject(rows[0]);
callback(null, user);
}
conn.release();
});
});
}
module.exports = new mysqluser();