52 lines
1.6 KiB
C#
52 lines
1.6 KiB
C#
using ApiEfCoreLibrary.DTO.Loan.Request;
|
|
using ApiEfCoreLibrary.DTO.Loan.Response;
|
|
using FastEndpoints;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace ApiEfCoreLibrary.Endpoints.Loan;
|
|
|
|
public class PatchLoanEndpoint(LibraryDbContext database) : Endpoint<PatchLoanDto, GetLoanDto>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Patch("/api/loans/{@Id}/EffectiveReturningDate", x => new {x.Id});
|
|
}
|
|
|
|
public override async Task HandleAsync(PatchLoanDto req, CancellationToken ct)
|
|
{
|
|
var loan = await database.Loans.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
|
|
|
|
if (loan == null)
|
|
{
|
|
await Send.NotFoundAsync(ct);
|
|
return;
|
|
}
|
|
|
|
if (req.EffectiveReturningDate <= DateOnly.FromDateTime(DateTime.Now))
|
|
{
|
|
await Send.StringAsync("Erreur de date. La date est inférieure à la date actuelle.", 400);
|
|
return;
|
|
}
|
|
|
|
if (loan.EffectiveReturningDate != null)
|
|
{
|
|
await Send.StringAsync("Impossible de modifier la date de retour.", 400);
|
|
return;
|
|
}
|
|
|
|
loan.EffectiveReturningDate = req.EffectiveReturningDate;
|
|
await database.SaveChangesAsync(ct);
|
|
|
|
GetLoanDto responseDto = new()
|
|
{
|
|
Id = loan.Id,
|
|
BookId = loan.BookId,
|
|
UserId = loan.UserId,
|
|
Date = loan.Date,
|
|
PlannedReturningDate = loan.PlannedReturningDate,
|
|
EffectiveReturningDate = loan.EffectiveReturningDate
|
|
};
|
|
|
|
await Send.OkAsync(responseDto, ct);
|
|
}
|
|
} |