-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitemsHelper.js
More file actions
104 lines (102 loc) · 2.93 KB
/
Copy pathitemsHelper.js
File metadata and controls
104 lines (102 loc) · 2.93 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const ENV = process.env.ENV || "development";
const knexConfig = require("./knexfile");
const knex = require("knex")(knexConfig[ENV]);
const morgan = require('morgan');
const knexLogger = require('knex-logger');
module.exports = {
findItemByText: function(text_from_user) {
return knex
.select("*")
.from("items")
.where({text_from_user: text_from_user})
.orWhere(({text_from_user: text_from_user.toLowerCase()}))
.then(function(item){
return item;
})
.catch(function (e) {
console.error(e);
})
},
findItemById: function(id) {
return knex
.select("text_from_user")
.from("items")
.where({id: id})
.then(function(item){
return item;
})
.catch(function (e) {
console.error(e);
})
},
findItemState: function(user_id, id) {
return knex
.select('*')
.from('users_items')
.where({user_id: user_id, item_id: id})
.then(function (state){
return state;
})
.catch(function (e) {
console.error(e);
})
},
addNewItem: function (user_id, category_Id, text_from_user) {
return knex.insert([{ category_id: category_Id, text_from_user: text_from_user}], 'id').into('items')
.then(function(newID){
return knex.insert([{user_id: user_id, item_id: newID[0], state: false, date_created: new Date()}]).into("users_items")
.then(function(){
}).catch(function (e) {
console.error(e);
})
})
.catch(function (e) {
console.error(e);
})
},
addNewItemRelation: function (user_id, category_Id) {
return knex.insert([{user_id: user_id, item_id: category_Id, state: false, date_created: new Date()}]).into("users_items")
.then(function(){
}).catch(function (e) {
console.error(e);
})
},
updateItemText: function(id, text_from_user) {
return knex('items')
.where({id: id})
.update({text_from_user: text_from_user})
.then(function(){
}).catch(function (e) {
console.error(e);
})
},
updateItemTextAndCategory: function(id, category_id, text_from_user) {
return knex('items')
.where({id: id})
.update({category_id: category_id,
text_from_user: text_from_user})
.then(function(){
}).catch(function (e) {
console.error(e);
})
},
updateState: function(user_id, item_id, state){
return knex('users_items')
.where({user_id: user_id, item_id: item_id})
.update({state: state})
.then(function(){
}).catch(function (e) {
console.error(e);
})
},
deleteItemRelation: function(user_id, item_id){
return knex('users_items')
.where({user_id: user_id, item_id: item_id})
.del()
.then(function(){
})
.catch(function (e) {
console.error(e);
})
}
};