-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser-model.js
42 lines (36 loc) · 1001 Bytes
/
user-model.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
const db = require("../database/connection");
function resultRowsZero(result){
return result.rows[0];
}
function resultRows(result){
return result.rows;
}
//Adds user to db and returns newly created user obj
function addUser(username, hashedPassword) {
return db
.query(
"INSERT INTO users(username, password) VALUES (($1), ($2)) RETURNING id, username",
[
username,
hashedPassword
]
)
.then(resultRowsZero)
}
//Gets a single user by id
function getUser(userId) {
return db
.query("SELECT id, username FROM users WHERE id=($1)", [userId])
.then(resultRowsZero);
}
//Gets a single user by username
function getUserByName(username) {
return db
.query("SELECT id, username FROM users WHERE username=($1)", [username])
.then(resultRowsZero);
}
//Gets all users
function getEveryUser() {
return db.query("SELECT id, username FROM users").then(resultRows);
}
module.exports = { addUser, getUser, getEveryUser, getUserByName };