44using exercise . wwwapi . DTOs . Posts ;
55using exercise . wwwapi . Models ;
66using exercise . wwwapi . Repository ;
7+ using Microsoft . AspNetCore . Authorization ;
78using Microsoft . AspNetCore . Mvc ;
89using Microsoft . EntityFrameworkCore ;
910using System . Linq ;
@@ -18,11 +19,13 @@ public static void ConfigureCohortEndpoints(this WebApplication app)
1819 cohorts . MapPost ( "/" , CreateCohort ) . WithSummary ( "Create a cohort" ) ;
1920 cohorts . MapGet ( "/" , GetAllCohorts ) . WithSummary ( "Get all cohorts" ) ;
2021 cohorts . MapGet ( "/cohortId/{cohortId}" , GetCohort ) . WithSummary ( "Get a cohort by ID" ) ;
21- // cohorts.MapGet("/{userId}", GetCohortByUserId).WithSummary("");
22+ cohorts . MapGet ( "/userId/ {userId}" , GetCohortByUserId ) . WithSummary ( "Get all cohorts a user is in by its Id " ) ;
2223 cohorts . MapPost ( "/cohortId/{cohortId}/userId/{userId}/courseId/{courseId}" , AddUserToCohort ) . WithSummary ( "Add a user to a cohort" ) ;
2324 cohorts . MapDelete ( "/cohortId/{cohortId}/userId/{userId}/courseId/{courseId}" , DeleteUserFromCohort ) . WithSummary ( "Delete a user from a cohort" ) ;
2425 }
2526
27+ [ Authorize ]
28+ [ ProducesResponseType ( StatusCodes . Status400BadRequest ) ]
2629 [ ProducesResponseType ( StatusCodes . Status200OK ) ]
2730 public static async Task < IResult > GetCohort ( IRepository < Cohort > service , IMapper mapper , int cohortId )
2831 {
@@ -32,6 +35,14 @@ public static async Task<IResult> GetCohort(IRepository<Cohort> service, IMapper
3235 . Include ( c => c . CohortCourses )
3336 . ThenInclude ( cc => cc . CohortCourseUsers )
3437 . ThenInclude ( ccu => ccu . User ) ) ;
38+
39+ if ( result == null )
40+ return TypedResults . BadRequest ( new ResponseDTO < object >
41+ {
42+ Message = $ "Cohort with id '{ cohortId } ' does not exists",
43+ Data = result
44+ } ) ;
45+
3546 CohortDTO cohortDTO = mapper . Map < CohortDTO > ( result ) ;
3647 ResponseDTO < CohortDTO > response = new ResponseDTO < CohortDTO > ( )
3748 {
@@ -44,6 +55,37 @@ public static async Task<IResult> GetCohort(IRepository<Cohort> service, IMapper
4455 //return TypedResults.Ok(cohortDTOs);
4556 }
4657
58+ [ Authorize ]
59+ [ ProducesResponseType ( StatusCodes . Status400BadRequest ) ]
60+ [ ProducesResponseType ( StatusCodes . Status200OK ) ]
61+ public static async Task < IResult > GetCohortByUserId ( IRepository < Cohort > cohortRepo , IMapper mapper , int userId )
62+ {
63+ var results = cohortRepo . GetWithIncludes ( q => q
64+ . Include ( c => c . CohortCourses )
65+ . ThenInclude ( cc => cc . Course )
66+ . Include ( c => c . CohortCourses )
67+ . ThenInclude ( cc => cc . CohortCourseUsers )
68+ . ThenInclude ( ccu => ccu . User )
69+ ) . Where ( r => r . CohortCourses . Any ( cc => cc . CohortCourseUsers . Any ( ccu => ccu . UserId == userId ) ) ) ;
70+
71+ if ( ! results . Any ( ) )
72+ return TypedResults . BadRequest ( new ResponseDTO < object >
73+ {
74+ Message = $ "User with id { userId } either does not exist, or is not registered in any cohorts",
75+ Data = results
76+ } ) ;
77+
78+ IEnumerable < CohortDTO > cohortDTOs = mapper . Map < IEnumerable < CohortDTO > > ( results ) ;
79+ ResponseDTO < IEnumerable < CohortDTO > > response = new ResponseDTO < IEnumerable < CohortDTO > > ( )
80+ {
81+ Message = "Success" ,
82+ Data = cohortDTOs
83+ } ;
84+
85+ return TypedResults . Ok ( response ) ;
86+ }
87+
88+ [ Authorize ]
4789 [ ProducesResponseType ( StatusCodes . Status200OK ) ]
4890 public static async Task < IResult > GetAllCohorts ( IRepository < Cohort > cohortService , IMapper mapper )
4991 {
@@ -54,7 +96,6 @@ public static async Task<IResult> GetAllCohorts(IRepository<Cohort> cohortServic
5496 . ThenInclude ( cc => cc . CohortCourseUsers )
5597 . ThenInclude ( ccu => ccu . User )
5698 ) ;
57- Console . WriteLine ( results ) ;
5899
59100 IEnumerable < CohortDTO > cohortDTOs = mapper . Map < IEnumerable < CohortDTO > > ( results ) ;
60101 ResponseDTO < IEnumerable < CohortDTO > > response = new ResponseDTO < IEnumerable < CohortDTO > > ( )
@@ -66,6 +107,8 @@ public static async Task<IResult> GetAllCohorts(IRepository<Cohort> cohortServic
66107 return TypedResults . Ok ( response ) ;
67108 }
68109
110+ [ Authorize ]
111+ [ ProducesResponseType ( StatusCodes . Status400BadRequest ) ]
69112 [ ProducesResponseType ( StatusCodes . Status201Created ) ]
70113 public static async Task < IResult > CreateCohort (
71114 IRepository < Cohort > cohortService ,
@@ -119,6 +162,7 @@ public static async Task<IResult> CreateCohort(
119162 return TypedResults . Created ( $ "/api/cohorts/{ cohort . Id } ", response ) ;
120163 }
121164
165+ [ Authorize ]
122166 [ ProducesResponseType ( StatusCodes . Status200OK ) ]
123167 [ ProducesResponseType ( StatusCodes . Status400BadRequest ) ]
124168 public static async Task < IResult > AddUserToCohort (
@@ -207,6 +251,7 @@ public static async Task<IResult> AddUserToCohort(
207251 } ) ;
208252 }
209253
254+ [ Authorize ]
210255 [ ProducesResponseType ( StatusCodes . Status200OK ) ]
211256 [ ProducesResponseType ( StatusCodes . Status400BadRequest ) ]
212257 public static async Task < IResult > DeleteUserFromCohort (
0 commit comments