@@ -17,8 +17,8 @@ public static void ConfigureCohortEndpoints(this WebApplication app)
1717 var cohorts = app . MapGroup ( "cohorts" ) ;
1818 cohorts . MapPost ( "/" , CreateCohort ) . WithSummary ( "Create a cohort" ) ;
1919 cohorts . MapGet ( "/" , GetAllCohorts ) . WithSummary ( "Get all cohorts" ) ;
20- cohorts . MapGet ( "/{id}" , GetCohort ) . WithSummary ( "Get a cohort by ID" ) ;
21- // cohorts.MapGet("/{userId}", GetCohortByUserId).WithSummary("");
20+ cohorts . MapGet ( "/cohortId/ {id}" , GetCohort ) . WithSummary ( "Get a cohort by ID" ) ;
21+ cohorts . MapGet ( "/userId/ {userId}" , GetCohortByUserId ) . WithSummary ( "Get all cohorts a user is in by its Id " ) ;
2222 cohorts . MapPost ( "/{cohortId}/{userId}/{courseId}" , AddUserToCohort ) . WithSummary ( "Add a user to a cohort" ) ;
2323 cohorts . MapDelete ( "/{cohortId}/{userId}/{courseId}" , DeleteUserFromCohort ) . WithSummary ( "Delete a user from a cohort" ) ;
2424 }
@@ -32,6 +32,14 @@ public static async Task<IResult> GetCohort(IRepository<Cohort> service, IMapper
3232 . Include ( c => c . CohortCourses )
3333 . ThenInclude ( cc => cc . CohortCourseUsers )
3434 . ThenInclude ( ccu => ccu . User ) ) ;
35+
36+ if ( result == null )
37+ return TypedResults . BadRequest ( new ResponseDTO < object >
38+ {
39+ Message = $ "Cohort with id '{ cohortId } ' does not exists",
40+ Data = result
41+ } ) ;
42+
3543 CohortDTO cohortDTO = mapper . Map < CohortDTO > ( result ) ;
3644 ResponseDTO < CohortDTO > response = new ResponseDTO < CohortDTO > ( )
3745 {
@@ -44,6 +52,35 @@ public static async Task<IResult> GetCohort(IRepository<Cohort> service, IMapper
4452 //return TypedResults.Ok(cohortDTOs);
4553 }
4654
55+
56+ [ ProducesResponseType ( StatusCodes . Status200OK ) ]
57+ public static async Task < IResult > GetCohortByUserId ( IRepository < Cohort > cohortRepo , IMapper mapper , int userId )
58+ {
59+ var results = cohortRepo . GetWithIncludes ( q => q
60+ . Include ( c => c . CohortCourses )
61+ . ThenInclude ( cc => cc . Course )
62+ . Include ( c => c . CohortCourses )
63+ . ThenInclude ( cc => cc . CohortCourseUsers )
64+ . ThenInclude ( ccu => ccu . User )
65+ ) . Where ( r => r . CohortCourses . Any ( cc => cc . CohortCourseUsers . Any ( ccu => ccu . UserId == userId ) ) ) ;
66+
67+ if ( ! results . Any ( ) )
68+ return TypedResults . BadRequest ( new ResponseDTO < object >
69+ {
70+ Message = $ "User with id { userId } either does not exist, or is not registered in any cohorts",
71+ Data = results
72+ } ) ;
73+
74+ IEnumerable < CohortDTO > cohortDTOs = mapper . Map < IEnumerable < CohortDTO > > ( results ) ;
75+ ResponseDTO < IEnumerable < CohortDTO > > response = new ResponseDTO < IEnumerable < CohortDTO > > ( )
76+ {
77+ Message = "Success" ,
78+ Data = cohortDTOs
79+ } ;
80+
81+ return TypedResults . Ok ( response ) ;
82+ }
83+
4784 [ ProducesResponseType ( StatusCodes . Status200OK ) ]
4885 public static async Task < IResult > GetAllCohorts ( IRepository < Cohort > cohortService , IMapper mapper )
4986 {
@@ -54,7 +91,6 @@ public static async Task<IResult> GetAllCohorts(IRepository<Cohort> cohortServic
5491 . ThenInclude ( cc => cc . CohortCourseUsers )
5592 . ThenInclude ( ccu => ccu . User )
5693 ) ;
57- Console . WriteLine ( results ) ;
5894
5995 IEnumerable < CohortDTO > cohortDTOs = mapper . Map < IEnumerable < CohortDTO > > ( results ) ;
6096 ResponseDTO < IEnumerable < CohortDTO > > response = new ResponseDTO < IEnumerable < CohortDTO > > ( )
0 commit comments