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,11 +1,14 @@
using BookHive.DTO.Loan;
using BookHive.Models;
using BookHive.Repositories;
using BookHive.Specifications.Books;
using BookHive.Specifications.Loans;
using BookHive.Specifications.Members;
using FastEndpoints;
namespace BookHive.Endpoints.Loans;
public class CreateLoanEndpoint(LoanRepository loanRepository, AutoMapper.IMapper mapper) : Endpoint<CreateLoanDto>
public class CreateLoanEndpoint(LoanRepository loanRepository, BookRepository bookRepository, MemberRepository memberRepository, AutoMapper.IMapper mapper) : Endpoint<CreateLoanDto>
{
public override void Configure()
{
@@ -15,6 +18,39 @@ public class CreateLoanEndpoint(LoanRepository loanRepository, AutoMapper.IMappe
public override async Task HandleAsync(CreateLoanDto req, CancellationToken ct)
{
Book? book = await bookRepository.SingleOrDefaultAsync(new GetBookByIdSpec(req.BookId), ct);
if (book is null)
{
await Send.NotFoundAsync(ct);
return;
}
Member? member = await memberRepository.SingleOrDefaultAsync(new GetMemberByIdSpec(req.MemberId), ct);
if (member is null)
{
await Send.NotFoundAsync(ct);
return;
}
if (!member.IsActive)
{
await Send.StringAsync("Le membre est désactivé", 400, cancellation: ct);
return;
}
Loan? loan = await loanRepository.FirstOrDefaultAsync(new GetAvailableBookByIdSpec(req.BookId), ct);
if (loan is not null)
{
await Send.StringAsync("Ce livre est déjà emprunté", 400, cancellation: ct);
return;
}
if (req.DueDate < req.LoanDate || req.DueDate.DayNumber - req.LoanDate.DayNumber < 1 || req.DueDate.DayNumber - req.LoanDate.DayNumber > 30)
{
await Send.StringAsync("La date de retour estimée est incorrecte", 400, cancellation: ct);
return;
}
await loanRepository.AddAsync(mapper.Map<Loan>(req), ct);
await Send.OkAsync(cancellation: ct);
}