37 lines
998 B
C#
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);
|
|
}
|
|
}
|