1+ using exercise . wwwapi . DTOs ;
2+ using exercise . wwwapi . DTOs . CohortCourse ;
3+ using exercise . wwwapi . DTOs . Courses ;
4+ using exercise . wwwapi . DTOs . Exercises ;
5+ using exercise . wwwapi . Enums ;
6+ using exercise . wwwapi . Models ;
7+ using exercise . wwwapi . Repository ;
8+ using Microsoft . AspNetCore . Mvc ;
9+ using Microsoft . EntityFrameworkCore ;
10+ using Npgsql ;
11+
12+ namespace exercise . wwwapi . Endpoints ;
13+
14+ public static class CohortCourseEndpoints
15+ {
16+ public static void ConfigureCohortCourseEndpoints ( this WebApplication app )
17+ {
18+ var cohortcourses = app . MapGroup ( "cohortcourses" ) ;
19+ cohortcourses . MapGet ( "/" , GetAllCohortCourses ) . WithSummary ( "Get all cohort_courses" ) ;
20+ cohortcourses . MapGet ( "/{id}" , GetCohortCourseById ) . WithSummary ( "Get cohort_course by id" ) ;
21+ }
22+
23+ [ ProducesResponseType ( StatusCodes . Status200OK ) ]
24+ [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
25+ public static async Task < IResult > GetAllCohortCourses ( IRepository < CohortCourse > cohortCourseRepository )
26+ {
27+ var response = await cohortCourseRepository . GetWithIncludes ( a => a
28+ . Include ( b => b . Cohort )
29+ . Include ( c => c . Course )
30+ . Include ( d => d . UserCCs )
31+ . ThenInclude ( e => e . User ) ) ;
32+
33+ var result = response . Select ( cc => new GetCohortCourseDTO ( cc ) ) . ToList ( ) ;
34+
35+ return TypedResults . Ok ( result ) ;
36+
37+
38+ }
39+ [ ProducesResponseType ( StatusCodes . Status200OK ) ]
40+ [ ProducesResponseType ( StatusCodes . Status400BadRequest ) ]
41+ [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
42+ public static async Task < IResult > GetCohortCourseById ( IRepository < CohortCourse > cohortCourseRepository , int id )
43+ {
44+ var response = await cohortCourseRepository . GetByIdWithIncludes ( a => a
45+ . Include ( b => b . Cohort )
46+ . Include ( c => c . Course )
47+ . Include ( d => d . UserCCs )
48+ . ThenInclude ( e => e . User ) , id ) ;
49+
50+ if ( response == null ) return TypedResults . NotFound ( "No cohort_course with that id exists" ) ;
51+
52+ var result = new GetCohortCourseDTO ( response ) ;
53+
54+ return TypedResults . Ok ( result ) ;
55+ }
56+
57+
58+ }
0 commit comments