45 lines
1.2 KiB
C#
45 lines
1.2 KiB
C#
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);
|
|
}
|
|
}
|