From e944f1b563e046d5eae9c47fb8de1c1f8a5fa2c3 Mon Sep 17 00:00:00 2001 From: Thiago Citeli Date: Sun, 17 Apr 2022 16:08:13 -0300 Subject: [PATCH] Beleza na web api project --- .../BelezaNaWeb.Domain.csproj | 15 +++ .../Constants/WareHouseTypeConstants.cs | 8 ++ .../BelezaNaWeb.Domain/Entities/Inventory.cs | 16 +++ .../BelezaNaWeb.Domain/Entities/Product.cs | 18 +++ .../BelezaNaWeb.Domain/Entities/Warehouse.cs | 9 ++ .../Response/BaseResponse.cs | 16 +++ .../BelezaNaWeb.Infrastructure.csproj | 9 ++ .../BelezaNaWeb.Repository.csproj | 13 +++ .../IProductRepository.cs | 13 +++ .../ProductRepository.cs | 34 ++++++ .../BelezaNaWeb.Services.csproj | 14 +++ .../BelezaNaWeb.Services/IProductService.cs | 14 +++ .../BelezaNaWeb.Services/ProductService.cs | 109 ++++++++++++++++++ .../BelezaNaWeb.Tests.csproj | 27 +++++ .../UnitTests/Product/ProductTest.cs | 55 +++++++++ BelezaNaWeb/BelezaNaWeb.sln | 66 +++++++++++ .../BelezaNaWeb/BelezaNaWeb.WebApi.csproj | 18 +++ .../Controllers/ProductController.cs | 96 +++++++++++++++ BelezaNaWeb/BelezaNaWeb/Program.cs | 47 ++++++++ .../BelezaNaWeb/appsettings.Development.json | 8 ++ BelezaNaWeb/BelezaNaWeb/appsettings.json | 9 ++ BelezaNaWeb/IRepositoryProduct.cs | 13 +++ 22 files changed, 627 insertions(+) create mode 100644 BelezaNaWeb/BelezaNaWeb.Domain/BelezaNaWeb.Domain.csproj create mode 100644 BelezaNaWeb/BelezaNaWeb.Domain/Constants/WareHouseTypeConstants.cs create mode 100644 BelezaNaWeb/BelezaNaWeb.Domain/Entities/Inventory.cs create mode 100644 BelezaNaWeb/BelezaNaWeb.Domain/Entities/Product.cs create mode 100644 BelezaNaWeb/BelezaNaWeb.Domain/Entities/Warehouse.cs create mode 100644 BelezaNaWeb/BelezaNaWeb.Domain/Response/BaseResponse.cs create mode 100644 BelezaNaWeb/BelezaNaWeb.Infrastructure/BelezaNaWeb.Infrastructure.csproj create mode 100644 BelezaNaWeb/BelezaNaWeb.Repository/BelezaNaWeb.Repository.csproj create mode 100644 BelezaNaWeb/BelezaNaWeb.Repository/IProductRepository.cs create mode 100644 BelezaNaWeb/BelezaNaWeb.Repository/ProductRepository.cs create mode 100644 BelezaNaWeb/BelezaNaWeb.Services/BelezaNaWeb.Services.csproj create mode 100644 BelezaNaWeb/BelezaNaWeb.Services/IProductService.cs create mode 100644 BelezaNaWeb/BelezaNaWeb.Services/ProductService.cs create mode 100644 BelezaNaWeb/BelezaNaWeb.Tests/BelezaNaWeb.Tests.csproj create mode 100644 BelezaNaWeb/BelezaNaWeb.Tests/UnitTests/Product/ProductTest.cs create mode 100644 BelezaNaWeb/BelezaNaWeb.sln create mode 100644 BelezaNaWeb/BelezaNaWeb/BelezaNaWeb.WebApi.csproj create mode 100644 BelezaNaWeb/BelezaNaWeb/Controllers/ProductController.cs create mode 100644 BelezaNaWeb/BelezaNaWeb/Program.cs create mode 100644 BelezaNaWeb/BelezaNaWeb/appsettings.Development.json create mode 100644 BelezaNaWeb/BelezaNaWeb/appsettings.json create mode 100644 BelezaNaWeb/IRepositoryProduct.cs diff --git a/BelezaNaWeb/BelezaNaWeb.Domain/BelezaNaWeb.Domain.csproj b/BelezaNaWeb/BelezaNaWeb.Domain/BelezaNaWeb.Domain.csproj new file mode 100644 index 0000000..d9c73b0 --- /dev/null +++ b/BelezaNaWeb/BelezaNaWeb.Domain/BelezaNaWeb.Domain.csproj @@ -0,0 +1,15 @@ + + + + net6.0 + enable + enable + + + + + C:\Program Files\dotnet\packs\Microsoft.AspNetCore.App.Ref\6.0.4\ref\net6.0\Microsoft.Net.Http.Headers.dll + + + + diff --git a/BelezaNaWeb/BelezaNaWeb.Domain/Constants/WareHouseTypeConstants.cs b/BelezaNaWeb/BelezaNaWeb.Domain/Constants/WareHouseTypeConstants.cs new file mode 100644 index 0000000..113e965 --- /dev/null +++ b/BelezaNaWeb/BelezaNaWeb.Domain/Constants/WareHouseTypeConstants.cs @@ -0,0 +1,8 @@ +namespace BelezaNaWeb.Domain.Constants +{ + public static class WareHouseTypeConstants + { + public const string ECOMMERCE = "ECOMMERCE"; + public const string PHYSICAL_STORE = "PHYSICAL_STORE"; + } +} diff --git a/BelezaNaWeb/BelezaNaWeb.Domain/Entities/Inventory.cs b/BelezaNaWeb/BelezaNaWeb.Domain/Entities/Inventory.cs new file mode 100644 index 0000000..c55b78e --- /dev/null +++ b/BelezaNaWeb/BelezaNaWeb.Domain/Entities/Inventory.cs @@ -0,0 +1,16 @@ +namespace BelezaNaWeb.Domain.Entities +{ + public class Inventory + { + public int Quantity { get; } + public List Warehouses { get; } + + public Inventory(List warehouses) + { + this.Warehouses = warehouses; + Quantity = SetQuantity(this.Warehouses); + } + + private static int SetQuantity(IEnumerable wh) => wh.Sum(item => item.Quantity); + } +} \ No newline at end of file diff --git a/BelezaNaWeb/BelezaNaWeb.Domain/Entities/Product.cs b/BelezaNaWeb/BelezaNaWeb.Domain/Entities/Product.cs new file mode 100644 index 0000000..d5f3853 --- /dev/null +++ b/BelezaNaWeb/BelezaNaWeb.Domain/Entities/Product.cs @@ -0,0 +1,18 @@ +namespace BelezaNaWeb.Domain.Entities +{ + public class Product + { + public int Sku { get; set; } + public string Name { get; set; } + public Inventory Inventory { get; } + public bool IsMarketable { get; } + + public Product(Inventory inventory) + { + Inventory = inventory; + IsMarketable = SetMarketable(inventory); + } + + private static bool SetMarketable(Inventory inventory) => inventory.Quantity > 0; + } +} diff --git a/BelezaNaWeb/BelezaNaWeb.Domain/Entities/Warehouse.cs b/BelezaNaWeb/BelezaNaWeb.Domain/Entities/Warehouse.cs new file mode 100644 index 0000000..9b80960 --- /dev/null +++ b/BelezaNaWeb/BelezaNaWeb.Domain/Entities/Warehouse.cs @@ -0,0 +1,9 @@ +namespace BelezaNaWeb.Domain.Entities +{ + public class Warehouse + { + public string Locality { get; set; } + public int Quantity { get; set; } + public string Type { get; set; } + } +} \ No newline at end of file diff --git a/BelezaNaWeb/BelezaNaWeb.Domain/Response/BaseResponse.cs b/BelezaNaWeb/BelezaNaWeb.Domain/Response/BaseResponse.cs new file mode 100644 index 0000000..ed71aa2 --- /dev/null +++ b/BelezaNaWeb/BelezaNaWeb.Domain/Response/BaseResponse.cs @@ -0,0 +1,16 @@ +using System.Net; + +namespace BelezaNaWeb.Domain.Response +{ + public class BaseResponse + { + public HttpStatusCode HttpStatusCode { get; set; } + public string Message { get; set; } + public List Data { get; set; } + + public BaseResponse() + { + Data = new List(); + } + } +} diff --git a/BelezaNaWeb/BelezaNaWeb.Infrastructure/BelezaNaWeb.Infrastructure.csproj b/BelezaNaWeb/BelezaNaWeb.Infrastructure/BelezaNaWeb.Infrastructure.csproj new file mode 100644 index 0000000..132c02c --- /dev/null +++ b/BelezaNaWeb/BelezaNaWeb.Infrastructure/BelezaNaWeb.Infrastructure.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + diff --git a/BelezaNaWeb/BelezaNaWeb.Repository/BelezaNaWeb.Repository.csproj b/BelezaNaWeb/BelezaNaWeb.Repository/BelezaNaWeb.Repository.csproj new file mode 100644 index 0000000..9c11e04 --- /dev/null +++ b/BelezaNaWeb/BelezaNaWeb.Repository/BelezaNaWeb.Repository.csproj @@ -0,0 +1,13 @@ + + + + net6.0 + enable + enable + + + + + + + diff --git a/BelezaNaWeb/BelezaNaWeb.Repository/IProductRepository.cs b/BelezaNaWeb/BelezaNaWeb.Repository/IProductRepository.cs new file mode 100644 index 0000000..105470b --- /dev/null +++ b/BelezaNaWeb/BelezaNaWeb.Repository/IProductRepository.cs @@ -0,0 +1,13 @@ +using BelezaNaWeb.Domain.Entities; + +namespace BelezaNaWeb.Repository +{ + public interface IProductRepository + { + Product? GetBySku(int sku); + List GetProducts(); + Product Create(Product product); + void UpdateBySku(int sku, Product product); + void DeleteBySku(int sku); + } +} diff --git a/BelezaNaWeb/BelezaNaWeb.Repository/ProductRepository.cs b/BelezaNaWeb/BelezaNaWeb.Repository/ProductRepository.cs new file mode 100644 index 0000000..d16f264 --- /dev/null +++ b/BelezaNaWeb/BelezaNaWeb.Repository/ProductRepository.cs @@ -0,0 +1,34 @@ +using BelezaNaWeb.Domain.Entities; + +namespace BelezaNaWeb.Repository +{ + public class ProductRepository : IProductRepository + { + private static readonly List Products = new(); + + public Product? GetBySku(int sku) => Products.FirstOrDefault(p => p.Sku == sku); + + public List GetProducts() => Products.ToList(); + + public Product Create(Product? product) + { + if (Products.All(p => p.Sku != product.Sku)) + Products.Add(product); + return product; + } + + public void UpdateBySku(int sku, Product newProduct) + { + var currentProduct = GetBySku(sku); + if (currentProduct is not null) return; + if (newProduct != null && sku != newProduct.Sku) return; + DeleteBySku(sku); + Products.Add(newProduct); + } + + public void DeleteBySku(int sku) + { + Products.RemoveAll(p => p.Sku == sku); + } + } +} diff --git a/BelezaNaWeb/BelezaNaWeb.Services/BelezaNaWeb.Services.csproj b/BelezaNaWeb/BelezaNaWeb.Services/BelezaNaWeb.Services.csproj new file mode 100644 index 0000000..4bd51d3 --- /dev/null +++ b/BelezaNaWeb/BelezaNaWeb.Services/BelezaNaWeb.Services.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/BelezaNaWeb/BelezaNaWeb.Services/IProductService.cs b/BelezaNaWeb/BelezaNaWeb.Services/IProductService.cs new file mode 100644 index 0000000..e3dc818 --- /dev/null +++ b/BelezaNaWeb/BelezaNaWeb.Services/IProductService.cs @@ -0,0 +1,14 @@ +using BelezaNaWeb.Domain.Entities; +using BelezaNaWeb.Domain.Response; + +namespace BelezaNaWeb.Services +{ + public interface IProductService + { + Task> CreateProduct(Product? product); + Task> DeleteProductBySku(int sku); + Task> GetAllProducts(); + Task> GetBySku(int sku); + Task> UpdateBySku(int sku, Product? product); + } +} diff --git a/BelezaNaWeb/BelezaNaWeb.Services/ProductService.cs b/BelezaNaWeb/BelezaNaWeb.Services/ProductService.cs new file mode 100644 index 0000000..3e07ca0 --- /dev/null +++ b/BelezaNaWeb/BelezaNaWeb.Services/ProductService.cs @@ -0,0 +1,109 @@ +using System.Net; +using BelezaNaWeb.Domain.Entities; +using BelezaNaWeb.Domain.Response; +using BelezaNaWeb.Repository; + +namespace BelezaNaWeb.Services +{ + public class ProductService : IProductService + { + private readonly IProductRepository _productRepository; + + public ProductService(IProductRepository productRepository) + { + _productRepository = productRepository; + } + + public async Task> CreateProduct(Product? product) + { + var response = new BaseResponse(); + + if (product is not null && _productRepository.GetBySku(product.Sku) is null) + { + response.Data.Add(_productRepository.Create(product)); + response.Message = "Product has been created"; + response.HttpStatusCode = HttpStatusCode.OK; + } + else + { + throw new Exception($"Product already exist with this sku: {product.Sku}"); + } + + return response; + } + + public async Task> DeleteProductBySku(int sku) + { + var response = new BaseResponse(); + + if (_productRepository.GetBySku(sku) is not null) + { + _productRepository.DeleteBySku(sku); + response.HttpStatusCode = HttpStatusCode.OK; + response.Message = "Product has been deleted"; + response.Data.Add(true); + } + else + { + response.HttpStatusCode = HttpStatusCode.NotFound; + response.Data.Add(false); + response.Message = "Product not found"; + } + + return response; + } + + public async Task> GetAllProducts() + { + var response = new BaseResponse(); + var helper = _productRepository.GetProducts(); + if (helper is not null) + { + response.Data.AddRange(helper); + } + response.HttpStatusCode = HttpStatusCode.OK; + response.Message = "Products has been retrieved"; + + return response; + } + + public async Task> GetBySku(int sku) + { + var response = new BaseResponse(); + var helper = _productRepository.GetBySku(sku); + if (helper is not null) + { + response.Data.Add(helper); + response.HttpStatusCode = HttpStatusCode.OK; + response.Message = "Product has been retrieved"; + } + else + { + response.HttpStatusCode = HttpStatusCode.NotFound; + response.Message = "Product not found"; + } + + + return response; + } + public async Task> UpdateBySku(int sku, Product? product) + { + var response = new BaseResponse(); + var helper = _productRepository.GetBySku(sku); + if (helper is not null && product is not null) + { + _productRepository.UpdateBySku(sku, product); + response.Data.Add(true); + response.HttpStatusCode = HttpStatusCode.OK; + response.Message = "Product updated"; + } + else + { + response.HttpStatusCode = HttpStatusCode.NotFound; + response.Message = "Product Not Fount"; + } + + return response; + } + } +} \ No newline at end of file diff --git a/BelezaNaWeb/BelezaNaWeb.Tests/BelezaNaWeb.Tests.csproj b/BelezaNaWeb/BelezaNaWeb.Tests/BelezaNaWeb.Tests.csproj new file mode 100644 index 0000000..c268944 --- /dev/null +++ b/BelezaNaWeb/BelezaNaWeb.Tests/BelezaNaWeb.Tests.csproj @@ -0,0 +1,27 @@ + + + + net6.0 + enable + + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/BelezaNaWeb/BelezaNaWeb.Tests/UnitTests/Product/ProductTest.cs b/BelezaNaWeb/BelezaNaWeb.Tests/UnitTests/Product/ProductTest.cs new file mode 100644 index 0000000..051657a --- /dev/null +++ b/BelezaNaWeb/BelezaNaWeb.Tests/UnitTests/Product/ProductTest.cs @@ -0,0 +1,55 @@ +using System.Collections.Generic; +using System.Linq; +using BelezaNaWeb.Domain.Constants; +using BelezaNaWeb.Domain.Entities; +using BelezaNaWeb.Repository; +using Xunit; + +namespace BelezaNaWeb.Tests.UnitTests.Product +{ + public class ProductTest + { + private readonly ProductRepository _productRepository; + private Domain.Entities.Product _product; + + public ProductTest() + { + _productRepository = new ProductRepository(); + + var warehouse1 = new Warehouse { Locality = "SP", Quantity = 10, Type = WareHouseTypeConstants.ECOMMERCE }; + var warehouse2 = new Warehouse { Locality = "RJ", Quantity = 12, Type = WareHouseTypeConstants.PHYSICAL_STORE }; + _product = _productRepository.Create(new Domain.Entities.Product(new Inventory(new List { warehouse1, warehouse2 })) { Name = "L'Oréal Professionnel Exp...", Sku = 43264 }); + } + + [Fact] + public void Get_Product_By_Valid_Sku() + { + Assert.True(_productRepository.GetBySku(43264) is not null); + } + + [Fact] + public void Get_Product_By_Invalid_Sku() + { + Assert.False(_productRepository.GetBySku(33) is not null); + } + + [Fact] + public void Delete_Product_By_Valid_Sku_And_Verify_Deleted_Product() + { + _productRepository.DeleteBySku(43264); + var product = _productRepository.GetBySku(43264); + + Assert.True(product is null); + } + + [Fact] + public void Verify_Inventory_Quantity_Is_Calculated_Correctly() + { + var product = _productRepository.GetBySku(43264); + var inventoryQuantity = product.Inventory.Quantity; + var wareHousesQuantity = product.Inventory.Warehouses.Sum(inventoryWarehouse => inventoryWarehouse.Quantity); + + Assert.Equal(inventoryQuantity, wareHousesQuantity); + } + } +} diff --git a/BelezaNaWeb/BelezaNaWeb.sln b/BelezaNaWeb/BelezaNaWeb.sln new file mode 100644 index 0000000..d1cd45d --- /dev/null +++ b/BelezaNaWeb/BelezaNaWeb.sln @@ -0,0 +1,66 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.1.32407.343 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BelezaNaWeb.WebApi", "BelezaNaWeb\BelezaNaWeb.WebApi.csproj", "{38EC0641-A357-4EA1-B6A3-B28B21DE1F12}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "1 - Application", "1 - Application", "{B99545BC-79EC-46ED-8C63-4254D85ABDDD}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "2 - Services", "2 - Services", "{D972B993-B8B6-4D8E-B0B0-CF6A373E3BBE}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "3 - Domain", "3 - Domain", "{6D45D1C6-EF64-442E-8FE4-14279DBE2F09}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "4 - Infrastructure", "4 - Infrastructure", "{D99D718D-F49A-4A95-8295-B37448B9DC15}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "5 - Tests", "5 - Tests", "{5DEC08B2-81C5-493F-BE9C-E901D429A4A0}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BelezaNaWeb.Services", "BelezaNaWeb.Services\BelezaNaWeb.Services.csproj", "{79001677-E405-4BB0-A5AA-AFE9AD12051A}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BelezaNaWeb.Domain", "BelezaNaWeb.Domain\BelezaNaWeb.Domain.csproj", "{0ACE3D63-28C2-40E9-810A-68A75C3B87F9}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BelezaNaWeb.Tests", "BelezaNaWeb.Tests\BelezaNaWeb.Tests.csproj", "{2EB5ED53-12B8-4DBF-8AB4-F87245100FDC}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BelezaNaWeb.Repository", "BelezaNaWeb.Repository\BelezaNaWeb.Repository.csproj", "{8ADB1167-1FF4-41D4-AF24-687FE45DC195}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {38EC0641-A357-4EA1-B6A3-B28B21DE1F12}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {38EC0641-A357-4EA1-B6A3-B28B21DE1F12}.Debug|Any CPU.Build.0 = Debug|Any CPU + {38EC0641-A357-4EA1-B6A3-B28B21DE1F12}.Release|Any CPU.ActiveCfg = Release|Any CPU + {38EC0641-A357-4EA1-B6A3-B28B21DE1F12}.Release|Any CPU.Build.0 = Release|Any CPU + {79001677-E405-4BB0-A5AA-AFE9AD12051A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {79001677-E405-4BB0-A5AA-AFE9AD12051A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {79001677-E405-4BB0-A5AA-AFE9AD12051A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {79001677-E405-4BB0-A5AA-AFE9AD12051A}.Release|Any CPU.Build.0 = Release|Any CPU + {0ACE3D63-28C2-40E9-810A-68A75C3B87F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0ACE3D63-28C2-40E9-810A-68A75C3B87F9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0ACE3D63-28C2-40E9-810A-68A75C3B87F9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0ACE3D63-28C2-40E9-810A-68A75C3B87F9}.Release|Any CPU.Build.0 = Release|Any CPU + {2EB5ED53-12B8-4DBF-8AB4-F87245100FDC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2EB5ED53-12B8-4DBF-8AB4-F87245100FDC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2EB5ED53-12B8-4DBF-8AB4-F87245100FDC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2EB5ED53-12B8-4DBF-8AB4-F87245100FDC}.Release|Any CPU.Build.0 = Release|Any CPU + {8ADB1167-1FF4-41D4-AF24-687FE45DC195}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8ADB1167-1FF4-41D4-AF24-687FE45DC195}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8ADB1167-1FF4-41D4-AF24-687FE45DC195}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8ADB1167-1FF4-41D4-AF24-687FE45DC195}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {38EC0641-A357-4EA1-B6A3-B28B21DE1F12} = {B99545BC-79EC-46ED-8C63-4254D85ABDDD} + {79001677-E405-4BB0-A5AA-AFE9AD12051A} = {D972B993-B8B6-4D8E-B0B0-CF6A373E3BBE} + {0ACE3D63-28C2-40E9-810A-68A75C3B87F9} = {6D45D1C6-EF64-442E-8FE4-14279DBE2F09} + {2EB5ED53-12B8-4DBF-8AB4-F87245100FDC} = {5DEC08B2-81C5-493F-BE9C-E901D429A4A0} + {8ADB1167-1FF4-41D4-AF24-687FE45DC195} = {D99D718D-F49A-4A95-8295-B37448B9DC15} + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {7F5F3DB7-CF2C-4983-97F7-F99AC2CF138D} + EndGlobalSection +EndGlobal diff --git a/BelezaNaWeb/BelezaNaWeb/BelezaNaWeb.WebApi.csproj b/BelezaNaWeb/BelezaNaWeb/BelezaNaWeb.WebApi.csproj new file mode 100644 index 0000000..e5731bb --- /dev/null +++ b/BelezaNaWeb/BelezaNaWeb/BelezaNaWeb.WebApi.csproj @@ -0,0 +1,18 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + + diff --git a/BelezaNaWeb/BelezaNaWeb/Controllers/ProductController.cs b/BelezaNaWeb/BelezaNaWeb/Controllers/ProductController.cs new file mode 100644 index 0000000..ff7041d --- /dev/null +++ b/BelezaNaWeb/BelezaNaWeb/Controllers/ProductController.cs @@ -0,0 +1,96 @@ +using System.Net; +using BelezaNaWeb.Domain.Entities; +using BelezaNaWeb.Domain.Response; +using BelezaNaWeb.Services; +using Microsoft.AspNetCore.Mvc; + +namespace BelezaNaWeb.WebApi.Controllers +{ + [ApiController] + [Route("api/[controller]")] + public class ProductController : ControllerBase + { + private readonly IProductService _productService; + + public ProductController(IProductService productService) + { + _productService = productService; + } + + [HttpPost("Create")] + public async Task>> Create([FromBody] Product? product) + { + var response = new BaseResponse(); + try + { + response = await _productService.CreateProduct(product); + return StatusCode((int)response.HttpStatusCode, response); + } + catch (Exception ex) + { + response.HttpStatusCode = HttpStatusCode.InternalServerError; + throw ex; + } + } + + [HttpGet("GetAll")] + public async Task>> GetAll() + { + var response = new BaseResponse(); + try + { + response = await _productService.GetAllProducts(); + return StatusCode((int)response.HttpStatusCode, response); + } + catch (Exception ex) + { + throw ex; + } + } + + [HttpGet("GetBySku")] + public async Task>> GetBySku(int sku) + { + var response = new BaseResponse(); + try + { + response = await _productService.GetBySku(sku); + return StatusCode((int)response.HttpStatusCode, response); + } + catch (Exception ex) + { + throw ex; + } + } + + [HttpDelete("DeleteBySku")] + public async Task>> DeleteBySku(int sku) + { + var response = new BaseResponse(); + try + { + response = await _productService.DeleteProductBySku(sku); + return StatusCode((int)response.HttpStatusCode, response); + } + catch (Exception ex) + { + throw ex; + } + } + + [HttpPut("UpdateBySku")] + public async Task>> UpdateBySku(int sku, [FromBody] Product? product) + { + var response = new BaseResponse(); + try + { + response = await _productService.UpdateBySku(sku, product); + return StatusCode((int)response.HttpStatusCode, response); + } + catch (Exception ex) + { + throw ex; + } + } + } +} \ No newline at end of file diff --git a/BelezaNaWeb/BelezaNaWeb/Program.cs b/BelezaNaWeb/BelezaNaWeb/Program.cs new file mode 100644 index 0000000..ab91056 --- /dev/null +++ b/BelezaNaWeb/BelezaNaWeb/Program.cs @@ -0,0 +1,47 @@ +using BelezaNaWeb.Domain.Entities; +using BelezaNaWeb.Domain.Constants; +using BelezaNaWeb.Repository; +using BelezaNaWeb.Services; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +builder.Services.AddControllers(); +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseHttpsRedirection(); + +app.UseAuthorization(); + +app.MapControllers(); + +#region Mock + + var productRepository = new ProductRepository(); + var warehouse1 = new Warehouse { Locality = "SP", Quantity = 10, Type = WareHouseTypeConstants.ECOMMERCE }; + var warehouse2 = new Warehouse { Locality = "RJ", Quantity = 12, Type = WareHouseTypeConstants.ECOMMERCE }; + var warehouse3 = new Warehouse { Locality = "RJ", Quantity = 0, Type = WareHouseTypeConstants.PHYSICAL_STORE }; + var warehouse4 = new Warehouse { Locality = "MOEMA", Quantity = 0, Type = WareHouseTypeConstants.ECOMMERCE }; + + productRepository.Create(new Product(new Inventory(new List { warehouse1, warehouse2 })) { Name = "prod1", Sku = 123 }); + productRepository.Create(new Product(new Inventory(new List { warehouse3, warehouse4 })) { Name = "prod2", Sku = 321 }); + productRepository.Create(new Product(new Inventory(new List { warehouse1, warehouse2, warehouse3 })) { Name = "prod3", Sku = 444 }); + +#endregion + + +app.Run(); \ No newline at end of file diff --git a/BelezaNaWeb/BelezaNaWeb/appsettings.Development.json b/BelezaNaWeb/BelezaNaWeb/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/BelezaNaWeb/BelezaNaWeb/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/BelezaNaWeb/BelezaNaWeb/appsettings.json b/BelezaNaWeb/BelezaNaWeb/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/BelezaNaWeb/BelezaNaWeb/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/BelezaNaWeb/IRepositoryProduct.cs b/BelezaNaWeb/IRepositoryProduct.cs new file mode 100644 index 0000000..65c9aa2 --- /dev/null +++ b/BelezaNaWeb/IRepositoryProduct.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Infrastructure +{ + public interface IRepositoryProduct + { + + } +}