Skip to content

Commit fb70ba9

Browse files
committed
refactor(ucdays): students api
1 parent 4437d96 commit fb70ba9

File tree

3 files changed

+39
-33
lines changed

3 files changed

+39
-33
lines changed

bun.lockb

0 Bytes
Binary file not shown.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"devDependencies": {
2424
"@types/nodemailer": "^6.4.14",
2525
"@types/pug": "^2.0.10",
26-
"bun-types": "^1.1.0"
26+
"bun-types": "^1.1.1"
2727
},
2828
"module": "src/index.js",
2929
"trustedDependencies": [

src/api/univ_events/students.ts

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,55 @@
11
import type { ElysiaContext, ResponseBody } from "../../types";
22
import { ErrorTypes } from "../../types/enums";
3+
import { status501 } from "../../routes";
34

45
import UnivStudent from "../../db/models/univ_events/student";
56
import response from "../../utils/response";
67
import Strings from "../../config/strings";
7-
import { status501 } from "../../routes";
8-
9-
8+
import Log from "../../utils/log";
109

1110
/**
1211
* Students API
1312
* @author TotalElderBerry (Unknown af)
1413
* @param context
1514
*/
1615
export function students(context: ElysiaContext): Promise<ResponseBody | undefined> | ResponseBody {
17-
switch (context.request.method) {
18-
case "GET":
19-
// return getStudents(context);
20-
case "POST":
21-
return postStudents(context);
22-
case "PUT":
23-
// return putStudents(context);
24-
case "OPTIONS":
25-
return response.success();
26-
}
27-
28-
return status501(context);
16+
switch (context.request.method) {
17+
case "POST":
18+
return postStudents(context);
19+
case "OPTIONS":
20+
return response.success();
21+
}
22+
23+
return status501(context);
2924
}
3025

31-
async function postStudents(context: ElysiaContext){
32-
try {
33-
await UnivStudent.insert(context.body);
34-
// If no error, student is created
35-
return response.success(Strings.STUDENT_CREATED);
36-
} catch (error) {
37-
// if list of errors
38-
if (Array.isArray(error)) {
39-
context.set.status = 400;
40-
return response.error(error[0], error[1]);
41-
}
42-
43-
// If database error
44-
if (error === ErrorTypes.DB_ERROR) {
45-
context.set.status = 500;
46-
return response.error(Strings.STUDENT_POST_ERROR);
47-
}
26+
/**
27+
* POST /tatakforms/register
28+
*/
29+
async function postStudents(context: ElysiaContext) {
30+
try {
31+
// Insert student
32+
await UnivStudent.insert(context.body);
33+
// If no error, student is created
34+
return response.success("You have successfully registered! 💛");
35+
}
36+
37+
catch (error) {
38+
// if list of errors
39+
if (Array.isArray(error)) {
40+
context.set.status = 400;
41+
return response.error(error[0], error[1]);
4842
}
43+
44+
// If database error
45+
if (error === ErrorTypes.DB_ERROR) {
46+
context.set.status = 500;
47+
return response.error(Strings.STUDENT_POST_ERROR);
48+
}
49+
50+
// If unknown error
51+
Log.e(error);
52+
context.set.status = 500;
53+
return response.error(Strings.GENERAL_SYSTEM_ERROR);
54+
}
4955
}

0 commit comments

Comments
 (0)