32 lines
935 B
C#
32 lines
935 B
C#
using AutoMapper;
|
|
using FastEndpoints;
|
|
using MetaCourse.Api.Data;
|
|
using MetaCourse.Api.DTOs.Resources;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace MetaCourse.Api.Endpoints.Resources;
|
|
|
|
public class UpdateResourceEndpoint(AppDbContext db, AutoMapper.IMapper mapper) : Endpoint<UpdateResourceDto, GetResourceDto>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Put("api/resources/{id}");
|
|
AllowAnonymous();
|
|
Summary(s => s.Summary = "Met à jour une ressource");
|
|
}
|
|
|
|
public override async Task HandleAsync(UpdateResourceDto req, CancellationToken ct)
|
|
{
|
|
var resource = await db.Resources.FirstOrDefaultAsync(r => r.Id == req.Id, ct);
|
|
if (resource is null)
|
|
{
|
|
await SendNotFoundAsync(ct);
|
|
return;
|
|
}
|
|
|
|
mapper.Map(req, resource);
|
|
await db.SaveChangesAsync(ct);
|
|
await SendOkAsync(mapper.Map<GetResourceDto>(resource), ct);
|
|
}
|
|
}
|