@@ -41,10 +41,17 @@ public static void ConfigureExerciseEndpoints(this WebApplication app)
4141 units . MapGet ( "/" , GetUnits ) . WithSummary ( "Returns all units" ) ;
4242 units . MapGet ( "/{id}" , GetUnitById ) . WithSummary ( "Returns unit with provided id" ) ;
4343 units . MapPost ( "/{id}" , CreateExerciseInUnit ) . WithSummary ( "Create an exercise in the given unit" ) ;
44+ units . MapDelete ( "/{id}" , DeleteUnit ) . WithSummary ( "Deletes unit with provided id" ) ;
45+ units . MapPut ( "/{id}" , UpdateUnit ) . WithSummary ( "Update unit with provided id" ) ;
4446
4547 var modules = app . MapGroup ( "modules" ) ;
4648 modules . MapGet ( "/" , GetModules ) . WithSummary ( "Returns all modules" ) ;
4749 modules . MapGet ( "/{id}" , GetModuleById ) . WithSummary ( "Returns module with provided id" ) ;
50+ modules . MapPost ( "/" , CreateModule ) . WithSummary ( "Create a new module" ) ;
51+ modules . MapPut ( "/{id}" , UpdateModule ) . WithSummary ( "Update a module with provided id" ) ;
52+ modules . MapDelete ( "/{id}" , DeleteModule ) . WithSummary ( "Delete a module with provided id" ) ;
53+ modules . MapPost ( "/{id}" , CreateUnitInModule ) . WithSummary ( "Create a unit in the given module" ) ;
54+ //TODO: Add MapPost to modules which creates a new Unit in the module
4855
4956
5057 }
@@ -57,13 +64,114 @@ private static async Task<IResult> GetModules(IRepository<Module> moduleReposito
5764 return TypedResults . Ok ( result ) ;
5865 }
5966
67+ [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
6068 [ ProducesResponseType ( StatusCodes . Status200OK ) ]
6169 private static async Task < IResult > GetModuleById ( IRepository < Module > moduleRepository , int id )
6270 {
6371 var response = await moduleRepository . GetByIdWithIncludes ( a => a . Include ( u => u . Units ) . ThenInclude ( u => u . Exercises ) , id ) ;
72+ if ( response == null )
73+ {
74+ return TypedResults . NotFound ( ) ;
75+ }
76+
77+ var result = new GetModuleDTO ( response ) ;
78+ return TypedResults . Ok ( result ) ;
79+ }
80+
81+ [ ProducesResponseType ( StatusCodes . Status200OK ) ]
82+ [ ProducesResponseType ( StatusCodes . Status401Unauthorized ) ]
83+ private static async Task < IResult > CreateModule ( IRepository < Module > moduleRepository , ClaimsPrincipal claimsPrincipal , string moduleTitle )
84+ {
85+ var authorized = claimsPrincipal . IsInRole ( "Teacher" ) ;
86+ if ( ! authorized )
87+ {
88+ return TypedResults . Unauthorized ( ) ;
89+ }
90+
91+ var newModule = new Module { Title = moduleTitle } ;
92+
93+ moduleRepository . Insert ( newModule ) ;
94+ await moduleRepository . SaveAsync ( ) ;
95+
96+ var result = new GetModuleDTO ( newModule ) ;
97+ return TypedResults . Ok ( result ) ;
98+ }
99+
100+ [ ProducesResponseType ( StatusCodes . Status200OK ) ]
101+ [ ProducesResponseType ( StatusCodes . Status401Unauthorized ) ]
102+ [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
103+ private static async Task < IResult > UpdateModule ( IRepository < Module > moduleRepository , ClaimsPrincipal claimsPrincipal , int id , string moduleTitle )
104+ {
105+ var authorized = claimsPrincipal . IsInRole ( "Teacher" ) ;
106+ if ( ! authorized )
107+ {
108+ return TypedResults . Unauthorized ( ) ;
109+ }
110+
111+ var response = await moduleRepository . GetByIdWithIncludes ( a => a . Include ( u => u . Units ) . ThenInclude ( u => u . Exercises ) , id ) ;
112+ if ( response == null )
113+ {
114+ return TypedResults . NotFound ( ) ;
115+ }
116+
117+ response . Title = moduleTitle ;
118+ moduleRepository . Update ( response ) ;
119+ await moduleRepository . SaveAsync ( ) ;
120+
64121 var result = new GetModuleDTO ( response ) ;
65122 return TypedResults . Ok ( result ) ;
66123 }
124+ // TODO: Create a DTO to make it easier to later change what module might hold/need when creating/editing a module
125+
126+ [ ProducesResponseType ( StatusCodes . Status200OK ) ]
127+ [ ProducesResponseType ( StatusCodes . Status401Unauthorized ) ]
128+ [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
129+ private static async Task < IResult > DeleteModule ( IRepository < Module > moduleRepository , ClaimsPrincipal claimsPrincipal , int id )
130+ {
131+ var authorized = claimsPrincipal . IsInRole ( "Teacher" ) ;
132+ if ( ! authorized )
133+ {
134+ return TypedResults . Unauthorized ( ) ;
135+ }
136+
137+ var response = await moduleRepository . GetByIdWithIncludes ( a => a . Include ( u => u . Units ) . ThenInclude ( u => u . Exercises ) , id ) ;
138+ if ( response == null )
139+ {
140+ return TypedResults . NotFound ( ) ;
141+ }
142+
143+ moduleRepository . Delete ( response ) ;
144+ await moduleRepository . SaveAsync ( ) ;
145+
146+ var result = new GetModuleDTO ( response ) ;
147+ return TypedResults . Ok ( result ) ;
148+ }
149+
150+ [ ProducesResponseType ( StatusCodes . Status200OK ) ]
151+ [ ProducesResponseType ( StatusCodes . Status401Unauthorized ) ]
152+ [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
153+ private static async Task < IResult > CreateUnitInModule ( IRepository < Module > moduleRepository , IRepository < Unit > unitRepository , ClaimsPrincipal claimsPrincipal , int id , string name )
154+ {
155+ var authorized = claimsPrincipal . IsInRole ( "Teacher" ) ;
156+ if ( ! authorized )
157+ {
158+ return TypedResults . Unauthorized ( ) ;
159+ }
160+
161+ var module = await moduleRepository . GetByIdWithIncludes ( a => a . Include ( u => u . Units ) . ThenInclude ( u => u . Exercises ) , id ) ;
162+ if ( module == null )
163+ {
164+ return TypedResults . NotFound ( ) ;
165+ }
166+
167+ var newUnit = new Unit { ModuleId = id , Name = name , Module = module } ;
168+
169+ unitRepository . Insert ( newUnit ) ;
170+ await unitRepository . SaveAsync ( ) ;
171+
172+ var result = new GetUnitDTO ( newUnit ) ;
173+ return TypedResults . Ok ( result ) ;
174+ }
67175
68176 [ ProducesResponseType ( StatusCodes . Status200OK ) ]
69177 private static async Task < IResult > GetUnits ( IRepository < Unit > unitRepository , ClaimsPrincipal claimsPrincipal )
@@ -73,57 +181,127 @@ private static async Task<IResult> GetUnits(IRepository<Unit> unitRepository, Cl
73181 return TypedResults . Ok ( result ) ;
74182 }
75183
184+ [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
76185 [ ProducesResponseType ( StatusCodes . Status200OK ) ]
77186 private static async Task < IResult > GetUnitById ( IRepository < Unit > unitRepository , int id )
78187 {
79188 var response = await unitRepository . GetByIdWithIncludes ( a => a . Include ( u => u . Exercises ) , id ) ;
189+ if ( response == null )
190+ {
191+ return TypedResults . NotFound ( ) ;
192+ }
80193 var result = new GetUnitDTO ( response ) ;
81194 return TypedResults . Ok ( result ) ;
82195 }
83196
84-
85197 [ ProducesResponseType ( StatusCodes . Status200OK ) ]
86- private static async Task < IResult > GetExercises ( IRepository < Exercise > exerciseRepository , ClaimsPrincipal claimsPrincipal )
198+ [ ProducesResponseType ( StatusCodes . Status401Unauthorized ) ]
199+ [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
200+ private static async Task < IResult > DeleteUnit ( IRepository < Unit > unitRepository , ClaimsPrincipal claimsPrincipal , int id )
87201 {
88- var response = await exerciseRepository . GetWithIncludes ( null ) ;
89- var result = response . Select ( e => new Exercise_noUnit ( e ) ) ;
202+ var authorized = claimsPrincipal . IsInRole ( "Teacher" ) ;
203+ if ( ! authorized )
204+ {
205+ return TypedResults . Unauthorized ( ) ;
206+ }
207+
208+ var response = await unitRepository . GetByIdWithIncludes ( a => a . Include ( u => u . Exercises ) , id ) ;
209+ if ( response == null )
210+ {
211+ return TypedResults . NotFound ( ) ;
212+ }
213+
214+ unitRepository . Delete ( response ) ;
215+ await unitRepository . SaveAsync ( ) ;
216+
217+ var result = new GetUnitDTO ( response ) ;
90218 return TypedResults . Ok ( result ) ;
91219 }
220+
92221 [ ProducesResponseType ( StatusCodes . Status200OK ) ]
93- private static async Task < IResult > GetExerciseById ( IRepository < Exercise > exerciseRepository , int id )
222+ [ ProducesResponseType ( StatusCodes . Status401Unauthorized ) ]
223+ [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
224+ private static async Task < IResult > UpdateUnit ( IRepository < Unit > unitRepository , ClaimsPrincipal claimsPrincipal , int id , string name )
94225 {
95- var response = await exerciseRepository . GetByIdWithIncludes ( null , id ) ;
96- var result = new Exercise_noUnit ( response ) ;
226+ var authorized = claimsPrincipal . IsInRole ( "Teacher" ) ;
227+ if ( ! authorized )
228+ {
229+ return TypedResults . Unauthorized ( ) ;
230+ }
231+
232+ var response = await unitRepository . GetByIdWithIncludes ( a => a . Include ( u => u . Exercises ) , id ) ;
233+ if ( response == null )
234+ {
235+ return TypedResults . NotFound ( ) ;
236+ }
237+ response . Name = name ;
238+ unitRepository . Update ( response ) ;
239+ await unitRepository . SaveAsync ( ) ;
240+
241+ var result = new GetUnitDTO ( response ) ;
97242 return TypedResults . Ok ( result ) ;
98243 }
99244
100245 [ ProducesResponseType ( StatusCodes . Status200OK ) ]
101246 [ ProducesResponseType ( StatusCodes . Status401Unauthorized ) ]
102247 [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
103- private static async Task < IResult > DeleteExerciseById ( IRepository < Exercise > exerciseRepository , ClaimsPrincipal claimsPrincipal , int id )
248+ private static async Task < IResult > CreateExerciseInUnit (
249+ IRepository < Exercise > exerciseRepository ,
250+ IRepository < Unit > unitRepository ,
251+ ClaimsPrincipal claimsPrincipal ,
252+ int id ,
253+ UpdateCreateExerciseDTO exerciseDTO )
104254 {
105255 var authorized = claimsPrincipal . IsInRole ( "Teacher" ) ;
106256 if ( ! authorized )
107257 {
108- return Results . Unauthorized ( ) ;
258+ return TypedResults . Unauthorized ( ) ;
109259 }
110260
261+ var unit = await unitRepository . GetByIdWithIncludes ( null , id ) ;
262+ if ( unit == null )
263+ {
264+ return TypedResults . NotFound ( ) ;
265+ }
266+
267+ var newExercise = new Exercise { UnitId = id , Name = exerciseDTO . Name , GitHubLink = exerciseDTO . GitHubLink , Description = exerciseDTO . Description , Unit = unit } ;
268+ exerciseRepository . Insert ( newExercise ) ;
269+ await exerciseRepository . SaveAsync ( ) ;
270+
271+ var result = new Exercise_noUnit ( newExercise ) ;
272+ return TypedResults . Ok ( result ) ;
273+ }
274+
275+ [ ProducesResponseType ( StatusCodes . Status200OK ) ]
276+ private static async Task < IResult > GetExercises ( IRepository < Exercise > exerciseRepository , ClaimsPrincipal claimsPrincipal )
277+ {
278+ var response = await exerciseRepository . GetWithIncludes ( null ) ;
279+ var result = response . Select ( e => new Exercise_noUnit ( e ) ) ;
280+ return TypedResults . Ok ( result ) ;
281+ }
282+
283+ [ ProducesResponseType ( StatusCodes . Status200OK ) ]
284+ [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
285+ private static async Task < IResult > GetExerciseById ( IRepository < Exercise > exerciseRepository , int id )
286+ {
111287 var response = await exerciseRepository . GetByIdWithIncludes ( null , id ) ;
112288 if ( response == null )
113289 {
114290 return TypedResults . NotFound ( ) ;
115291 }
116-
117292 var result = new Exercise_noUnit ( response ) ;
118293 return TypedResults . Ok ( result ) ;
119294 }
120295
121- private static async Task < IResult > UpdateExerciseById ( IRepository < Exercise > exerciseRepository , ClaimsPrincipal claimsPrincipal , int id , UpdateCreateExerciseDTO exerciseDTO )
296+ [ ProducesResponseType ( StatusCodes . Status200OK ) ]
297+ [ ProducesResponseType ( StatusCodes . Status401Unauthorized ) ]
298+ [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
299+ private static async Task < IResult > DeleteExerciseById ( IRepository < Exercise > exerciseRepository , ClaimsPrincipal claimsPrincipal , int id )
122300 {
123301 var authorized = claimsPrincipal . IsInRole ( "Teacher" ) ;
124302 if ( ! authorized )
125303 {
126- return Results . Unauthorized ( ) ;
304+ return TypedResults . Unauthorized ( ) ;
127305 }
128306
129307 var response = await exerciseRepository . GetByIdWithIncludes ( null , id ) ;
@@ -132,46 +310,48 @@ private static async Task<IResult> UpdateExerciseById(IRepository<Exercise> exer
132310 return TypedResults . NotFound ( ) ;
133311 }
134312
135- response . Name = exerciseDTO . Name ;
136- response . Description = exerciseDTO . Description ;
137- response . GitHubLink = exerciseDTO . GitHubLink ;
138-
139- exerciseRepository . Update ( response ) ;
313+ exerciseRepository . Delete ( response ) ;
140314 await exerciseRepository . SaveAsync ( ) ;
141315
142316 var result = new Exercise_noUnit ( response ) ;
143317 return TypedResults . Ok ( result ) ;
144-
145318 }
146319
147- private static async Task < IResult > CreateExerciseInUnit (
148- IRepository < Exercise > exerciseRepository ,
149- IRepository < Unit > unitRepository ,
150- ClaimsPrincipal claimsPrincipal ,
151- int id ,
152- UpdateCreateExerciseDTO exerciseDTO )
320+ [ ProducesResponseType ( StatusCodes . Status200OK ) ]
321+ [ ProducesResponseType ( StatusCodes . Status401Unauthorized ) ]
322+ [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
323+ private static async Task < IResult > UpdateExerciseById ( IRepository < Exercise > exerciseRepository , IRepository < Unit > unitRepository , ClaimsPrincipal claimsPrincipal , int id , UpdateWithNewUnitExerciseDTO exerciseDTO )
153324 {
154325 var authorized = claimsPrincipal . IsInRole ( "Teacher" ) ;
155326 if ( ! authorized )
156327 {
157- return Results . Unauthorized ( ) ;
328+ return TypedResults . Unauthorized ( ) ;
158329 }
159330
160- var unit = await unitRepository . GetByIdWithIncludes ( null , id ) ;
161- if ( unit == null )
331+ var unit = await unitRepository . GetByIdWithIncludes ( null , exerciseDTO . UnitId ) ;
332+ if ( unit == null )
333+ {
334+ return TypedResults . NotFound ( "Couldn't fint Unit" ) ;
335+ }
336+ var response = await exerciseRepository . GetByIdWithIncludes ( null , id ) ;
337+ if ( response == null )
162338 {
163339 return TypedResults . NotFound ( ) ;
164340 }
165341
166- var newExercise = new Exercise { UnitId = id , Name = exerciseDTO . Name , GitHubLink = exerciseDTO . GitHubLink , Description = exerciseDTO . Description , Unit = unit } ;
167- exerciseRepository . Insert ( newExercise ) ;
168- await exerciseRepository . SaveAsync ( ) ;
342+ response . Name = exerciseDTO . Name ;
343+ response . Description = exerciseDTO . Description ;
344+ response . GitHubLink = exerciseDTO . GitHubLink ;
345+ response . UnitId = exerciseDTO . UnitId ;
346+ response . Unit = unit ;
169347
170- var result = new Exercise_noUnit ( newExercise ) ;
171- return TypedResults . Ok ( result ) ;
172- }
173348
349+ exerciseRepository . Update ( response ) ;
350+ await exerciseRepository . SaveAsync ( ) ;
174351
352+ var result = new Exercise_noUnit ( response ) ;
353+ return TypedResults . Ok ( result ) ;
175354
355+ }
176356
177357}
0 commit comments