@@ -17,22 +17,109 @@ public static void ConfigureCourseEndpoints(this WebApplication app)
1717 var courses = app . MapGroup ( "courses" ) ;
1818 courses . MapGet ( "/" , GetCourses ) . WithSummary ( "Returns all courses" ) ;
1919 courses . MapGet ( "/{id}" , GetCourseById ) . WithSummary ( "Returns course with provided id" ) ;
20- }
20+ courses . MapPost ( "/" , CreateCourse ) . WithSummary ( "Create a new course" ) ;
21+ courses . MapDelete ( "/{id}" , DeleteCourseById ) . WithSummary ( "Delete a course" ) ;
22+ courses . MapPut ( "/{id}" , UpdateCourse ) . WithSummary ( "Update a course name" ) ;
23+ }
2124
2225
2326 [ ProducesResponseType ( StatusCodes . Status200OK ) ]
27+ [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
2428 private static async Task < IResult > GetCourses ( IRepository < Course > repository , ClaimsPrincipal claimsPrincipal )
2529 {
2630 var response = await repository . GetWithIncludes ( c => c . Include ( a => a . CourseModules ) . ThenInclude ( b => b . Module ) . ThenInclude ( d => d . Units ) . ThenInclude ( u => u . Exercises ) ) ;
31+ if ( response == null || response . Count == 0 )
32+ {
33+ return TypedResults . NotFound ( "No courses found" ) ;
34+ }
2735 var result = response . Select ( c => new GetCourseDTO ( c ) ) ;
2836 return TypedResults . Ok ( result ) ;
2937 }
3038
3139 [ ProducesResponseType ( StatusCodes . Status200OK ) ]
32- private static async Task < IResult > GetCourseById ( IRepository < Course > repository , int id )
40+ [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
41+ private static async Task < IResult > GetCourseById ( IRepository < Course > repository , int id , ClaimsPrincipal claimsPrincipal )
3342 {
3443 var response = await repository . GetByIdWithIncludes ( c => c . Include ( a => a . CourseModules ) . ThenInclude ( b => b . Module ) . ThenInclude ( d => d . Units ) . ThenInclude ( u => u . Exercises ) , id ) ;
44+ if ( response == null )
45+ {
46+ return TypedResults . NotFound ( "No course with the given id was found" ) ;
47+ }
3548 var result = new GetCourseDTO ( response ) ;
3649 return TypedResults . Ok ( result ) ;
3750 }
38- }
51+
52+ [ ProducesResponseType ( StatusCodes . Status201Created ) ]
53+ [ ProducesResponseType ( StatusCodes . Status400BadRequest ) ]
54+ [ ProducesResponseType ( StatusCodes . Status401Unauthorized ) ]
55+ private static async Task < IResult > CreateCourse ( IRepository < Course > repository , CoursePostDTO postedCourse , ClaimsPrincipal claimsPrincipal )
56+ {
57+
58+ if ( claimsPrincipal . IsInRole ( "Teacher" ) == false )
59+ {
60+ return TypedResults . Unauthorized ( ) ;
61+ }
62+ if ( postedCourse == null || postedCourse . Name == null || postedCourse . Name == "" )
63+ {
64+ return TypedResults . BadRequest ( "Course data missing in request" ) ;
65+ }
66+
67+ Course newCourse = new Course { Name = postedCourse . Name } ;
68+ repository . Insert ( newCourse ) ;
69+ await repository . SaveAsync ( ) ;
70+ GetCourseDTO response = new GetCourseDTO ( newCourse ) ;
71+
72+ return TypedResults . Created ( $ "/courses/{ newCourse . Id } ", response ) ;
73+ }
74+
75+ [ ProducesResponseType ( StatusCodes . Status204NoContent ) ]
76+ [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
77+ [ ProducesResponseType ( StatusCodes . Status401Unauthorized ) ]
78+ private static async Task < IResult > DeleteCourseById ( IRepository < Course > repository , int id , ClaimsPrincipal claimsPrincipal )
79+ {
80+ if ( claimsPrincipal . IsInRole ( "Teacher" ) == false )
81+ {
82+ return TypedResults . Unauthorized ( ) ;
83+ }
84+
85+ Course ? course = await repository . GetByIdAsync ( id ) ;
86+ if ( course == null )
87+ {
88+ return TypedResults . NotFound ( $ "No course with the given id: { id } was found") ;
89+ }
90+ repository . Delete ( course ) ;
91+ await repository . SaveAsync ( ) ;
92+
93+ return TypedResults . NoContent ( ) ;
94+ }
95+
96+ [ ProducesResponseType ( StatusCodes . Status200OK ) ]
97+ [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
98+ [ ProducesResponseType ( StatusCodes . Status400BadRequest ) ]
99+ [ ProducesResponseType ( StatusCodes . Status401Unauthorized ) ]
100+ private static async Task < IResult > UpdateCourse ( IRepository < Course > repository , int id , CoursePostDTO updatedCourse , ClaimsPrincipal claimsPrincipal )
101+ {
102+ if ( claimsPrincipal . IsInRole ( "Teacher" ) == false )
103+ {
104+ return TypedResults . Unauthorized ( ) ;
105+ }
106+
107+ Course ? course = await repository . GetByIdAsync ( id ) ;
108+ if ( course == null )
109+ {
110+ return TypedResults . NotFound ( $ "No course with the given id: { id } was found") ;
111+ }
112+ if ( updatedCourse == null || updatedCourse . Name == null || updatedCourse . Name == "" )
113+ {
114+ return TypedResults . BadRequest ( "Missing update data in request" ) ;
115+ }
116+ course . Name = updatedCourse . Name ;
117+ repository . Update ( course ) ;
118+ await repository . SaveAsync ( ) ;
119+
120+ GetCourseDTO response = new GetCourseDTO ( course ) ;
121+
122+ return TypedResults . Ok ( response ) ;
123+ }
124+
125+ }
0 commit comments