generated from lighthouse-labs/node-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.js
63 lines (50 loc) · 1.71 KB
/
database.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
// PG database client/connection setup
const { Pool } = require('pg');
const dbParams = require('./lib/db.js');
const db = new Pool(dbParams);
const getUser = function(username) {
return db.query(`SELECT * FROM users WHERE name = $1`, [username])
.then(res => res.rows[0])
.catch(err => err);
};
exports.getUser = getUser;
const addTodo = function(userId, category, todo) {
return db.query(`INSERT INTO to_dos (user_id, category, text) VALUES ($1, $2, $3)`,
[userId, category, todo])
.then(res => res.rows)
.catch(err => err);
};
exports.addTodo = addTodo;
const getTodos = function(userid) {
return db.query(`SELECT * FROM to_dos WHERE user_id = $1`, [userid])
.then(res => res.rows)
.catch(err => err);
};
exports.getTodos = getTodos;
const removeTodo = function(todoId) {
return db.query(`DELETE FROM to_dos WHERE id = $1`,
[todoId])
.then(res => res.rows[0])
.catch(err => err);
};
exports.removeTodo = removeTodo;
const completeToDo = function(todoId) {
return db.query(`SELECT completed FROM to_dos WHERE id = $1`, [todoId])
.then((result)=> {
const verdict = result.rows[0].completed
console.log(verdict)
if (verdict === true){
return db.query(`UPDATE to_dos SET completed = false WHERE id = $1`, [todoId])
// // .then(res => console.log(res))
// .catch(err => err)
} else if (verdict === false) {
return db.query(`UPDATE to_dos SET completed = true WHERE id = $1`, [todoId])
// .then(res => res.rows[0])
// .catch(err => err)
}
});
}
exports.completeToDo = completeToDo;
// return db.query(`UPDATE to_dos SET completed = true WHERE id = $1`, [todoId])
// .then(res => res.rows[0])
// .catch(err => err);