-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.js
66 lines (57 loc) · 1.67 KB
/
db.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
const environment = process.env.NODE_ENV || 'development'
const config = require('./knexfile')[environment]
const connection = require('knex')(config)
module.exports = {
getAllTasks,
getAllUsers,
getTask,
getUser,
getCollabs,
addTask,
editTask
}
function getAllTasks(UID, db = connection){
return db('users').select('tasks.id', 'tasks.name', 'tasks.description', 'tasks.creation_date', 'tasks.status')
.join('task_user', 'users.id', 'task_user.user_id')
.join('tasks', 'task_user.task_id', 'tasks.id')
.where('users.id', UID)
}
function getTask(taskID, db = connection){
return db('tasks')
.select()
.where('tasks.id', taskID)
}
function getCollabs(taskID, db = connection){
return db('task_user')
.select('users.id', 'users.name', 'users.email')
.join('users', 'users.id', 'task_user.user_id')
.where('task_user.task_id', taskID)
}
function getAllUsers(db = connection){
return db('users').select()
}
function getUser(login, db = connection){
return db('users')
.select()
.where('email', login)
}
function addTask(UID, task, db = connection){
const date = new Date().toString().slice(0,15)
var cleanData = {
name: task.name,
description: task.description,
creation_date: date,
status: task.status
}
return db('tasks')
.insert(cleanData)
.then((taskID) => {
return db('task_user')
.insert({'user_id': UID, 'task_id': taskID[0]})
})
}
function editTask(taskID, taskItem, db = connection){
return db('tasks')
.where('id', taskID)
.update(taskItem)
}