Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions exercise.wwwapi/DTOs/Courses/CoursePostDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

namespace exercise.wwwapi.DTOs.Courses
{
public class CoursePostDTO
{
public string Name { get; set; }

}
}
93 changes: 90 additions & 3 deletions exercise.wwwapi/Endpoints/CourseEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,109 @@ public static void ConfigureCourseEndpoints(this WebApplication app)
var courses = app.MapGroup("courses");
courses.MapGet("/", GetCourses).WithSummary("Returns all courses");
courses.MapGet("/{id}", GetCourseById).WithSummary("Returns course with provided id");
}
courses.MapPost("/", CreateCourse).WithSummary("Create a new course");
courses.MapDelete("/{id}", DeleteCourseById).WithSummary("Delete a course");
courses.MapPut("/{id}", UpdateCourse).WithSummary("Update a course name");
}


[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
private static async Task<IResult> GetCourses(IRepository<Course> repository, ClaimsPrincipal claimsPrincipal)
{
var response = await repository.GetWithIncludes(c => c.Include(a => a.CourseModules).ThenInclude(b => b.Module).ThenInclude(d => d.Units).ThenInclude(u => u.Exercises));
if (response == null || response.Count == 0)
{
return TypedResults.NotFound("No courses found");
}
var result = response.Select(c => new GetCourseDTO(c));
return TypedResults.Ok(result);
}

[ProducesResponseType(StatusCodes.Status200OK)]
private static async Task<IResult> GetCourseById(IRepository<Course> repository, int id)
[ProducesResponseType(StatusCodes.Status404NotFound)]
private static async Task<IResult> GetCourseById(IRepository<Course> repository, int id, ClaimsPrincipal claimsPrincipal)
{
var response = await repository.GetByIdWithIncludes(c => c.Include(a => a.CourseModules).ThenInclude(b => b.Module).ThenInclude(d => d.Units).ThenInclude(u => u.Exercises), id);
if (response == null)
{
return TypedResults.NotFound("No course with the given id was found");
}
var result = new GetCourseDTO(response);
return TypedResults.Ok(result);
}
}

[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
private static async Task<IResult> CreateCourse(IRepository<Course> repository, CoursePostDTO postedCourse, ClaimsPrincipal claimsPrincipal)
{

if (claimsPrincipal.IsInRole("Teacher") == false)
{
return TypedResults.Unauthorized();
}
if (postedCourse == null || postedCourse.Name == null || postedCourse.Name == "")
{
return TypedResults.BadRequest("Course data missing in request");
}

Course newCourse = new Course { Name = postedCourse.Name };
repository.Insert(newCourse);
await repository.SaveAsync();
GetCourseDTO response = new GetCourseDTO(newCourse);

return TypedResults.Created($"/courses/{newCourse.Id}", response);
}

[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
private static async Task<IResult> DeleteCourseById(IRepository<Course> repository, int id, ClaimsPrincipal claimsPrincipal)
{
if (claimsPrincipal.IsInRole("Teacher") == false)
{
return TypedResults.Unauthorized();
}

Course? course = await repository.GetByIdAsync(id);
if(course == null)
{
return TypedResults.NotFound($"No course with the given id: {id} was found");
}
repository.Delete(course);
await repository.SaveAsync();

return TypedResults.NoContent();
}

[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
private static async Task<IResult> UpdateCourse(IRepository<Course> repository, int id, CoursePostDTO updatedCourse, ClaimsPrincipal claimsPrincipal)
{
if (claimsPrincipal.IsInRole("Teacher") == false)
{
return TypedResults.Unauthorized();
}

Course? course = await repository.GetByIdAsync(id);
if (course == null)
{
return TypedResults.NotFound($"No course with the given id: {id} was found");
}
if(updatedCourse == null || updatedCourse.Name == null || updatedCourse.Name == "")
{
return TypedResults.BadRequest("Missing update data in request");
}
course.Name = updatedCourse.Name;
repository.Update(course);
await repository.SaveAsync();

GetCourseDTO response = new GetCourseDTO(course);

return TypedResults.Ok(response);
}

}