34 lines
856 B
C#
34 lines
856 B
C#
using BookHive.Models;
|
|
using BookHive.Repositories;
|
|
using BookHive.Specifications.Books;
|
|
using FastEndpoints;
|
|
|
|
namespace BookHive.Endpoints.Books;
|
|
|
|
public class DeleteBookRequest
|
|
{
|
|
public int Id { get; set; }
|
|
}
|
|
|
|
public class DeleteBookEndpoint(BookRepository bookRepository) : Endpoint<DeleteBookRequest>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Delete("/books/{@Id}", x => new { x.Id });
|
|
AllowAnonymous();
|
|
}
|
|
|
|
public override async Task HandleAsync(DeleteBookRequest req, CancellationToken ct)
|
|
{
|
|
Book? book = await bookRepository.SingleOrDefaultAsync(new GetBookByIdSpec(req.Id), ct);
|
|
|
|
if (book is null)
|
|
{
|
|
await Send.NotFoundAsync(ct);
|
|
return;
|
|
}
|
|
|
|
await bookRepository.DeleteAsync(book, ct);
|
|
await Send.OkAsync(cancellation: ct);
|
|
}
|
|
} |