-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.js
182 lines (170 loc) · 5.47 KB
/
tasks.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
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
const express = require("express");
const serverless = require("serverless-http");
const cors = require("cors");
const bodyParser = require("body-parser");
const app = express();
app.use(cors());
app.use(bodyParser.json());
const mysql = require("mysql");
//////////////////////////////////// connect to the database //////////////////////////////////
const connection = mysql.createConnection({
host: process.env.dataBaseHost,
user: process.env.dataBaseUser,
password: process.env.dataBasePassword,
database: "rainbowhaven", // The DS that I created in mysql
});
////////////////////////////////////// Get ////////////////////////////////////////////
app.get("/items", function (req, res) {
const query = "SELECT * FROM items;";
connection.query(query, function (error, data) {
if (error) {
console.timeLog("Error fetching clients", error);
res.status(500).json({ error: error });
} else {
res.status(200).json({ items: data });
}
});
});
app.get("/volunteers", function (req, res) {
const query = "SELECT * FROM volunteer;";
connection.query(query, function (error, data) {
if (error) {
console.timeLog("Error fetching volunteers", error);
res.status(500).json({ error: error });
} else {
res.status(200).json({ volunteer: data });
}
});
});
////////////////////////////////////// Post ////////////////////////////////////////////
// request includes : full_name, email, address, postcode, phone, completed, date, zone, managerId
app.post("/items", function (req, res) {
const query =
"INSERT INTO items (full_name,email,address,postcode,phone, completed,date,zone,managerId) VALUES (?, ?, ?, ?,?,?,?,?,?);";
const queryTask = "SELECT * FROM items WHERE client_id = ?";
connection.query(
query,
[
req.body.full_name,
req.body.email,
req.body.address,
req.body.postcode,
req.body.phone,
req.body.completed,
req.body.date,
req.body.zone,
req.body.managerId,
],
function (error, data) {
if (error) {
console.log("Error adding the client", error);
res.status(500).json({ error: error });
} else {
connection.query(queryTask, [data.insertId], function (error, data) {
if (error) {
console.log("Error retreiving the client", error);
res.status(500).json({ error: error });
} else {
res.status(201).json({ task: data });
}
});
}
}
);
});
// request includes : full_name, email, phone, address, postcode, password, zone, managerId
app.post("/volunteer", function (req, res) {
const query =
"INSERT INTO volunteer (full_name,email,phone, address,postcode,password,zone,managerId) VALUES (?, ?, ?,?,?,?,?,?);";
const queryTask = "SELECT * FROM volunteer WHERE volunteer_Id = ?";
connection.query(
query,
[
req.body.full_name,
req.body.email,
req.body.phone,
req.body.address,
req.body.postcode,
req.body.password,
req.body.zone,
req.body.managerId,
],
function (error, data) {
if (error) {
console.log("Error adding the volunteer", error);
res.status(500).json({ error: error });
} else {
connection.query(queryTask, [data.insertId], function (error, data) {
if (error) {
console.log("Error retreiving the client", error);
res.status(500).json({ error: error });
} else {
res.status(201).json({ task: data });
}
});
}
}
);
});
/////////////////////////////////////// Delete ////////////////////////////////////
app.delete("/items/:client_id", function (req, res) {
const taskIdDeleted = "DELETE FROM items WHERE client_id=?";
const taskDeleted = "SELECT * FROM items;";
connection.query(taskIdDeleted, [req.params.client_id], function (
error,
data
) {
if (error) {
console.log("Error deleting the client", error);
res.status(500).json({ error: error });
} else {
res.sendStatus(200);
}
});
});
app.delete("/volunteer/:volunteer_Id", function (req, res) {
const taskIdDeleted = "DELETE FROM volunteer WHERE volunteer_Id=?";
const taskDeleted = "SELECT * FROM volunteer;";
connection.query(taskIdDeleted, [req.params.volunteer_Id], function (
error,
data
) {
if (error) {
console.log("Error deleting the volunteer", error);
res.status(500).json({ error: error });
} else {
res.sendStatus(200);
}
});
});
////////////////////////////////////// Put ////////////////////////////////////////////
// request includes : full_name, email, address, postcode, phone, completed, date, zone, managerId
app.put("/items/:client_id", function (req, res) {
const taskIdUpdated =
"UPDATE items SET full_name=?, email=?, address=?, postcode=?, phone=?, completed=?, date=?, zone=?, managerId=? WHERE client_id=?;";
const taskUpdated = "SELECT * FROM items;";
connection.query(
taskIdUpdated,
[
req.body.full_name,
req.body.email,
req.body.address,
req.body.postcode,
req.body.phone,
req.body.completed,
req.body.date,
req.body.zone,
req.body.managerId,
req.params.client_id,
],
function (error, date) {
if (error) {
console.log("Error updating the client", error);
res.status(500).json({ error: error });
} else {
res.sendStatus(200);
}
}
);
});
module.exports.handler = serverless(app);