-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
140 lines (119 loc) · 3.81 KB
/
index.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
134
135
136
137
138
139
140
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const cors = require('cors');
const mysql = require("mysql");
const db = mysql.createPool({
host: 'us-cdbr-east-05.cleardb.net',
user: 'b47960a65c7d36',
password: '67e03a47',
database: 'heroku_ba47a776f1afbd3',
// host: 'localhost',
// user: 'root',
// password: 'password',
// database: 'TheAppDB'
});
app.use(cors());
app.use(express.json());
app.use(bodyParser.urlencoded({extended: true}));
app.post("/api/insert", (req, res) => {
const indexCol = req.body.indexCol;
const listId = req.body.listId;
const sqlInsert = "INSERT INTO tasks (task, description, indexCol, listId) VALUES (NULL, NULL, ?, ?);";
db.query(sqlInsert, [indexCol, listId], (err, result) => {
console.log(err);
res.send(result);
});
});
app.get("/api/get/all", (req, res) => {
const sqlSelect = "SELECT * FROM tasks;";
db.query(sqlSelect, (err, result) => {
res.send(result);
})
})
app.get("/api/get/max", (req, res) => {
const sqlSelect = "SELECT MAX(id) FROM tasks;";
db.query(sqlSelect, (err, result) => {
res.send(result);
})
})
app.put("/api/update/task", (req, res) => {
const id = req.body.id;
const task = req.body.task;
const sqlUpdate = "UPDATE tasks SET task = ? WHERE id = ?;";
db.query(sqlUpdate, [task, id], (err, result) => {
if (err) console.log(err);
res.send(result);
})
});
app.put("/api/update/swapTaskIndex", (req, res) => {
const id1 = req.body.id1;
const index1 = req.body.index1;
const id2 = req.body.id2;
const index2 = req.body.index2;
const sqlUpdate1 = "UPDATE tasks SET indexCol = ? WHERE id = ?;"
db.query(sqlUpdate1, [index1, id2], (err, result) => {
if (err) console.log(err);
})
const sqlUpdate2 = "UPDATE tasks SET indexCol = ? WHERE id = ?;"
db.query(sqlUpdate2, [index2, id1], (err, result) => {
if (err) console.log(err);
res.send(result);
})
});
app.put("/api/update/listId", (req, res) => {
const id = req.body.id;
const index = req.body.index;
const listId = req.body.listId;
const newListId = req.body.newListId;
const newListLength = req.body.newListLength;
if (index > newListLength - 1) {
const sqlUpdate1 =
"UPDATE tasks SET indexCol = ?, listId = ? WHERE id = ?;";
db.query(sqlUpdate1, [newListLength, newListId, id], (err, result) => {
if (err) console.log(err);
})
}
else {
const sqlUpdate1 =
"UPDATE tasks SET indexCol = indexCol + 1 WHERE indexCol >= ? AND listId = ?";
db.query(sqlUpdate1, [index, newListId], (err, result) => {
if (err) console.log(err);
})
const sqlUpdate2 =
"UPDATE tasks SET listId = ? WHERE id = ?;";
db.query(sqlUpdate2, [newListId, id], (err, result) => {
if (err) console.log(err);
})
}
const sqlUpdate = "UPDATE tasks SET indexCol = indexCol - 1 WHERE indexCol > ? AND listId = ?;";
db.query(sqlUpdate, [index, listId], (err, result) => {
if(err) console.log(err);
res.send(result);
})
})
app.put("/api/update/decrementIndexes", (req, res) => {
const index = req.body.index;
const listId = req.body.listId;
const sqlUpdate = "UPDATE tasks SET indexCol = indexCol - 1 WHERE indexCol > ? AND listId = ?;"
db.query(sqlUpdate, [index, listId], (err, result) => {
if (err) console.log(err);
res.send(result);
});
});
app.delete("/api/delete/:id", (req, res) => {
const id = req.params.id;
console.log(id);
const sqlDelete = "DELETE FROM tasks WHERE id = ?;";
db.query(sqlDelete, id, (err, result) => {
if (err) console.log(err);
console.log("number of rows deleted = " + result.affectedRows);
res.send(result);
});
});
app.listen(process.env.PORT || 3001, () => {
console.log('running on port 3001');
});
app.get('/', (req, res) => {
res.send("welcome to earth");
})