forked from sanchezvem/PyroFetes
Created all endpoints for Quotation and QuotationProduct
This commit is contained in:
41
PyroFetes/Endpoints/Quotation/DeleteQuotationEndpoint.cs
Normal file
41
PyroFetes/Endpoints/Quotation/DeleteQuotationEndpoint.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace PyroFetes.Endpoints.Quotation;
|
||||
|
||||
public class DeleteQuotationRequest
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteQuotationEndpoint(PyroFetesDbContext database) : Endpoint<DeleteQuotationRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/api/quotations/{Id}", x => new {x.Id});
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeleteQuotationRequest req, CancellationToken ct)
|
||||
{
|
||||
var quotation = await database.Quotations
|
||||
.Include(q => q.QuotationProducts)
|
||||
.SingleOrDefaultAsync(q => q.Id == req.Id, ct);
|
||||
|
||||
if (quotation == null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
if (quotation.QuotationProducts != null && quotation.QuotationProducts.Any())
|
||||
{
|
||||
database.QuotationProducts.RemoveRange(quotation.QuotationProducts);
|
||||
}
|
||||
|
||||
database.Quotations.Remove(quotation);
|
||||
await database.SaveChangesAsync(ct);
|
||||
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user