From 97a7c6811c727ca705ad1f256324c93238d177b3 Mon Sep 17 00:00:00 2001 From: sanchezvem Date: Fri, 7 Nov 2025 17:41:50 +0100 Subject: [PATCH] created product's endpoints --- .../DTO/Product/Request/CreateProductDto.cs | 2 +- .../DTO/Product/Request/UpdateProductDto.cs | 2 +- .../DTO/Product/Response/GetProductDto.cs | 2 +- .../Product/GetAllProductsEndpoint.cs | 36 +++++++++++++ .../Endpoints/Product/GetProductEndpoint.cs | 48 +++++++++++++++++ .../PatchProductMinimalStockEndpoint.cs | 46 ++++++++++++++++ .../Product/UpdateProductEndpoint.cs | 54 +++++++++++++++++++ 7 files changed, 187 insertions(+), 3 deletions(-) create mode 100644 PyroFetes/Endpoints/Product/GetAllProductsEndpoint.cs create mode 100644 PyroFetes/Endpoints/Product/GetProductEndpoint.cs create mode 100644 PyroFetes/Endpoints/Product/PatchProductMinimalStockEndpoint.cs create mode 100644 PyroFetes/Endpoints/Product/UpdateProductEndpoint.cs diff --git a/PyroFetes/DTO/Product/Request/CreateProductDto.cs b/PyroFetes/DTO/Product/Request/CreateProductDto.cs index 24ecf17..44c38c4 100644 --- a/PyroFetes/DTO/Product/Request/CreateProductDto.cs +++ b/PyroFetes/DTO/Product/Request/CreateProductDto.cs @@ -2,7 +2,7 @@ namespace PyroFetes.DTO.Product.Request; public class CreateProductDto { - public int References { get; set; } + public string? References { get; set; } public string? Name { get; set; } public decimal Duration {get; set;} public decimal Caliber { get; set; } diff --git a/PyroFetes/DTO/Product/Request/UpdateProductDto.cs b/PyroFetes/DTO/Product/Request/UpdateProductDto.cs index 0548119..23bb108 100644 --- a/PyroFetes/DTO/Product/Request/UpdateProductDto.cs +++ b/PyroFetes/DTO/Product/Request/UpdateProductDto.cs @@ -3,7 +3,7 @@ namespace PyroFetes.DTO.Product.Request; public class UpdateProductDto { public int Id { get; set; } - public int References { get; set; } + public string? References { get; set; } public string? Name { get; set; } public decimal Duration {get; set;} public decimal Caliber { get; set; } diff --git a/PyroFetes/DTO/Product/Response/GetProductDto.cs b/PyroFetes/DTO/Product/Response/GetProductDto.cs index 6781381..d31bfb5 100644 --- a/PyroFetes/DTO/Product/Response/GetProductDto.cs +++ b/PyroFetes/DTO/Product/Response/GetProductDto.cs @@ -3,7 +3,7 @@ namespace PyroFetes.DTO.Product.Response; public class GetProductDto { public int Id { get; set; } - public int References { get; set; } + public string? References { get; set; } public string? Name { get; set; } public decimal Duration {get; set;} public decimal Caliber { get; set; } diff --git a/PyroFetes/Endpoints/Product/GetAllProductsEndpoint.cs b/PyroFetes/Endpoints/Product/GetAllProductsEndpoint.cs new file mode 100644 index 0000000..70ec082 --- /dev/null +++ b/PyroFetes/Endpoints/Product/GetAllProductsEndpoint.cs @@ -0,0 +1,36 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.Product.Response; +using PyroFetes.DTO.PurchaseProduct.Response; + +namespace PyroFetes.Endpoints.Product; + +public class GetAllProductsEndpoint(PyroFetesDbContext database) : EndpointWithoutRequest> +{ + public override void Configure() + { + Get("/api/products"); + } + + public override async Task HandleAsync(CancellationToken ct) + { + var product = await database.Products + .Select(product => new GetProductDto() + { + Id = product.Id, + References = product.Reference, + Name = product.Name, + Duration = product.Duration, + Caliber = product.Caliber, + ApprovalNumber = product.ApprovalNumber, + Weight = product.Weight, + Nec = product.Nec, + Image = product.Image, + Link = product.Link, + MinimalQuantity = product.MinimalQuantity, + }) + .ToListAsync(ct); + + await Send.OkAsync(product, ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Product/GetProductEndpoint.cs b/PyroFetes/Endpoints/Product/GetProductEndpoint.cs new file mode 100644 index 0000000..72e3fa5 --- /dev/null +++ b/PyroFetes/Endpoints/Product/GetProductEndpoint.cs @@ -0,0 +1,48 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.Product.Response; +using PyroFetes.DTO.PurchaseProduct.Response; + +namespace PyroFetes.Endpoints.Product; + +public class GetProductRequest +{ + public int Id { get; set; } +} + +public class GetProductEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Get("/api/products/{@Id}", x => new {x.Id}); + } + + public override async Task HandleAsync(GetProductRequest req, CancellationToken ct) + { + var product = await database.Products + .SingleOrDefaultAsync(x => x.Id == req.Id, ct); + + if (product == null) + { + await Send.NotFoundAsync(ct); + return; + } + + GetProductDto responseDto = new() + { + Id = product.Id, + References = product.Reference, + Name = product.Name, + Duration = product.Duration, + Caliber = product.Caliber, + ApprovalNumber = product.ApprovalNumber, + Weight = product.Weight, + Nec = product.Nec, + Image = product.Image, + Link = product.Link, + MinimalQuantity = product.MinimalQuantity, + }; + + await Send.OkAsync(responseDto, ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Product/PatchProductMinimalStockEndpoint.cs b/PyroFetes/Endpoints/Product/PatchProductMinimalStockEndpoint.cs new file mode 100644 index 0000000..31ad504 --- /dev/null +++ b/PyroFetes/Endpoints/Product/PatchProductMinimalStockEndpoint.cs @@ -0,0 +1,46 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.Product.Request; +using PyroFetes.DTO.Product.Response; +using PyroFetes.DTO.PurchaseProduct.Response; + +namespace PyroFetes.Endpoints.Product; + +public class PatchProductMinimalStockEndpoint(PyroFetesDbContext database) + : Endpoint +{ + public override void Configure() + { + Patch("/api/products/{@Id}/MinimalStock", x => new { x.Id }); + AllowAnonymous(); + } + + public override async Task HandleAsync(PatchProductMinimalStockDto req, CancellationToken ct) + { + var product = await database.Products.SingleOrDefaultAsync(po => po.Id == req.Id, ct); + if (product == null) + { + await Send.NotFoundAsync(ct); + return; + } + + product.MinimalQuantity = req.MinimalQuantity; + await database.SaveChangesAsync(ct); + + GetProductDto responseDto = new() + { + Id = product.Id, + References = product.Reference, + Name = product.Name, + Duration = product.Duration, + Caliber = product.Caliber, + ApprovalNumber = product.ApprovalNumber, + Weight = product.Weight, + Nec = product.Nec, + Image = product.Image, + Link = product.Link, + MinimalQuantity = product.MinimalQuantity, + }; + await Send.OkAsync(responseDto, ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Product/UpdateProductEndpoint.cs b/PyroFetes/Endpoints/Product/UpdateProductEndpoint.cs new file mode 100644 index 0000000..856280f --- /dev/null +++ b/PyroFetes/Endpoints/Product/UpdateProductEndpoint.cs @@ -0,0 +1,54 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.Product.Request; +using PyroFetes.DTO.Product.Response; + +namespace PyroFetes.Endpoints.Product; + +public class UpdateProductEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Put("/api/products/{@Id}", x => new {x.Id}); + } + + public override async Task HandleAsync(UpdateProductDto req, CancellationToken ct) + { + var product = await database.Products.SingleOrDefaultAsync(x => x.Id == req.Id, ct); + + if (product == null) + { + await Send.NotFoundAsync(ct); + return; + } + + product.Reference = req.References; + product.Name = req.Name; + product.Duration = req.Duration; + product.Caliber = req.Caliber; + product.ApprovalNumber = req.ApprovalNumber; + product.Weight = req.Weight; + product.Nec = req.Nec; + product.Image = req.Image; + product.Link = req.Link; + product.MinimalQuantity = req.MinimalQuantity; + await database.SaveChangesAsync(ct); + + GetProductDto responseDto = new() + { + Id = product.Id, + References = product.Reference, + Name = product.Name, + Duration = product.Duration, + Caliber = product.Caliber, + ApprovalNumber = product.ApprovalNumber, + Weight = product.Weight, + Nec = product.Nec, + Image = product.Image, + Link = product.Link, + MinimalQuantity = product.MinimalQuantity, + }; + + await Send.OkAsync(responseDto, ct); + } +} \ No newline at end of file