Finished refactoring prices endpoints
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
using PyroFetes.Specifications.Prices;
|
||||
|
||||
namespace PyroFetes.Endpoints.Prices;
|
||||
|
||||
@@ -10,7 +12,7 @@ public class DeletePriceRequest
|
||||
public int SupplierId { get; set; }
|
||||
}
|
||||
|
||||
public class DeletePriceEndpoint(PyroFetesDbContext database) : Endpoint<DeletePriceRequest>
|
||||
public class DeletePriceEndpoint(PricesRepository pricesRepository) : Endpoint<DeletePriceRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
@@ -20,8 +22,7 @@ public class DeletePriceEndpoint(PyroFetesDbContext database) : Endpoint<DeleteP
|
||||
|
||||
public override async Task HandleAsync(DeletePriceRequest req, CancellationToken ct)
|
||||
{
|
||||
Price? price = await database.Prices
|
||||
.SingleOrDefaultAsync(p => p.ProductId == req.ProductId && p.SupplierId == req.SupplierId, ct);
|
||||
Price? price = await pricesRepository.FirstOrDefaultAsync(new GetPriceByProductIdAndSupplierIdSpec(req.ProductId,req.SupplierId), ct);
|
||||
|
||||
if (price == null)
|
||||
{
|
||||
@@ -29,9 +30,8 @@ public class DeletePriceEndpoint(PyroFetesDbContext database) : Endpoint<DeleteP
|
||||
return;
|
||||
}
|
||||
|
||||
database.Prices.Remove(price);
|
||||
await database.SaveChangesAsync(ct);
|
||||
await pricesRepository.DeleteAsync(price, ct);
|
||||
|
||||
await Send.NoContentAsync(ct);
|
||||
await Send.OkAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,15 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.DTO.Price.Request;
|
||||
using PyroFetes.DTO.Price.Response;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
using PyroFetes.Specifications.Prices;
|
||||
|
||||
namespace PyroFetes.Endpoints.Prices;
|
||||
|
||||
public class PatchPriceEndpoint(PyroFetesDbContext database) : Endpoint<PatchPriceSellingPriceDto, GetPriceDto>
|
||||
public class PatchPriceEndpoint(
|
||||
PricesRepository pricesRepository,
|
||||
AutoMapper.IMapper mapper) : Endpoint<PatchPriceSellingPriceDto, GetPriceDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
@@ -16,7 +19,8 @@ public class PatchPriceEndpoint(PyroFetesDbContext database) : Endpoint<PatchPri
|
||||
|
||||
public override async Task HandleAsync(PatchPriceSellingPriceDto req, CancellationToken ct)
|
||||
{
|
||||
Price? price = await database.Prices.SingleOrDefaultAsync(p => p.ProductId == req.ProductId && p.SupplierId == req.SupplierId, ct);
|
||||
Price? price = await pricesRepository.FirstOrDefaultAsync(new GetPriceByProductIdAndSupplierIdSpec(req.ProductId, req.SupplierId),ct);
|
||||
|
||||
if (price == null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
@@ -24,14 +28,9 @@ public class PatchPriceEndpoint(PyroFetesDbContext database) : Endpoint<PatchPri
|
||||
}
|
||||
|
||||
price.SellingPrice = req.SellingPrice;
|
||||
await database.SaveChangesAsync(ct);
|
||||
|
||||
GetPriceDto responseDto = new()
|
||||
{
|
||||
ProductId = price.ProductId,
|
||||
SupplierId = price.SupplierId,
|
||||
SellingPrice = price.SellingPrice
|
||||
};
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
|
||||
await pricesRepository.UpdateAsync(price, ct);
|
||||
|
||||
await Send.OkAsync(mapper.Map<GetPriceDto>(price), ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user