37 lines
955 B
C#
37 lines
955 B
C#
using ApiEfCoreLibrary.DTO.Loan.Request;
|
|
using ApiEfCoreLibrary.DTO.Loan.Response;
|
|
using ApiEfCoreLibrary.DTO.Book.Response;
|
|
using FastEndpoints;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace ApiEfCoreLibrary.Endpoints.Loan;
|
|
|
|
public class DeleteLoanRequest
|
|
{
|
|
public int Id { get; set; }
|
|
}
|
|
|
|
public class DeleteLoanEndpoint(LibraryDbContext database) : Endpoint<DeleteLoanRequest>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Delete("/loans/{@Id}", x => new {x.Id});
|
|
Roles("admin");
|
|
}
|
|
|
|
public override async Task HandleAsync(DeleteLoanRequest req, CancellationToken ct)
|
|
{
|
|
Models.Loan? loan = await database.Loans.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
|
|
|
|
if (loan == null)
|
|
{
|
|
await Send.NotFoundAsync(ct);
|
|
return;
|
|
}
|
|
|
|
database.Loans.Remove(loan);
|
|
await database.SaveChangesAsync(ct);
|
|
|
|
await Send.NoContentAsync(ct);
|
|
}
|
|
} |