1+ from fastapi import APIRouter , Depends , HTTPException
2+
3+ from app .middleware .auth import has_roles
4+ from app .schemas .user import UserRole
5+ from app .schemas .volunteer_data import (
6+ VolunteerDataCreateRequest ,
7+ VolunteerDataListResponse ,
8+ VolunteerDataResponse ,
9+ VolunteerDataUpdateRequest ,
10+ )
11+ from app .services .implementations .volunteer_data_service import VolunteerDataService
12+ from app .utilities .service_utils import get_volunteer_data_service
13+
14+ router = APIRouter (
15+ prefix = "/volunteer-data" ,
16+ tags = ["volunteer-data" ],
17+ )
18+
19+
20+ # Admin only - create volunteer data
21+ @router .post ("/" , response_model = VolunteerDataResponse )
22+ async def create_volunteer_data (
23+ volunteer_data : VolunteerDataCreateRequest ,
24+ volunteer_data_service : VolunteerDataService = Depends (get_volunteer_data_service ),
25+ authorized : bool = has_roles ([UserRole .ADMIN ]),
26+ ):
27+ try :
28+ return await volunteer_data_service .create_volunteer_data (volunteer_data )
29+ except HTTPException as http_ex :
30+ raise http_ex
31+ except Exception as e :
32+ raise HTTPException (status_code = 500 , detail = str (e ))
33+
34+
35+ # Admin only - get all volunteer data
36+ @router .get ("/" , response_model = VolunteerDataListResponse )
37+ async def get_all_volunteer_data (
38+ volunteer_data_service : VolunteerDataService = Depends (get_volunteer_data_service ),
39+ authorized : bool = has_roles ([UserRole .ADMIN ]),
40+ ):
41+ try :
42+ volunteer_data_list = await volunteer_data_service .get_all_volunteer_data ()
43+ return VolunteerDataListResponse (
44+ volunteer_data = volunteer_data_list , total = len (volunteer_data_list )
45+ )
46+ except HTTPException as http_ex :
47+ raise http_ex
48+ except Exception as e :
49+ raise HTTPException (status_code = 500 , detail = str (e ))
50+
51+
52+ # Admin only - get volunteer data by ID
53+ @router .get ("/{volunteer_data_id}" , response_model = VolunteerDataResponse )
54+ async def get_volunteer_data (
55+ volunteer_data_id : str ,
56+ volunteer_data_service : VolunteerDataService = Depends (get_volunteer_data_service ),
57+ authorized : bool = has_roles ([UserRole .ADMIN ]),
58+ ):
59+ try :
60+ return await volunteer_data_service .get_volunteer_data_by_id (volunteer_data_id )
61+ except HTTPException as http_ex :
62+ raise http_ex
63+ except Exception as e :
64+ raise HTTPException (status_code = 500 , detail = str (e ))
65+
66+
67+ # Admin only - get volunteer data by user ID
68+ @router .get ("/user/{user_id}" , response_model = VolunteerDataResponse )
69+ async def get_volunteer_data_by_user (
70+ user_id : str ,
71+ volunteer_data_service : VolunteerDataService = Depends (get_volunteer_data_service ),
72+ authorized : bool = has_roles ([UserRole .ADMIN ]),
73+ ):
74+ try :
75+ return await volunteer_data_service .get_volunteer_data_by_user_id (user_id )
76+ except HTTPException as http_ex :
77+ raise http_ex
78+ except Exception as e :
79+ raise HTTPException (status_code = 500 , detail = str (e ))
80+
81+
82+ # Admin only - update volunteer data
83+ @router .put ("/{volunteer_data_id}" , response_model = VolunteerDataResponse )
84+ async def update_volunteer_data (
85+ volunteer_data_id : str ,
86+ volunteer_data_update : VolunteerDataUpdateRequest ,
87+ volunteer_data_service : VolunteerDataService = Depends (get_volunteer_data_service ),
88+ authorized : bool = has_roles ([UserRole .ADMIN ]),
89+ ):
90+ try :
91+ return await volunteer_data_service .update_volunteer_data_by_id (
92+ volunteer_data_id , volunteer_data_update
93+ )
94+ except HTTPException as http_ex :
95+ raise http_ex
96+ except Exception as e :
97+ raise HTTPException (status_code = 500 , detail = str (e ))
98+
99+
100+ # Admin only - delete volunteer data
101+ @router .delete ("/{volunteer_data_id}" )
102+ async def delete_volunteer_data (
103+ volunteer_data_id : str ,
104+ volunteer_data_service : VolunteerDataService = Depends (get_volunteer_data_service ),
105+ authorized : bool = has_roles ([UserRole .ADMIN ]),
106+ ):
107+ try :
108+ await volunteer_data_service .delete_volunteer_data_by_id (volunteer_data_id )
109+ return {"message" : "Volunteer data deleted successfully" }
110+ except HTTPException as http_ex :
111+ raise http_ex
112+ except Exception as e :
113+ raise HTTPException (status_code = 500 , detail = str (e ))
0 commit comments