-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
220 lines (196 loc) · 5.99 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
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
import express, { json } from "express";
const app = express()
import bodyParser from "body-parser";
import cors from "cors";
import { createDrug, createPharmacy, createUser, deleteDrug, deletePharmacy, logOutUser, main, readDrug, readDrugs, readPharmacies, readPharmacy, readUser, readUsers, signInUser, updateDrug, updatePharmacy } from "./connection.js";
const port = process.env.PORT || 3000;
app.use(cors());
app.use(json());
app.get("/api/status", (req, res) => {
res.send("check status");
})
// Pharmacy API
app.post("/api/createPharmacy", async(req, res) => {
try{
main().catch(console.error);
let result = await createPharmacy(req.body);
res.status(200).send('succesfully created pharmacy');
} catch (err){
console.log(err);
res.status(503).send('failed to create pharmacy');
}
})
app.post("/api/readPharmacies", async(req, res) => {
try{
main().catch(console.error);
let result = await readPharmacies();
res.status(200).send(result);
} catch (err){
console.log(err);
res.status(503).send('failed to read');
}
})
app.post("/api/readPharmacy", async(req, res) => {
try{
main().catch(console.error);
let result = await readPharmacy(req.body.pln);
res.status(200).send(result);
} catch (err){
console.log(err);
res.status(503).send('failed to read');
}
})
app.post("/api/updatePharmacy", async(req, res) => {
try{
main().catch(console.error);
let result = await updatePharmacy(req.body.pln, req.body.updated);
res.status(200).send("successfully updated");
} catch (err){
console.log(err);
res.status(503).send('failed to update');
}
})
app.post("/api/deletePharmacy", async(req, res) => {
try{
main().catch(console.error);
let result = await deletePharmacy(req.body.pln);
res.status(200).send("successfully deleted");
} catch (err){
console.log(err);
res.status(503).send('failed to delete');
}
})
// Drug API
app.post("/api/createDrug", async(req, res) => {
try{
main().catch(console.error);
let result = await createDrug(req.body);
res.status(200).send('succesfully created drug');
} catch (err){
console.log(err);
res.status(503).send('failed to create drug');
}
})
app.post("/api/readDrugs", async(req, res) => {
try{
main().catch(console.error);
let result = await readDrugs(req.body.query);
res.status(200).send(result);
} catch (err){
console.log(err);
res.status(503).send('failed to read');
}
})
app.post("/api/readDrug", async(req, res) => {
try{
main().catch(console.error);
let result = await readDrug(req.body.ndc);
res.status(200).send(result);
} catch (err){
console.log(err);
res.status(503).send('failed to read');
}
})
app.post("/api/updateDrug", async(req, res) => {
try{
main().catch(console.error);
let result = await updateDrug(req.body.ndc, req.body.updated);
res.status(200).send("successfully updated");
} catch (err){
console.log(err);
res.status(503).send('failed to update');
}
})
app.post("/api/deleteDrug", async(req, res) => {
try{
main().catch(console.error);
let result = await deleteDrug(req.body.ndc);
res.status(200).send("successfully deleted");
} catch (err){
console.log(err);
res.status(503).send('failed to delete');
}
})
// User API
app.post("/api/createUser", async(req, res) => {
try{
main().catch(console.error);
let result = await createUser(req.body);
res.status(200).send('succesfully created user');
} catch (err){
console.log(err);
res.status(503).send('failed to create user');
}
})
app.post("/api/readUsers", async(req, res) => {
try{
main().catch(console.error);
let result = await readUsers();
res.status(200).send(result);
} catch (err){
console.log(err);
res.status(503).send('failed to read');
}
})
app.post("/api/readUser", async(req, res) => {
try{
main().catch(console.error);
let result = await readUser(req.body.email);
res.status(200).send(result);
} catch (err){
console.log(err);
res.status(503).send('failed to read');
}
})
app.post("/api/updateUser", async(req, res) => {
try{
main().catch(console.error);
let result = await updateDrug(req.body.email, req.body.updated);
res.status(200).send("successfully updated");
} catch (err){
console.log(err);
res.status(503).send('failed to update');
}
})
app.post("/api/deleteUser", async(req, res) => {
try{
main().catch(console.error);
let result = await deleteDrug(req.body.email);
res.status(200).send("successfully deleted");
} catch (err){
console.log(err);
res.status(503).send('failed to delete');
}
})
app.post("/api/signInUser", async(req, res) => {
try{
main().catch(console.error);
let result = await signInUser(req.body.email, req.body.password);
// console.log(result);
if(result) return res.status(200).send("logged in");
return res.status(503).send("failed to login");
} catch (err){
console.log(err);
return res.status(503).send("failed to login");
}
})
app.post("/api/logOutUser", async(req, res) => {
try{
main().catch(console.error);
let result = await logOutUser(req.body.email);
// console.log(result);
if(result) return res.status(200).send("logged out");
return res.status(503).send("failed to log out");
} catch (err){
console.log(err);
return res.status(503).send("failed to log out");
}
})
app.post("/api/search", async (req, res) => {
try{
} catch (err) {
}
})
app.listen(port, ()=>{
console.log("Listening on port", port);
});