fixed errors and finish endpoints

This commit is contained in:
2026-03-09 23:24:01 +01:00
parent 679c552a3f
commit 756382a496
8 changed files with 124 additions and 12 deletions

View File

@@ -1,6 +1,7 @@
using BookHive.Models;
using BookHive.Repositories;
using BookHive.Specifications.Books;
using BookHive.Specifications.Loans;
using FastEndpoints;
namespace BookHive.Endpoints.Books;
@@ -10,14 +11,14 @@ public class DeleteBookRequest
public int Id { get; set; }
}
public class DeleteBookEndpoint(BookRepository bookRepository) : Endpoint<DeleteBookRequest>
public class DeleteBookEndpoint(BookRepository bookRepository, LoanRepository loanRepository) : 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);
@@ -27,7 +28,14 @@ public class DeleteBookEndpoint(BookRepository bookRepository) : Endpoint<Delete
await Send.NotFoundAsync(ct);
return;
}
Loan? loan = await loanRepository.FirstOrDefaultAsync(new GetAvailableBookByIdSpec(req.Id), ct);
if (loan is not null)
{
await Send.StringAsync("Le livre est encore emprunté", 409, cancellation: ct);
return;
}
await bookRepository.DeleteAsync(book, ct);
await Send.OkAsync(cancellation: ct);
}