1
1
from typing import Annotated
2
2
3
- from fastapi import APIRouter , Depends , HTTPException , Query
3
+ from fastapi import APIRouter , Body , Depends , HTTPException , Query , status
4
4
5
5
from app .api .endpoints .user import get_current_user
6
6
from app .models .registration import registration_model
7
7
from app .models .volunteer import volunteer_model
8
8
from app .schemas .event import Event
9
- from app .schemas .registration import Registration , RegistrationStatus
9
+ from app .schemas .registration import CreateRegistrationRequest , Registration , RegistrationStatus
10
10
from app .schemas .user import User , UserType
11
11
12
12
router = APIRouter ()
@@ -21,7 +21,9 @@ async def get_volunteers_by_event(event_id: str) -> list[Registration]:
21
21
@router .get ("/events/{volunteer_id}" , response_model = list [Event ])
22
22
async def get_events_by_volunteer (
23
23
volunteer_id : str ,
24
- status : Annotated [RegistrationStatus | None , Query (description = "Filter by status" )] = None ,
24
+ registration_status : Annotated [
25
+ RegistrationStatus | None , Query (description = "Filter by status" )
26
+ ] = None ,
25
27
current_user : Annotated [User , Depends (get_current_user )] = None ,
26
28
) -> list [Event ]:
27
29
volunteer = await volunteer_model .get_volunteer_by_id (volunteer_id )
@@ -37,4 +39,43 @@ async def get_events_by_volunteer(
37
39
elif current_user .user_type != UserType .ADMIN :
38
40
raise HTTPException (status_code = status .HTTP_403_FORBIDDEN , detail = "Access denied" )
39
41
40
- return await registration_model .get_events_by_volunteer (volunteer_id , status )
42
+ return await registration_model .get_events_by_volunteer (volunteer_id , registration_status )
43
+
44
+
45
+ @router .post ("/new" , response_model = Registration )
46
+ async def create_registrion (
47
+ registration : Annotated [CreateRegistrationRequest , Body (...)],
48
+ current_user : Annotated [User , Depends (get_current_user )],
49
+ ) -> Registration :
50
+ if current_user .user_type != UserType .VOLUNTEER :
51
+ raise HTTPException (
52
+ status_code = status .HTTP_403_FORBIDDEN , detail = "Only volunteers can register for events"
53
+ )
54
+
55
+ if current_user .entity_id is None :
56
+ raise HTTPException (
57
+ status_code = status .HTTP_400_BAD_REQUEST ,
58
+ detail = "You must be associated with a volunteer profile to register for an event" ,
59
+ )
60
+
61
+ return await registration_model .create_registration (registration , current_user .entity_id )
62
+
63
+
64
+ @router .put ("/unregister/{registration_id}" , response_model = Registration )
65
+ async def unregister_registration (
66
+ registration_id : str ,
67
+ current_user : Annotated [User , Depends (get_current_user )],
68
+ ) -> Registration :
69
+ if current_user .user_type != UserType .VOLUNTEER :
70
+ raise HTTPException (
71
+ status_code = status .HTTP_403_FORBIDDEN ,
72
+ detail = "Only volunteers can unregister from events" ,
73
+ )
74
+
75
+ if current_user .entity_id is None :
76
+ raise HTTPException (
77
+ status_code = status .HTTP_400_BAD_REQUEST ,
78
+ detail = "You must be associated with a volunteer profile to unregister from an event" ,
79
+ )
80
+
81
+ return await registration_model .unregister_registration (registration_id , current_user .entity_id )
0 commit comments