1
1
import { Router , type Request , type Response } from "express" ;
2
- import { createActivity } from "../controllers/activities.controller" ;
2
+ import {
3
+ createActivity ,
4
+ getAllActivities ,
5
+ getActivityById ,
6
+ updateActivity ,
7
+ deleteActivity ,
8
+ } from "../controllers/activities.controller" ;
3
9
import { HttpError , HttpStatus } from "../utils/errors" ;
4
10
import { type ActivitiesType } from "../models/activities.model" ;
5
11
@@ -10,7 +16,25 @@ activitiesRouter.post(
10
16
"/" ,
11
17
async ( req : Request < any , any , ActivitiesType > , res : Response ) => {
12
18
try {
13
- const activities = await createActivity ( req . body ) ;
19
+ const activity = await createActivity ( req . body ) ;
20
+ res . status ( HttpStatus . OK ) . json ( activity ) ;
21
+ } catch ( err : unknown ) {
22
+ if ( err instanceof HttpError ) {
23
+ res . status ( err . errorCode ) . json ( { error : err . message } ) ;
24
+ } else {
25
+ res
26
+ . status ( HttpStatus . INTERNAL_SERVER_ERROR )
27
+ . json ( { error : "An unknown error occurred" } ) ;
28
+ }
29
+ }
30
+ } ,
31
+ ) ;
32
+
33
+ activitiesRouter . get (
34
+ "/" ,
35
+ async ( req : Request < any , any , ActivitiesType > , res : Response ) => {
36
+ try {
37
+ const activities = await getAllActivities ( req . body . user ) ;
14
38
res . status ( HttpStatus . OK ) . json ( activities ) ;
15
39
} catch ( err : unknown ) {
16
40
if ( err instanceof HttpError ) {
@@ -23,3 +47,57 @@ activitiesRouter.post(
23
47
}
24
48
} ,
25
49
) ;
50
+
51
+ activitiesRouter . get (
52
+ "/:activityId" ,
53
+ async ( req : Request < any , any , ActivitiesType > , res : Response ) => {
54
+ try {
55
+ const activity = await getActivityById ( req . body . user , req . params . activityId ) ;
56
+ res . status ( HttpStatus . OK ) . json ( activity ) ;
57
+ } catch ( err : unknown ) {
58
+ if ( err instanceof HttpError ) {
59
+ res . status ( err . errorCode ) . json ( { error : err . message } ) ;
60
+ } else {
61
+ res
62
+ . status ( HttpStatus . INTERNAL_SERVER_ERROR )
63
+ . json ( { error : "An unknown error occurred" } ) ;
64
+ }
65
+ }
66
+ } ,
67
+ ) ;
68
+
69
+ activitiesRouter . put (
70
+ "/:activityId" ,
71
+ async ( req : Request < any , any , ActivitiesType > , res : Response ) => {
72
+ try {
73
+ const activity = await updateActivity ( req . body . user , req . params . activityId , req . body ) ;
74
+ res . status ( HttpStatus . OK ) . json ( activity ) ;
75
+ } catch ( err : unknown ) {
76
+ if ( err instanceof HttpError ) {
77
+ res . status ( err . errorCode ) . json ( { error : err . message } ) ;
78
+ } else {
79
+ res
80
+ . status ( HttpStatus . INTERNAL_SERVER_ERROR )
81
+ . json ( { error : "An unknown error occurred" } ) ;
82
+ }
83
+ }
84
+ } ,
85
+ ) ;
86
+
87
+ activitiesRouter . delete (
88
+ "/:activityId" ,
89
+ async ( req : Request < any , any , ActivitiesType > , res : Response ) => {
90
+ try {
91
+ const activity = await deleteActivity ( req . body . user , req . params . activityId ) ;
92
+ res . status ( HttpStatus . OK ) . json ( activity ) ;
93
+ } catch ( err : unknown ) {
94
+ if ( err instanceof HttpError ) {
95
+ res . status ( err . errorCode ) . json ( { error : err . message } ) ;
96
+ } else {
97
+ res
98
+ . status ( HttpStatus . INTERNAL_SERVER_ERROR )
99
+ . json ( { error : "An unknown error occurred" } ) ;
100
+ }
101
+ }
102
+ } ,
103
+ ) ;
0 commit comments