-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.js
133 lines (115 loc) · 3.97 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
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
var mysql = require('mysql');
var connection;
function openDB(){
connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "testPasswort",
database: "tasklist",
});
connection.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
}
//<------------------------------------------------------------------------ PROJECTS --------------------------------------------------------------------------->
function insertProject(project, callback){
connection.query("INSERT INTO projekt (title, active, clientId) VALUES ('" + project.title + "'," + 0 + ",'" + uuidv4() + "')", function(err, results){
if(!err){
console.log('Query successfull.');
callback(200, project);
}else{
console.log('Error while performing the Query.', err);
callback(404, err);
}
});
}
function removeProject(id, callback){
connection.query("DELETE FROM projekt WHERE clientId='" + id + "'", function(err, results){
if(!err){
console.log('Query successfull.');
callback(200, results);
}else{
console.log('Error while performing the Query.', err);
callback(404, err);
}
});
}
function updateProject(id, active, callback){
connection.query("UPDATE projekt SET active = '"+ active +"' WHERE clientId ='" + id +"'", function(err, results) {
if(!err){
console.log('Query successfull.');
callback(200, results);
}else{
console.log('Error while performing the Query.', err);
callback(404, err);
}
});
}
function getAllProject(callback){
connection.query("SELECT * FROM projekt", function(err, results) {
if(!err){
console.log('Query successfull.');
callback(200, results);
}else{
console.log('ERROR while.', err);
callback(404, err);
}
});
}
//<------------------------------------------------------------------------ ISSUES --------------------------------------------------------------------------->
function insertIssue(issue, callback){
connection.query("INSERT INTO issue (projektId, clientId, done, title, due_date, created_at, updated_at, priority) VALUES ('"+ issue.project_id + "','"
+ uuidv4() + "','" + issue.done + "','" + issue.title + "','" + issue.due_date + "','" + issue.created_at + "','" + issue.updated_at + "','" + issue.priority + "')" , function(err, results){
if(!err){
console.log('Query successfull.');
callback(200, issue);
}else{
console.log('Error while performing the Query.', err);
callback(404, err);
}
});
}
function getIssuesOfProject(id, callback){
connection.query("SELECT * FROM issue WHERE projektID ='" + id + "'", function(err, results){
if(!err){
console.log('Query successfull.');
callback(200,results);
}else{
console.log('Error while performing the Query.', err);
callback(404, err);
}
});
}
function removeIssue(projectId, issueId, callback){
console.log(projectId,issueId)
connection.query("DELETE FROM issue WHERE projektId ='" + projectId + "' AND id ='" + issueId + "'", function(err, results){
if(!err){
console.log('Query successfull.');
callback(200, results);
}else{
console.log('Error while performing the Query.', err);
callback(404, err);
}
});
}
//<------------------------------------------------------------------------ UUID AND EXPORTS --------------------------------------------------------------------------->
// generate uuidv4
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
module.exports = {
//close: closeDB,
open: openDB,
insertProject: insertProject,
updateProject: updateProject,
removeProject: removeProject,
insertIssue: insertIssue,
//updateIssue: updateIssue,
removeIssue: removeIssue,
getAllProject: getAllProject,
getIssues: getIssuesOfProject
}