From 33719b708e7fd705b83690a8e120fc7dcf9185f1 Mon Sep 17 00:00:00 2001 From: sanchezvem Date: Thu, 6 Nov 2025 18:52:18 +0100 Subject: [PATCH] Creating of price's endpoints --- PyroFetes/DTO/Price/Request/CreatePriceDto.cs | 2 +- .../Request/PatchPriceSellingPriceDto.cs | 3 +- .../Endpoints/Price/CreatePriceEndpoint.cs | 83 +++++++++++++++++++ .../Endpoints/Price/DeletePriceEndpoint.cs | 36 ++++++++ .../Endpoints/Price/PatchPriceEndpoint.cs | 36 ++++++++ 5 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 PyroFetes/Endpoints/Price/CreatePriceEndpoint.cs create mode 100644 PyroFetes/Endpoints/Price/DeletePriceEndpoint.cs create mode 100644 PyroFetes/Endpoints/Price/PatchPriceEndpoint.cs diff --git a/PyroFetes/DTO/Price/Request/CreatePriceDto.cs b/PyroFetes/DTO/Price/Request/CreatePriceDto.cs index f268d21..046c064 100644 --- a/PyroFetes/DTO/Price/Request/CreatePriceDto.cs +++ b/PyroFetes/DTO/Price/Request/CreatePriceDto.cs @@ -14,7 +14,7 @@ public class CreatePriceDto public int SupplierDeliveryDelay { get; set; } public int ProductId { get; set; } - public int ProductReferences { get; set; } + public string? ProductReferences { get; set; } public string? ProductName { get; set; } public decimal ProductDuration {get; set;} public decimal ProductCaliber { get; set; } diff --git a/PyroFetes/DTO/Price/Request/PatchPriceSellingPriceDto.cs b/PyroFetes/DTO/Price/Request/PatchPriceSellingPriceDto.cs index 5692248..3dd1b6b 100644 --- a/PyroFetes/DTO/Price/Request/PatchPriceSellingPriceDto.cs +++ b/PyroFetes/DTO/Price/Request/PatchPriceSellingPriceDto.cs @@ -2,6 +2,7 @@ public class PatchPriceSellingPriceDto { - public int Id { get; set; } + public int ProductId { get; set; } + public int SupplierId { get; set; } public decimal SellingPrice { get; set; } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Price/CreatePriceEndpoint.cs b/PyroFetes/Endpoints/Price/CreatePriceEndpoint.cs new file mode 100644 index 0000000..79b3060 --- /dev/null +++ b/PyroFetes/Endpoints/Price/CreatePriceEndpoint.cs @@ -0,0 +1,83 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.Price.Request; +using PyroFetes.DTO.Price.Response; + +namespace PyroFetes.Endpoints.Price; + +public class CreatePriceEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Post("/api/prices"); + AllowAnonymous(); + } + + public override async Task HandleAsync(CreatePriceDto req, CancellationToken ct) + { + var price = await database.Prices.SingleOrDefaultAsync(p => p.ProductId == req.ProductId && p.SupplierId == req.SupplierId, ct); + if (price == null) + { + await Send.NotFoundAsync(ct); + return; + } + + var supplier = await database.Suppliers.FirstOrDefaultAsync(s => s.Id == req.SupplierId, ct); + + var product = await database.Products.SingleOrDefaultAsync(p => p.Id == req.ProductId, ct); + if (product == null) + { + await Send.NotFoundAsync(ct); + return; + } + + if (supplier == null) + { + supplier = new Models.Supplier() + { + Name = req.SupplierName, + Email = req.SupplierEmail, + Phone = req.SupplierPhone, + Address = req.SupplierAddress, + City = req.SupplierCity, + ZipCode = req.SupplierZipCode, + DeliveryDelay = req.SupplierDeliveryDelay + + }; + database.Suppliers.Add(supplier); + await database.SaveChangesAsync(ct); + } + + var priceAdded = new Models.Price() + { + SellingPrice = price.SellingPrice, + SupplierId = supplier.Id, + ProductId = product.Id, + }; + database.Prices.Add(priceAdded); + await database.SaveChangesAsync(ct); + + var productAdded = new Models.Product() + { + Reference = req.ProductReferences, + Name = req.ProductName, + Duration = req.ProductDuration, + Caliber = req.ProductCaliber, + ApprovalNumber = req.ProductApprovalNumber, + Weight = req.ProductWeight, + Nec = req.ProductNec, + Image = req.ProductImage, + Link = req.ProductLink, + MinimalQuantity = req.ProductMinimalQuantity + }; + database.Products.Add(productAdded); + await database.SaveChangesAsync(ct); + + var responseDto = new GetPriceDto() + { + + }; + + await Send.OkAsync(responseDto, ct); + } +} diff --git a/PyroFetes/Endpoints/Price/DeletePriceEndpoint.cs b/PyroFetes/Endpoints/Price/DeletePriceEndpoint.cs new file mode 100644 index 0000000..bb289c9 --- /dev/null +++ b/PyroFetes/Endpoints/Price/DeletePriceEndpoint.cs @@ -0,0 +1,36 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; + +namespace PyroFetes.Endpoints.QuotationProduct; + +public class DeletePriceRequest +{ + public int ProductId { get; set; } + public int SupplierId { get; set; } +} + +public class DeletePriceEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Delete("/api/prices/{@ProductId}/{@SupplierId}", x => new {x.ProductId, x.SupplierId}); + AllowAnonymous(); + } + + public override async Task HandleAsync(DeletePriceRequest req, CancellationToken ct) + { + var price = await database.Prices + .SingleOrDefaultAsync(p => p.ProductId == req.ProductId && p.SupplierId == req.SupplierId, ct); + + if (price == null) + { + await Send.NotFoundAsync(ct); + return; + } + + database.Prices.Remove(price); + await database.SaveChangesAsync(ct); + + await Send.NoContentAsync(ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Price/PatchPriceEndpoint.cs b/PyroFetes/Endpoints/Price/PatchPriceEndpoint.cs new file mode 100644 index 0000000..05dbdb8 --- /dev/null +++ b/PyroFetes/Endpoints/Price/PatchPriceEndpoint.cs @@ -0,0 +1,36 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.Price.Request; +using PyroFetes.DTO.Price.Response; + +namespace PyroFetes.Endpoints.Price; + +public class PatchPriceEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Patch("/api/prices/{@ProductId}/{@SupplierId}/SellingPrice", x => new { x.ProductId, x.SupplierId }); + AllowAnonymous(); + } + + public override async Task HandleAsync(PatchPriceSellingPriceDto req, CancellationToken ct) + { + var price = await database.Prices.SingleOrDefaultAsync(p => p.ProductId == req.ProductId && p.SupplierId == req.SupplierId, ct); + if (price == null) + { + await Send.NotFoundAsync(ct); + return; + } + + 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); + } +} \ No newline at end of file