using AutoMapper; using FastEndpoints; using MetaCourse.Api.Data; using MetaCourse.Api.DTOs.Topics; using MetaCourse.Api.Entities; using Microsoft.EntityFrameworkCore; namespace MetaCourse.Api.Endpoints.Topics; public class CreateTopicEndpoint(AppDbContext db, AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { Post("api/topics"); AllowAnonymous(); Summary(s => s.Summary = "Ajoute un sujet à un cours"); } public override async Task HandleAsync(CreateTopicDto req, CancellationToken ct) { var courseExists = await db.Courses.AnyAsync(c => c.Id == req.CourseId, ct); if (!courseExists) { AddError(r => r.CourseId, "Le cours n'existe pas."); await SendErrorsAsync(404, ct); return; } var topic = mapper.Map(req); db.Topics.Add(topic); await db.SaveChangesAsync(ct); await SendAsync(mapper.Map(topic), 201, ct); } }