-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
171 lines (159 loc) · 4.11 KB
/
index.js
File metadata and controls
171 lines (159 loc) · 4.11 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
import express from "express";
import { validateRegistration } from "./middlewares/registration/regHelper.js";
import prisma from "./prisma/client.js";
import { validateLogin } from "./middlewares/token/loginHelper.js";
import jwt from "jsonwebtoken";
import { checkInsert } from "./middlewares/keyValueData/insertHelper.js";
import { validRead } from "./middlewares/keyValueData/readHelper.js";
import { validUpdateDelete } from "./middlewares/keyValueData/updateDelHelper.js";
const app = express();
app.use(express.json());
app.get("/", (req, res) => {
res.json({ hello: "dpdZero task running" });
});
app.post("/api/register", validateRegistration, async (req, res) => {
try {
const createdUser = await prisma.users.create({ data: req.body });
res.status(201);
delete createdUser.password;
res.json({
status: "success",
message: "User successfully registered!",
data: createdUser,
});
} catch (error) {
console.log(error);
res.status(500);
res.json({
status: "error",
code: "INTERNAL_SERVER_ERROR",
message: "An internal server error occurred. Please try again later.",
});
}
});
app.post("/api/token", validateLogin, (req, res) => {
try {
const accessToken = jwt.sign(
{
loggedIn: req.body.username,
},
process.env.TOKEN_SECRET,
{ expiresIn: "1h" }
);
res.status(200);
res.json({
status: "success",
message: "Access token generated successfully.",
data: {
access_token: accessToken,
expires_in: 3600,
},
});
} catch (error) {
console.log(error);
res.status(500);
res.json({
status: "error",
code: "INTERNAL_ERROR",
message: "Internal server error occurred. Please try again later.",
});
}
});
app.post("/api/data", checkInsert, async (req, res) => {
try {
const insertedData = await prisma.kvPairData.create({ data: req.body });
res.status(201);
res.json({
status: "success",
message: "Data stored successfully.",
});
} catch (error) {
console.log(error);
res.status(500);
res.json({
status: "error",
code: "INTERNAL_ERROR",
message: "Internal server error occurred. Please try again later.",
});
}
});
app.get("/api/data/:key", validRead, async (req, res) => {
try {
const checkKey = await prisma.kvPairData.findUnique({
where: {
key: req.params.key,
},
});
if (checkKey === null) {
res.status(404);
res.json({
status: "error",
code: "KEY_NOT_FOUND",
message: "The provided key does not exist in the database.",
});
return;
}
res.status(200);
res.json({
status: "success",
data: checkKey,
});
} catch (error) {
console.log(error);
res.status(500);
res.json({
status: "error",
code: "INTERNAL_ERROR",
message: "Internal server error occurred. Please try again later.",
});
}
});
app.put("/api/data/:key", validUpdateDelete, async (req, res) => {
try {
const updatedData = await prisma.kvPairData.update({
where: {
key: req.params.key,
},
data: {
value: req.body.value,
},
});
res.status(200);
res.json({
status: "success",
message: "Data updated successfully.",
});
} catch (error) {
console.log(error);
res.status(500);
res.json({
status: "error",
code: "INTERNAL_ERROR",
message: "Internal server error occurred. Please try again later.",
});
}
});
app.delete("/api/data/:key", validUpdateDelete, async (req, res) => {
try {
await prisma.kvPairData.delete({
where: {
key: req.params.key,
},
});
res.status(200);
res.json({
status: "success",
message: "Data deleted successfully.",
});
} catch (error) {
res.status(500);
res.json({
status: "error",
code: "INTERNAL_ERROR",
message: "Internal server error occurred. Please try again later.",
});
}
});
app.listen(process.env.PORT, () => {
console.log(`DPD Zero Task Running on port ${process.env.PORT}`);
});