using ApiEfCoreLibrary.DTO.Loan.Request; using ApiEfCoreLibrary.DTO.Loan.Response; using FastEndpoints; using Microsoft.EntityFrameworkCore; namespace ApiEfCoreLibrary.Endpoints.Loan; public class UpdateLoanEndpoint(LibraryDbContext database) : Endpoint { public override void Configure() { Put("/api/loans/{@Id}", x => new {x.Id}); Roles("admin", "librarian"); } public override async Task HandleAsync(UpdateLoanDto req, CancellationToken ct) { Models.Loan? loan = await database.Loans.SingleOrDefaultAsync(x => x.Id == req.Id, ct); if (loan == null) { await Send.NotFoundAsync(ct); return; } Models.Book? bookExists = await database.Books.FirstOrDefaultAsync(a => a.Id == req.BookId, ct); if (bookExists == null) { await Send.NoContentAsync(ct); return; } Models.User? userExists = await database.Users.FirstOrDefaultAsync(a => a.Id == req.UserId, ct); if (userExists == null) { await Send.NoContentAsync(ct); return; } loan.BookId = req.BookId; loan.UserId = req.UserId; loan.Date = req.Date; loan.PlannedReturningDate = req.PlannedReturningDate; 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); } }