first commit
This commit is contained in:
30
BookHive/Endpoints/Authors/UpdateAuthorEndpoint.cs
Normal file
30
BookHive/Endpoints/Authors/UpdateAuthorEndpoint.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using BookHive.DTO.Author;
|
||||
using BookHive.Models;
|
||||
using BookHive.Repositories;
|
||||
using BookHive.Specifications.Authors;
|
||||
using FastEndpoints;
|
||||
|
||||
namespace BookHive.Endpoints.Authors;
|
||||
|
||||
public class UpdateAuthorEndpoint(AuthorRepository authorRepository, AutoMapper.IMapper mapper) : Endpoint<UpdateAuthorDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Put("/authors/{@Id}/", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateAuthorDto req, CancellationToken ct)
|
||||
{
|
||||
Author? author = await authorRepository.SingleOrDefaultAsync(new GetAuthorByIdSpec(req.Id), ct);
|
||||
if (author is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
mapper.Map(req, author);
|
||||
await authorRepository.SaveChangesAsync(ct);
|
||||
await Send.OkAsync(cancellation: ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user