-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.js
More file actions
79 lines (64 loc) · 2.77 KB
/
Copy pathuser.js
File metadata and controls
79 lines (64 loc) · 2.77 KB
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
var mysql = require("mysql");
var q = require("q");
var pool = mysql.createPool({
host: "localhost",
user: "root",
password: "",
database: "todo_list",
port: 3306,
connectionLimit: 4
});
function User(id, email, password, password_original, firstName, lastName, gender, country, dateOfBirth, photo) {
this.user_id = id;
this.email = email;
this.password = password;
this.password_original = password_original;
this.firstName = firstName;
this.lastName = lastName;
this.gender = gender;
this.country = country;
this.dateOfBirth = dateOfBirth;
this.picture = photo;
}
const FINDUSERSQL = "select * from users where email = ? and password = ?";
const SAVENEWUSERSQL = "INSERT INTO users (email, password, password_original, first_name, last_name, gender, country, date_of_birth, photo) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
const READUSERSQL = "SELECT id, email, first_name, last_name, gender, date_of_birth, country, photo, pic_url FROM users WHERE id = ?";
const FINDUSERIDSQL = "SELECT id FROM users WHERE email = ?";
const FINDUSERIDSSQL = "SELECT id FROM users WHERE email IN (?)";
const FINDUSERBYEMAILSQL = "SELECT id FROM users WHERE email = ?";
const SAVEUSERSOCIALPROFILESQL = "INSERT INTO users (email, first_name, last_name, gender) VALUES (?,?,?,?)";
const SAVEEDITUSERSQL = "UPDATE users SET email=?, first_name=?, last_name=?, gender=?, date_of_birth=?, country=? WHERE id = ?";
const DELETEUSERSQL = "UPDATE users SET disabled = 1 WHERE id = ?";
//const UPLOADIMGSQL = "UPDATE users SET picture = ? WHERE user_id = ?";
var makeQuery = function (sql, pool) {
return function (args) {
var defer = q.defer();
pool.getConnection(function (err, connection) {
if (err) {
return defer.reject(err);
}
//console.log("args:"+args);
connection.query(sql, args || [], function (err, result) {
connection.release();
if (err) {
// database error
return defer.reject(err);
}
defer.resolve(result);
})
});
return defer.promise;
};
};
User.findUser = makeQuery(FINDUSERSQL, pool);
User.prototype.saveNewUser = makeQuery(SAVENEWUSERSQL, pool);
User.readUser = makeQuery(READUSERSQL, pool);
User.findUserId = makeQuery(FINDUSERIDSQL, pool);
User.findUserIds = makeQuery(FINDUSERIDSSQL, pool);
User.findUserByEmail = makeQuery(FINDUSERBYEMAILSQL, pool);
User.saveUserSocialProfile = makeQuery(SAVEUSERSOCIALPROFILESQL, pool);
User.saveEditUser = makeQuery(SAVEEDITUSERSQL, pool);
User.deleteuser = makeQuery(DELETEUSERSQL, pool);
//User.prototype.uploadImg = makeQuery(UPLOADIMGSQL, pool);
module.exports = User;