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