diff --git a/PyroFetes/DTO/Price/Request/CreatePriceDto.cs b/PyroFetes/DTO/Price/Request/CreatePriceDto.cs index f268d21..b149ef6 100644 --- a/PyroFetes/DTO/Price/Request/CreatePriceDto.cs +++ b/PyroFetes/DTO/Price/Request/CreatePriceDto.cs @@ -4,7 +4,7 @@ public class CreatePriceDto { public decimal SellingPrice { get; set; } - public int SupplierId { get; set; } + public int? SupplierId { get; set; } public string? SupplierName { get; set; } public string? SupplierEmail { get; set; } public string? SupplierPhone { get; set; } @@ -13,8 +13,8 @@ public class CreatePriceDto public string? SupplierCity { get; set; } public int SupplierDeliveryDelay { get; set; } - public int ProductId { get; set; } - public int ProductReferences { get; set; } + public int? ProductId { 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/DTO/Price/Request/UpdatePriceDto.cs b/PyroFetes/DTO/Price/Request/UpdatePriceDto.cs index 6f6af24..1eac827 100644 --- a/PyroFetes/DTO/Price/Request/UpdatePriceDto.cs +++ b/PyroFetes/DTO/Price/Request/UpdatePriceDto.cs @@ -15,7 +15,7 @@ public class UpdatePriceDto 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/Response/GetPriceDto.cs b/PyroFetes/DTO/Price/Response/GetPriceDto.cs index 81a8f07..a042cd2 100644 --- a/PyroFetes/DTO/Price/Response/GetPriceDto.cs +++ b/PyroFetes/DTO/Price/Response/GetPriceDto.cs @@ -15,7 +15,7 @@ public class GetPriceDto 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/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/Price/CreatePriceEndpoint.cs b/PyroFetes/Endpoints/Price/CreatePriceEndpoint.cs new file mode 100644 index 0000000..d65ba2d --- /dev/null +++ b/PyroFetes/Endpoints/Price/CreatePriceEndpoint.cs @@ -0,0 +1,104 @@ +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) + { + // Gestion du fournisseur + var supplier = await database.Suppliers.FirstOrDefaultAsync(s => s.Id == req.SupplierId, ct); + 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); + } + + // Gestion du produit + var product = await database.Products.SingleOrDefaultAsync(p => p.Id == req.ProductId, ct); + if (product == null) + { + product = 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(product); + await database.SaveChangesAsync(ct); + } + + // Vérifie si le prix existe déjà pour ce fournisseur et produit + var existingPrice = await database.Prices + .SingleOrDefaultAsync(p => p.ProductId == product.Id && p.SupplierId == supplier.Id, ct); + + if (existingPrice != null) + { + await Send.ConflictAsync("Le fournisseur a déjà un prix pour ce produit.", ct); + return; + } + + // Création du prix + var priceAdded = new Models.Price() + { + SellingPrice = req.SellingPrice, + SupplierId = supplier.Id, + ProductId = product.Id + }; + database.Prices.Add(priceAdded); + await database.SaveChangesAsync(ct); + + // Création du DTO de réponse + var responseDto = new GetPriceDto() + { + SellingPrice = priceAdded.SellingPrice, + SupplierId = supplier.Id, + ProductId = product.Id, + SupplierName = supplier.Name, + SupplierEmail = supplier.Email, + SupplierPhone = supplier.Phone, + SupplierAddress = supplier.Address, + SupplierCity = supplier.City, + SupplierZipCode = supplier.ZipCode, + SupplierDeliveryDelay = supplier.DeliveryDelay, + ProductReferences = product.Reference, + ProductName = product.Name, + ProductDuration = product.Duration, + ProductCaliber = product.Caliber, + ProductApprovalNumber = product.ApprovalNumber, + ProductWeight = product.Weight, + ProductNec = product.Nec, + ProductImage = product.Image, + ProductLink = product.Link, + ProductMinimalQuantity = product.MinimalQuantity + }; + + 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 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 diff --git a/PyroFetes/Endpoints/Setting/CreateSettingEndpoint.cs b/PyroFetes/Endpoints/SettingEndpoints/CreateSettingEndpoint.cs similarity index 87% rename from PyroFetes/Endpoints/Setting/CreateSettingEndpoint.cs rename to PyroFetes/Endpoints/SettingEndpoints/CreateSettingEndpoint.cs index 11dcedd..93250d6 100644 --- a/PyroFetes/Endpoints/Setting/CreateSettingEndpoint.cs +++ b/PyroFetes/Endpoints/SettingEndpoints/CreateSettingEndpoint.cs @@ -1,7 +1,8 @@ -using PyroFetes.DTO.SettingDTO.Request; +using FastEndpoints; +using PyroFetes.DTO.SettingDTO.Request; using PyroFetes.DTO.SettingDTO.Response; -using FastEndpoints; -namespace PyroFetes.Endpoints.Setting; + +namespace PyroFetes.Endpoints.SettingEndpoints; public class CreateSettingEndpoint(PyroFetesDbContext database) : Endpoint { diff --git a/PyroFetes/Endpoints/Setting/DeleteSettingEndpoint.cs b/PyroFetes/Endpoints/SettingEndpoints/DeleteSettingEndpoint.cs similarity index 94% rename from PyroFetes/Endpoints/Setting/DeleteSettingEndpoint.cs rename to PyroFetes/Endpoints/SettingEndpoints/DeleteSettingEndpoint.cs index 92bfba7..0930566 100644 --- a/PyroFetes/Endpoints/Setting/DeleteSettingEndpoint.cs +++ b/PyroFetes/Endpoints/SettingEndpoints/DeleteSettingEndpoint.cs @@ -1,7 +1,7 @@ using FastEndpoints; using Microsoft.EntityFrameworkCore; -namespace PyroFetes.Endpoints.Setting; +namespace PyroFetes.Endpoints.SettingEndpoints; public class DeleteSettingRequest { diff --git a/PyroFetes/Endpoints/Setting/GetSettingEndpoint.cs b/PyroFetes/Endpoints/SettingEndpoints/GetSettingEndpoint.cs similarity index 95% rename from PyroFetes/Endpoints/Setting/GetSettingEndpoint.cs rename to PyroFetes/Endpoints/SettingEndpoints/GetSettingEndpoint.cs index 9ebd72b..34b059d 100644 --- a/PyroFetes/Endpoints/Setting/GetSettingEndpoint.cs +++ b/PyroFetes/Endpoints/SettingEndpoints/GetSettingEndpoint.cs @@ -2,7 +2,7 @@ using Microsoft.EntityFrameworkCore; using PyroFetes.DTO.SettingDTO.Response; -namespace PyroFetes.Endpoints.Setting; +namespace PyroFetes.Endpoints.SettingEndpoints; public class GetSettingRequest { diff --git a/PyroFetes/Endpoints/Setting/PatchSettingElectronicSignatureEndpoint.cs b/PyroFetes/Endpoints/SettingEndpoints/PatchSettingElectronicSignatureEndpoint.cs similarity index 95% rename from PyroFetes/Endpoints/Setting/PatchSettingElectronicSignatureEndpoint.cs rename to PyroFetes/Endpoints/SettingEndpoints/PatchSettingElectronicSignatureEndpoint.cs index c54d915..39e8459 100644 --- a/PyroFetes/Endpoints/Setting/PatchSettingElectronicSignatureEndpoint.cs +++ b/PyroFetes/Endpoints/SettingEndpoints/PatchSettingElectronicSignatureEndpoint.cs @@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore; using PyroFetes.DTO.SettingDTO.Request; using PyroFetes.DTO.SettingDTO.Response; -namespace PyroFetes.Endpoints.Setting; +namespace PyroFetes.Endpoints.SettingEndpoints; public class PatchSettingElectronicSignatureEndpoint(PyroFetesDbContext database) : Endpoint { diff --git a/PyroFetes/Endpoints/Setting/PatchSettingLogoEndpoint.cs b/PyroFetes/Endpoints/SettingEndpoints/PatchSettingLogoEndpoint.cs similarity index 95% rename from PyroFetes/Endpoints/Setting/PatchSettingLogoEndpoint.cs rename to PyroFetes/Endpoints/SettingEndpoints/PatchSettingLogoEndpoint.cs index 4bfe5c1..553800a 100644 --- a/PyroFetes/Endpoints/Setting/PatchSettingLogoEndpoint.cs +++ b/PyroFetes/Endpoints/SettingEndpoints/PatchSettingLogoEndpoint.cs @@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore; using PyroFetes.DTO.SettingDTO.Request; using PyroFetes.DTO.SettingDTO.Response; -namespace PyroFetes.Endpoints.Setting; +namespace PyroFetes.Endpoints.SettingEndpoints; public class PatchSettingLogoEndpoint(PyroFetesDbContext database) : Endpoint { diff --git a/PyroFetes/Endpoints/Supplier/CreateSupplierEndpoint.cs b/PyroFetes/Endpoints/Supplier/CreateSupplierEndpoint.cs new file mode 100644 index 0000000..6232481 --- /dev/null +++ b/PyroFetes/Endpoints/Supplier/CreateSupplierEndpoint.cs @@ -0,0 +1,43 @@ +using PyroFetes.DTO.Supplier.Request; +using PyroFetes.DTO.Supplier.Response; +using FastEndpoints; + +namespace PyroFetes.Endpoints.Supplier; + +public class CreateSupplierEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Post("/api/suppliers"); + } + + public override async Task HandleAsync(CreateSupplierDto req, CancellationToken ct) + { + var supplier = new Models.Supplier() + { + Name = req.Name, + Email = req.Email, + Phone = req.Phone, + Address = req.Address, + City = req.City, + ZipCode = req.ZipCode, + DeliveryDelay = req.DeliveryDelay + }; + + database.Suppliers.Add(supplier); + await database.SaveChangesAsync(ct); + + GetSupplierDto responseDto = new() + { + Id = supplier.Id, + Name = supplier.Name, + Email = supplier.Email, + Phone = supplier.Phone, + Address = supplier.Address, + City = supplier.City, + ZipCode = supplier.ZipCode, + DeliveryDelay = supplier.DeliveryDelay + }; + await Send.OkAsync(responseDto, ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Supplier/DeleteSupplierEndpoint.cs b/PyroFetes/Endpoints/Supplier/DeleteSupplierEndpoint.cs new file mode 100644 index 0000000..28b4487 --- /dev/null +++ b/PyroFetes/Endpoints/Supplier/DeleteSupplierEndpoint.cs @@ -0,0 +1,33 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; + +namespace PyroFetes.Endpoints.Supplier; + +public class DeleteSupplierRequest +{ + public int Id { get; set; } +} + +public class DeleteSupplierEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Delete("/api/suppliers/{@Id}", x => new {x.Id}); + } + + public override async Task HandleAsync(DeleteSupplierRequest req, CancellationToken ct) + { + var supplier = await database.Suppliers.SingleOrDefaultAsync(x => x.Id == req.Id, ct); + + if (supplier == null) + { + await Send.NotFoundAsync(ct); + return; + } + + database.Suppliers.Remove(supplier); + await database.SaveChangesAsync(ct); + + await Send.NoContentAsync(ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Supplier/GetAllSuppliersEndpoint.cs b/PyroFetes/Endpoints/Supplier/GetAllSuppliersEndpoint.cs new file mode 100644 index 0000000..b550c85 --- /dev/null +++ b/PyroFetes/Endpoints/Supplier/GetAllSuppliersEndpoint.cs @@ -0,0 +1,32 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.PurchaseProduct.Response; +using PyroFetes.DTO.Supplier.Response; + +namespace PyroFetes.Endpoints.Supplier; + +public class GetAllSuppliersEndpoint(PyroFetesDbContext database) : EndpointWithoutRequest> +{ + public override void Configure() + { + Get("/api/suppliers"); + } + + public override async Task HandleAsync(CancellationToken ct) + { + var supplier = await database.Suppliers + .Select(supplier => new GetSupplierDto() + { + Id = supplier.Id, + Name = supplier.Name, + Email = supplier.Email, + Phone = supplier.Phone, + Address = supplier.Address, + City = supplier.City, + ZipCode = supplier.ZipCode, + DeliveryDelay = supplier.DeliveryDelay + }).ToListAsync(ct); + + await Send.OkAsync(supplier, ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Supplier/GetSupplierEndpoint.cs b/PyroFetes/Endpoints/Supplier/GetSupplierEndpoint.cs new file mode 100644 index 0000000..40b6e8e --- /dev/null +++ b/PyroFetes/Endpoints/Supplier/GetSupplierEndpoint.cs @@ -0,0 +1,43 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.Supplier.Response; + +namespace PyroFetes.Endpoints.Supplier; + +public class GetSupplierRequest +{ + public int Id { get; set; } +} + +public class GetSupplierEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Get("/api/suppliers/{@Id}", x => new {x.Id}); + } + + public override async Task HandleAsync(GetSupplierRequest req, CancellationToken ct) + { + var supplier = await database.Suppliers + .SingleOrDefaultAsync(x => x.Id == req.Id, ct); + + if (supplier == null) + { + await Send.NotFoundAsync(ct); + return; + } + + GetSupplierDto responseDto = new() + { + Id = supplier.Id, + Name = supplier.Name, + Email = supplier.Email, + Phone = supplier.Phone, + Address = supplier.Address, + City = supplier.City, + ZipCode = supplier.ZipCode, + DeliveryDelay = supplier.DeliveryDelay + }; + await Send.OkAsync(responseDto, ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Supplier/PatchSupplierDeleveryDelayEndpoint.cs b/PyroFetes/Endpoints/Supplier/PatchSupplierDeleveryDelayEndpoint.cs new file mode 100644 index 0000000..ea31903 --- /dev/null +++ b/PyroFetes/Endpoints/Supplier/PatchSupplierDeleveryDelayEndpoint.cs @@ -0,0 +1,36 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.Supplier.Request; +using PyroFetes.DTO.Supplier.Response; + +namespace PyroFetes.Endpoints.Supplier; + +public class PatchSupplierDeleveryDelayEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Get("/api/supplier/{@Id}/DeleveryDalay", x => new {x.Id}); + } + + public override async Task HandleAsync(PatchSupplierDeliveryDelayDto req, CancellationToken ct) + { + var supplier = await database.Suppliers.SingleOrDefaultAsync(x => x.Id == req.Id, ct); + + if (supplier == null) + { + await Send.NotFoundAsync(ct); + return; + } + + supplier.DeliveryDelay = req.DeliveryDelay; + await database.SaveChangesAsync(ct); + + GetSupplierDto responseDto = new() + { + Id = supplier.Id, + DeliveryDelay = supplier.DeliveryDelay, + }; + + await Send.OkAsync(responseDto, ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Supplier/UpdateSupplierEndpoint.cs b/PyroFetes/Endpoints/Supplier/UpdateSupplierEndpoint.cs new file mode 100644 index 0000000..ab68ebb --- /dev/null +++ b/PyroFetes/Endpoints/Supplier/UpdateSupplierEndpoint.cs @@ -0,0 +1,48 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.Supplier.Request; +using PyroFetes.DTO.Supplier.Response; + +namespace PyroFetes.Endpoints.Supplier; + +public class UpdateSupplierEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Put("/api/suppliers/{@Id}", x => new {x.Id}); + } + + public override async Task HandleAsync(UpdateSupplierDto req, CancellationToken ct) + { + var supplier = await database.Suppliers.SingleOrDefaultAsync(x => x.Id == req.Id, ct); + + if (supplier == null) + { + await Send.NotFoundAsync(ct); + return; + } + + supplier.Name = req.Name; + supplier.Email = req.Email; + supplier.Phone = req.Phone; + supplier.Address = req.Address; + supplier.City = req.City; + supplier.ZipCode = req.ZipCode; + supplier.DeliveryDelay = req.DeliveryDelay; + await database.SaveChangesAsync(ct); + + GetSupplierDto responseDto = new() + { + Id = supplier.Id, + Name = supplier.Name, + Email = supplier.Email, + Phone = supplier.Phone, + Address = supplier.Address, + City = supplier.City, + ZipCode = supplier.ZipCode, + DeliveryDelay = supplier.DeliveryDelay + }; + + await Send.OkAsync(responseDto, ct); + } +} \ No newline at end of file