-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.js
More file actions
228 lines (190 loc) · 6.63 KB
/
Copy pathcontroller.js
File metadata and controls
228 lines (190 loc) · 6.63 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
'use strict';
const response = require('./res');
const connection = require('./conn');
exports.index = function(req, res) {
response.ok("welcome to simple note app",'', res)
};
//CRUD notes
exports.showNotes = function(req, res) {
let noteId = req.params.noteId;
let categoryId = req.query.categoryId;
let search = req.query.search || '';
let sort = req.query.sort || 'desc';
//variable for pagination
let page = parseInt(req.query.page) || 1;
let limit = parseInt(req.query.limit) || 10;
let offset = ((page * limit) - limit);
//variable for info pagination
let totalData;
let totalPage;
//query search, sort, pagination all notes
let query = `SELECT b.id, b.title, b.note, b.time, c.id as "categoryId", c.name as "categoryName"
FROM notes b left join category c on (b.id_category=c.id)
where b.title like '%${search}%' order by time ${sort} limit ${limit} offset ${offset}`
//query search, sort, pagination all notes by category
let query2 = `SELECT b.id, b.title, b.note, b.time, c.id as "categoryId", c.name as "categoryName"
FROM notes b left join category c on (b.id_category=c.id)
where b.title like '%${search}%' and c.id = ${categoryId} order by time ${sort} limit ${limit} offset ${offset}`
//query total data
connection.query(`SELECT count(*) as total from notes where title like '%${search}%'`,
function (error, rows, fields){
totalData = rows[0].total;
totalPage = Math.ceil(Number(totalData)/limit);
});
//for search by id note with parameter in url
if (noteId != null){
connection.query(`SELECT b.id, b.title, b.note, b.time, c.name as "categoryName"
FROM notes b left join category c on (b.id_category=c.id)
where b.id = ${noteId}`, function (error, rows, fields){
if(error){
console.log(error)
} else{
response.ok(rows, res)
}
});
}
if (noteId == null && categoryId ==null){
connection.query(query, function (error, rows, fields){
if(error){
console.log(error)
}else{
//search using function infoPage in res.js for show info total data in pagination
response.pagination(totalData,page,totalPage,limit,rows,res)
}
});
}
if (noteId == null && categoryId !=null){
connection.query(query2, function (error, rows, fields){
if(error){
console.log(error)
}else{
//search using function infoPage in res.js for show info total data in pagination
response.pagination(totalData,page,totalPage,limit,rows,res)
}
});
}
};
exports.createNote = function(req, res) {
let title = req.body.title;
let note = req.body.note;
let categoryId = req.body.categoryId;
connection.query('INSERT INTO notes (title, note, id_category) values (?,?,?)',
[ title, note, categoryId ],
function (error, rows, fields){
if(error){
console.log(error)
} else{
response.ok(rows, res)
}
});
};
exports.updateNote = function(req, res) {
let title = req.body.title;
let note = req.body.note;
let noteId = req.params.noteId;
let categoryId = req.body.categoryId;
if (title != null && note!= null && categoryId != null){
connection.query('UPDATE notes SET title = ?, note = ?, id_category = ? WHERE id = ?',
[ title, note, categoryId, noteId ],
function (error, rows, fields){
if(error){
console.log(error)
} else{
response.ok(rows, res)
}
});
}
if (title != null && note!= null && categoryId == null){
connection.query('UPDATE notes SET title = ?, note = ? WHERE id = ?',
[ title, note, noteId ],
function (error, rows, fields){
if(error){
console.log(error)
} else{
response.ok(rows, res)
}
});
}
};
exports.deleteNote = function(req, res) {
let noteId = req.params.noteId;
connection.query('DELETE FROM notes WHERE id = ?',
[ noteId ],
function (error, rows, fields){
if(error){
console.log(error)
} else{
response.delete(rows, res, noteId)
}
});
};
//CRUD category
exports.showCategories = function(req, res) {
let categoryId = req.params.categoryId;
if (categoryId != null){
connection.query(`SELECT * FROM category where id = ${categoryId}`, function (error, rows, fields){
if(error){
console.log(error)
} else{
response.ok(rows, res)
}
});
}
if (categoryId == null){
connection.query(`SELECT * FROM category`, function (error, rows, fields){
if(error){
console.log(error)
} else{
response.ok(rows, res)
}
});
}
};
exports.createCategory = function(req, res) {
let description = req.body.description;
let name = req.body.name;
let image = req.body.image;
connection.query('INSERT INTO category (name, description, image) values (?,?,?)',
[ name, description, image],
function (error, rows, fields){
if(error){
console.log(error)
} else{
connection.query(`SELECT * FROM category where id = (select max(id) from category)`,
function (error, rows, fields){
response.ok(rows, res)
})
}
});
};
exports.updateCategory = function(req, res) {
let name = req.body.name;
let description = req.body.description;
let categoryId = req.params.categoryId;
if (name != null && description!= null){
connection.query('UPDATE category SET name = ?, description = ? WHERE id = ?',
[ name, description, categoryId],
function (error, rows, fields){
if(error){
console.log(error)
} else{
response.ok(rows, res)
}
});
}
else{
return res.send({message : "data cannot be empty" })
}
};
exports.deleteCategory = function(req, res) {
let categoryId = req.params.categoryId;
connection.query('DELETE FROM category WHERE id = ?',
[ categoryId ],
function (error, rows, fields){
if(error){
console.log(error)
} else{
response.delete(rows, res, categoryId)
}
});
};