11using exercise . wwwapi . DTOs ;
2+ using exercise . wwwapi . DTOs . Courses ;
23using exercise . wwwapi . Enums ;
34using exercise . wwwapi . Models ;
45using exercise . wwwapi . Repository ;
@@ -14,6 +15,10 @@ public static void ConfigureCohortEndpoints(this WebApplication app)
1415 {
1516 var cohorts = app . MapGroup ( "cohorts" ) ;
1617 cohorts . MapPost ( "/" , CreateCohort ) . WithSummary ( "Create a cohort" ) ;
18+ cohorts . MapGet ( "/" , GetAllCohorts ) . WithSummary ( "Get all cohorts" ) ;
19+ cohorts . MapGet ( "/{id}" , GetCohortById ) . WithSummary ( "Get cohort by id" ) ;
20+ cohorts . MapPatch ( "/{id}" , UpdateCohortById ) . WithSummary ( "Update cohort" ) ;
21+ cohorts . MapDelete ( "/{id}" , DeleteCohortById ) . WithSummary ( "Delete cohort" ) ;
1722 }
1823 [ ProducesResponseType ( StatusCodes . Status201Created ) ]
1924 [ ProducesResponseType ( StatusCodes . Status400BadRequest ) ]
@@ -67,5 +72,99 @@ public static async Task<IResult> CreateCohort(IRepository<Cohort> cohortRepo, C
6772
6873 return TypedResults . Created ( $ "/cohorts/{ newCohortNumber } ") ;
6974 }
75+ [ ProducesResponseType ( StatusCodes . Status200OK ) ]
76+ public static async Task < IResult > GetAllCohorts ( IRepository < Cohort > cohortRepo )
77+ {
78+ // Use GetWithIncludes to include CohortCourses and their Course
79+ var cohorts = await cohortRepo . GetWithIncludes ( q =>
80+ q . Include ( c => c . CohortCourses )
81+ . ThenInclude ( cc => cc . Course )
82+ ) ;
83+
84+ var cohortDTOs = cohorts . Select ( c => new CohortDTO ( c ) ) . ToList ( ) ;
85+
86+ var response = new ResponseDTO < List < CohortDTO > > ( )
87+ {
88+ Status = "success" ,
89+ Data = cohortDTOs
90+ } ;
91+ return TypedResults . Ok ( response ) ;
92+ }
93+ [ ProducesResponseType ( StatusCodes . Status200OK ) ]
94+
95+ public static async Task < IResult > GetCohortById ( IRepository < Cohort > cohortRepo , int id )
96+ {
97+ // uses GetByIdWithIncludes for nested includes
98+ var cohort = await cohortRepo . GetByIdWithIncludes ( q =>
99+ q . Include ( c => c . CohortCourses )
100+ . ThenInclude ( cc => cc . Course ) , id ) ;
101+
102+ if ( cohort == null )
103+ {
104+ return TypedResults . NotFound ( ) ;
105+ }
106+
107+ var cohortDTO = new CohortDTO ( cohort ) ;
108+
109+ var response = new ResponseDTO < CohortDTO >
110+ {
111+ Status = "success" ,
112+ Data = cohortDTO
113+ } ;
114+
115+ return TypedResults . Ok ( response ) ;
116+ }
117+ [ ProducesResponseType ( StatusCodes . Status200OK ) ]
118+ [ ProducesResponseType ( StatusCodes . Status400BadRequest ) ]
119+ [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
120+ public static async Task < IResult > UpdateCohortById ( IRepository < Cohort > cohortRepo , int id , CohortPostDTO updateDto )
121+ {
122+ var cohort = await cohortRepo . GetByIdAsync ( id ) ;
123+ if ( cohort == null )
124+ {
125+ return TypedResults . NotFound ( ) ;
126+ }
127+
128+ if ( ! string . IsNullOrWhiteSpace ( updateDto . CohortName ) )
129+ cohort . CohortName = updateDto . CohortName ;
130+ if ( updateDto . StartDate != DateTime . MinValue )
131+ cohort . StartDate = updateDto . StartDate ;
132+ if ( updateDto . EndDate != DateTime . MinValue )
133+ cohort . EndDate = updateDto . EndDate ;
134+
135+ cohortRepo . Update ( cohort ) ;
136+ await cohortRepo . SaveAsync ( ) ;
137+
138+ var cohortDTO = new CohortDTO
139+ {
140+ CohortNumber = cohort . CohortNumber ,
141+ CohortName = cohort . CohortName ,
142+ StartDate = cohort . StartDate ,
143+ EndDate = cohort . EndDate
144+ } ;
145+
146+ var response = new ResponseDTO < CohortDTO >
147+ {
148+ Status = "success" ,
149+ Data = cohortDTO
150+ } ;
151+
152+ return TypedResults . Ok ( response ) ;
153+ }
154+ [ ProducesResponseType ( StatusCodes . Status200OK ) ]
155+ [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
156+ public static async Task < IResult > DeleteCohortById ( IRepository < Cohort > cohortRepo , int id )
157+ {
158+ var cohort = await cohortRepo . GetByIdAsync ( id ) ;
159+ if ( cohort == null )
160+ {
161+ return TypedResults . NotFound ( ) ;
162+ }
163+
164+ cohortRepo . Delete ( cohort ) ;
165+ await cohortRepo . SaveAsync ( ) ;
166+
167+ return TypedResults . Ok ( new { Status = "success" , Data = $ "Cohort with id { id } deleted" } ) ;
168+ }
70169
71170}
0 commit comments