Initial commit
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
using FastEndpoints;
|
||||
using MetaCourse.Api.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace MetaCourse.Api.Endpoints.Courses;
|
||||
|
||||
public class DeleteCourseRequest
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteCourseEndpoint(AppDbContext db) : Endpoint<DeleteCourseRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("api/courses/{id}");
|
||||
AllowAnonymous();
|
||||
Summary(s => s.Summary = "Supprime un cours (uniquement si brouillon sans inscriptions)");
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeleteCourseRequest req, CancellationToken ct)
|
||||
{
|
||||
var course = await db.Courses
|
||||
.Include(c => c.UserCourses)
|
||||
.FirstOrDefaultAsync(c => c.Id == req.Id, ct);
|
||||
|
||||
if (course is null)
|
||||
{
|
||||
await SendNotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
if (course.UserCourses.Any())
|
||||
{
|
||||
AddError("Impossible de supprimer un cours auquel des utilisateurs sont inscrits.");
|
||||
await SendErrorsAsync(409, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
db.Courses.Remove(course);
|
||||
await db.SaveChangesAsync(ct);
|
||||
await SendNoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user