11using AutoMapper ;
2+ using exercise . wwwapi . DTOs ;
23using exercise . wwwapi . DTOs . Cohort ;
4+ using exercise . wwwapi . DTOs . Posts ;
35using exercise . wwwapi . Models ;
46using exercise . wwwapi . Repository ;
57using Microsoft . AspNetCore . Mvc ;
@@ -28,9 +30,16 @@ public static async Task<IResult> GetCohort(IRepository<Cohort> service, IMapper
2830 . Include ( c => c . CohortCourses )
2931 . ThenInclude ( cc => cc . CohortCourseUsers )
3032 . ThenInclude ( ccu => ccu . User ) ) ;
31- CohortDTO cohortDTOs = mapper . Map < CohortDTO > ( result ) ;
33+ CohortDTO cohortDTO = mapper . Map < CohortDTO > ( result ) ;
34+ ResponseDTO < CohortDTO > response = new ResponseDTO < CohortDTO > ( )
35+ {
36+ Message = "Success" ,
37+ Data = cohortDTO
38+ } ;
39+
40+ return TypedResults . Ok ( response ) ;
3241
33- return TypedResults . Ok ( cohortDTOs ) ;
42+ // return TypedResults.Ok(cohortDTOs);
3443 }
3544
3645 [ ProducesResponseType ( StatusCodes . Status200OK ) ]
@@ -46,8 +55,13 @@ public static async Task<IResult> GetAllCohorts(IRepository<Cohort> cohortServic
4655 Console . WriteLine ( results ) ;
4756
4857 IEnumerable < CohortDTO > cohortDTOs = mapper . Map < IEnumerable < CohortDTO > > ( results ) ;
58+ ResponseDTO < IEnumerable < CohortDTO > > response = new ResponseDTO < IEnumerable < CohortDTO > > ( )
59+ {
60+ Message = "Success" ,
61+ Data = cohortDTOs
62+ } ;
4963
50- return TypedResults . Ok ( cohortDTOs ) ;
64+ return TypedResults . Ok ( response ) ;
5165 }
5266
5367 [ ProducesResponseType ( StatusCodes . Status201Created ) ]
@@ -57,6 +71,13 @@ public static async Task<IResult> CreateCohort(
5771 IMapper mapper ,
5872 CreateCohortDTO request )
5973 {
74+
75+ var results = cohortService . GetAllFiltered ( c => c . Title == request . Title ) ;
76+ if ( results != null ) return TypedResults . BadRequest ( new ResponseDTO < object >
77+ {
78+ Message = $ "Cohort with name { request . Title } already exists"
79+ } ) ;
80+
6081 Cohort cohort = new Cohort ( ) { Title = request . Title } ;
6182
6283 string [ ] defaultCourses = { "Software Development" , "Front-End Development" , "Data Analytics" } ;
@@ -84,7 +105,12 @@ public static async Task<IResult> CreateCohort(
84105 cohortService . Insert ( cohort ) ;
85106 cohortService . Save ( ) ;
86107
87- var response = mapper . Map < CohortDTO > ( cohort ) ;
108+ var cohortDTO = mapper . Map < CohortDTO > ( cohort ) ;
109+ ResponseDTO < CohortDTO > response = new ResponseDTO < CohortDTO > ( )
110+ {
111+ Message = "Success" ,
112+ Data = cohortDTO
113+ } ;
88114 return TypedResults . Created ( $ "/api/cohorts/{ cohort . Id } ", response ) ;
89115 }
90116
@@ -102,7 +128,10 @@ public static async Task<IResult> AddUserToCohort(
102128 {
103129 // 1. Get the user
104130 var user = userService . GetById ( userId ) ;
105- if ( user == null ) return Results . NotFound ( $ "User with Id { userId } not found.") ;
131+ if ( user == null ) return TypedResults . BadRequest ( new ResponseDTO < object >
132+ {
133+ Message = $ "User with Id { userId } not found."
134+ } ) ;
106135
107136 // 2. Get the cohort including its users and courses for verification steps
108137 var cohort = cohortService . GetById ( cohortId , q =>
@@ -112,16 +141,25 @@ public static async Task<IResult> AddUserToCohort(
112141 . ThenInclude ( cc => cc . CohortCourseUsers )
113142 . ThenInclude ( ccu => ccu . User ) ) ;
114143
115- if ( cohort == null ) return Results . NotFound ( $ "Cohort with Id { cohortId } not found.") ;
144+ if ( cohort == null ) return TypedResults . BadRequest ( new ResponseDTO < object >
145+ {
146+ Message = $ "Cohort with Id { cohortId } not found."
147+ } ) ;
116148
117149 // 3. Verify that the course exists in this cohort
118150 var cohortCourse = cohort . CohortCourses . FirstOrDefault ( cc => cc . CourseId == courseId ) ;
119151 if ( cohortCourse == null )
120- return Results . BadRequest ( "The specified course is not part of this cohort." ) ;
152+ return TypedResults . BadRequest ( new ResponseDTO < object >
153+ {
154+ Message = "The specified course is not part of this cohort."
155+ } ) ;
121156
122157 // 4. Check if the user is already in this cohort
123158 if ( cohortCourse . CohortCourseUsers . Any ( cu => cu . UserId == userId ) )
124- return Results . BadRequest ( "User is already a member of this cohort." ) ;
159+ return TypedResults . BadRequest ( new ResponseDTO < object >
160+ {
161+ Message = "User is already a member of this cohort."
162+ } ) ;
125163
126164 // 7. Add user to CohortCourseUser
127165 var existingCcu = cohortCourseUserService
@@ -147,10 +185,19 @@ public static async Task<IResult> AddUserToCohort(
147185 cohortCourseUserService . Save ( ) ;
148186
149187 // 9. Map response
150- var response = mapper . Map < CohortCourseUserDTO > ( ccu ) ;
188+ var ccuDTO = mapper . Map < CohortCourseUserDTO > ( ccu ) ;
189+ ResponseDTO < CohortCourseUserDTO > response = new ResponseDTO < CohortCourseUserDTO > ( )
190+ {
191+ Message = "Success" ,
192+ Data = ccuDTO
193+ } ;
194+
151195 return TypedResults . Ok ( response ) ;
152196 }
153- return TypedResults . BadRequest ( ) ;
197+ return TypedResults . BadRequest ( new ResponseDTO < object >
198+ {
199+ Message = "Failed to add user."
200+ } ) ;
154201 }
155202
156203 [ ProducesResponseType ( StatusCodes . Status200OK ) ]
@@ -167,7 +214,10 @@ public static async Task<IResult> DeleteUserFromCohort(
167214 {
168215 // 1. Get the user
169216 var user = userService . GetById ( userId ) ;
170- if ( user == null ) return Results . NotFound ( $ "User with Id { userId } not found.") ;
217+ if ( user == null ) return TypedResults . BadRequest ( new ResponseDTO < object >
218+ {
219+ Message = $ "User with Id { userId } not found."
220+ } ) ;
171221
172222 // 2. Get the cohort including its users and courses for verification steps
173223 var cohort = cohortService . GetById ( cohortId , q =>
@@ -177,16 +227,25 @@ public static async Task<IResult> DeleteUserFromCohort(
177227 . ThenInclude ( cc => cc . CohortCourseUsers )
178228 . ThenInclude ( ccu => ccu . User ) ) ;
179229
180- if ( cohort == null ) return Results . NotFound ( $ "Cohort with Id { cohortId } not found.") ;
230+ if ( cohort == null ) return TypedResults . BadRequest ( new ResponseDTO < object >
231+ {
232+ Message = $ "Cohort with Id { cohortId } not found."
233+ } ) ;
181234
182235 // 3. Verify that the course exists in this cohort
183236 var cohortCourse = cohort . CohortCourses . FirstOrDefault ( cc => cc . CourseId == courseId ) ;
184237 if ( cohortCourse == null )
185- return Results . BadRequest ( "The specified course is not part of this cohort." ) ;
238+ return TypedResults . BadRequest ( new ResponseDTO < object >
239+ {
240+ Message = "The specified course is not part of this cohort."
241+ } ) ;
186242
187243 // 4. Check if the user is already in this cohort
188244 if ( ! cohortCourse . CohortCourseUsers . Any ( cu => cu . UserId == userId ) )
189- return Results . BadRequest ( "User is not a member of this cohort." ) ;
245+ return TypedResults . BadRequest ( new ResponseDTO < object >
246+ {
247+ Message = "User is not a member of this cohort."
248+ } ) ;
190249
191250 // 7. Add user to CohortCourseUser
192251 var existingCcu = cohortCourseUserService
@@ -212,10 +271,19 @@ public static async Task<IResult> DeleteUserFromCohort(
212271 cohortCourseUserService . Save ( ) ;
213272
214273 // 9. Map response
215- var response = mapper . Map < CohortCourseUserDTO > ( existingCcu ) ;
274+ var ccuDTO = mapper . Map < CohortCourseUserDTO > ( existingCcu ) ;
275+ ResponseDTO < CohortCourseUserDTO > response = new ResponseDTO < CohortCourseUserDTO > ( )
276+ {
277+ Message = "Success" ,
278+ Data = ccuDTO
279+ } ;
280+
216281 return TypedResults . Ok ( response ) ;
217282 }
218- return TypedResults . BadRequest ( ) ;
283+ return TypedResults . BadRequest ( new ResponseDTO < object >
284+ {
285+ Message = "Failed to delete user."
286+ } ) ;
219287
220288 }
221289 }
0 commit comments