Files
MetaCourseApi/MetaCourse.Api/Endpoints/Topics/UpdateTopicEndpoint.cs
T
2026-05-05 10:39:43 +02:00

37 lines
998 B
C#

using AutoMapper;
using FastEndpoints;
using MetaCourse.Api.Data;
using MetaCourse.Api.DTOs.Topics;
using Microsoft.EntityFrameworkCore;
namespace MetaCourse.Api.Endpoints.Topics;
public class UpdateTopicEndpoint(AppDbContext db, AutoMapper.IMapper mapper) : Endpoint<UpdateTopicDto, GetTopicDto>
{
public override void Configure()
{
Put("api/topics/{id}");
AllowAnonymous();
Summary(s => s.Summary = "Met à jour un sujet");
}
public override async Task HandleAsync(UpdateTopicDto req, CancellationToken ct)
{
var topic = await db.Topics
.Include(t => t.TopicResources)
.ThenInclude(tr => tr.Resource)
.FirstOrDefaultAsync(t => t.Id == req.Id, ct);
if (topic is null)
{
await SendNotFoundAsync(ct);
return;
}
mapper.Map(req, topic);
await db.SaveChangesAsync(ct);
await SendOkAsync(mapper.Map<GetTopicDto>(topic), ct);
}
}