From a510ab33e2e4eca7f236a2bb28457cd0fc23637d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Radam=C3=A9s=20Iba=C3=B1ez?= Date: Fri, 19 Nov 2021 17:05:12 -0300 Subject: [PATCH] =?UTF-8?q?Primera=20versi=C3=B3n=20de=20la=20actividad=20?= =?UTF-8?q?inicial=20para=20revisi=C3=B3n.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Stock.Api.sln | 34 +++-- Stock.Api/Controllers/ProviderController.cs | 128 +++++++++++++++++++ Stock.Api/DTOs/ProviderDTO.cs | 20 +++ Stock.Api/DTOs/ProviderSearchDTO.cs | 10 ++ Stock.Api/MapperProfiles/ModelProfile.cs | 4 +- Stock.Api/Startup.cs | 2 +- Stock.AppService/Services/ProviderService.cs | 70 ++++++++++ Stock.Test/Stock.Test.csproj | 6 +- 8 files changed, 259 insertions(+), 15 deletions(-) create mode 100644 Stock.Api/Controllers/ProviderController.cs create mode 100644 Stock.Api/DTOs/ProviderDTO.cs create mode 100644 Stock.Api/DTOs/ProviderSearchDTO.cs create mode 100644 Stock.AppService/Services/ProviderService.cs diff --git a/Stock.Api.sln b/Stock.Api.sln index 73342ea..e704af4 100644 --- a/Stock.Api.sln +++ b/Stock.Api.sln @@ -1,15 +1,16 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.26124.0 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31829.152 MinimumVisualStudioVersion = 15.0.26124.0 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Stock.Api", "Stock.Api\Stock.Api.csproj", "{30E6F0DA-17F9-4A05-BD4E-FE5E3214B093}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Stock.Api", "Stock.Api\Stock.Api.csproj", "{30E6F0DA-17F9-4A05-BD4E-FE5E3214B093}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Stock.AppService", "Stock.AppService\Stock.AppService.csproj", "{BEEBC69D-B033-4EC4-B79D-7C323F9CF151}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Stock.AppService", "Stock.AppService\Stock.AppService.csproj", "{BEEBC69D-B033-4EC4-B79D-7C323F9CF151}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Stock.Model", "Stock.Model\Stock.Model.csproj", "{56F2A339-C7A1-4FF0-BBE3-FF83F454AE01}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Stock.Model", "Stock.Model\Stock.Model.csproj", "{56F2A339-C7A1-4FF0-BBE3-FF83F454AE01}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Stock.Repository.LiteDb", "Stock.Repository.LiteDb\Stock.Repository.LiteDb.csproj", "{D3AD2199-3805-45F5-B400-16C02C48A7B3}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Stock.Repository.LiteDb", "Stock.Repository.LiteDb\Stock.Repository.LiteDb.csproj", "{D3AD2199-3805-45F5-B400-16C02C48A7B3}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Stock.Test", "Stock.Test\Stock.Test.csproj", "{1DCE5647-C174-4291-84F1-30734EA90C33}" EndProject Global @@ -21,9 +22,6 @@ Global Release|x64 = Release|x64 Release|x86 = Release|x86 EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {30E6F0DA-17F9-4A05-BD4E-FE5E3214B093}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {30E6F0DA-17F9-4A05-BD4E-FE5E3214B093}.Debug|Any CPU.Build.0 = Debug|Any CPU @@ -73,5 +71,23 @@ Global {D3AD2199-3805-45F5-B400-16C02C48A7B3}.Release|x64.Build.0 = Release|Any CPU {D3AD2199-3805-45F5-B400-16C02C48A7B3}.Release|x86.ActiveCfg = Release|Any CPU {D3AD2199-3805-45F5-B400-16C02C48A7B3}.Release|x86.Build.0 = Release|Any CPU + {1DCE5647-C174-4291-84F1-30734EA90C33}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1DCE5647-C174-4291-84F1-30734EA90C33}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1DCE5647-C174-4291-84F1-30734EA90C33}.Debug|x64.ActiveCfg = Debug|Any CPU + {1DCE5647-C174-4291-84F1-30734EA90C33}.Debug|x64.Build.0 = Debug|Any CPU + {1DCE5647-C174-4291-84F1-30734EA90C33}.Debug|x86.ActiveCfg = Debug|Any CPU + {1DCE5647-C174-4291-84F1-30734EA90C33}.Debug|x86.Build.0 = Debug|Any CPU + {1DCE5647-C174-4291-84F1-30734EA90C33}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1DCE5647-C174-4291-84F1-30734EA90C33}.Release|Any CPU.Build.0 = Release|Any CPU + {1DCE5647-C174-4291-84F1-30734EA90C33}.Release|x64.ActiveCfg = Release|Any CPU + {1DCE5647-C174-4291-84F1-30734EA90C33}.Release|x64.Build.0 = Release|Any CPU + {1DCE5647-C174-4291-84F1-30734EA90C33}.Release|x86.ActiveCfg = Release|Any CPU + {1DCE5647-C174-4291-84F1-30734EA90C33}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B3D353B3-4BDA-435D-8399-2D127E0BD007} EndGlobalSection EndGlobal diff --git a/Stock.Api/Controllers/ProviderController.cs b/Stock.Api/Controllers/ProviderController.cs new file mode 100644 index 0000000..5c84f63 --- /dev/null +++ b/Stock.Api/Controllers/ProviderController.cs @@ -0,0 +1,128 @@ +using AutoMapper; +using Microsoft.AspNetCore.Mvc; +using Stock.Api.DTOs; +using Stock.Api.Extensions; +using Stock.AppService.Services; +using Stock.Model.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading.Tasks; + +namespace Stock.Api.Controllers +{ + /// + /// Provider endpoint. + /// + [Produces("application/json")] + [Route("api/provider")] + [ApiController] + public class ProviderController : ControllerBase + { + private readonly ProviderService service; + private readonly IMapper mapper; + + /// + /// Initializes a new instance of the class. + /// + /// Provider service. + /// Mapper configurator. + public ProviderController(ProviderService service, IMapper mapper) + { + this.service = service ?? throw new ArgumentException(nameof(service)); + this.mapper = mapper ?? throw new ArgumentException(nameof(mapper)); + } + + /// + /// Get all providers. + /// + /// List of + [HttpGet] + public ActionResult> Get() + { + return Ok(mapper.Map>(service.GetAll()).ToList()); + } + + /// + /// Gets a provider by id. + /// + /// Provider id to return. + /// A + [HttpGet("{id}")] + public ActionResult Get(string id) + { + return Ok(mapper.Map(service.Get(id))); + } + + /// + /// Add a provider. + /// + /// Provider information. + [HttpPost] + public Provider Post([FromBody] ProviderDTO value) + { + TryValidateModel(value); + var provider = service.Create(mapper.Map(value)); + return mapper.Map(provider); + } + + /// + /// Updates a provider. + /// + /// Provider id to edit. + /// Provider information. + [HttpPut("{id}")] + public void Put(string id, [FromBody] ProviderDTO value) + { + var provider = service.Get(id); + TryValidateModel(value); + mapper.Map(value, provider); + service.Update(provider); + } + + /// + /// Deletes a provider. + /// + /// Provider id to delete. + [HttpDelete("{id}")] + public ActionResult Delete(string id) + { + var provider = service.Get(id); + + if (provider is null) + return NotFound(); + + service.Delete(provider); + return Ok(); + } + + /// + /// Search some providers. + /// + /// Provider filters. + /// + [HttpPost("search")] + public ActionResult Search([FromBody] ProviderSearchDTO model) + { + Expression> filter = x => !string.IsNullOrWhiteSpace(x.Id); + + if (!string.IsNullOrWhiteSpace(model.Name)) + { + filter = filter.AndOrCustom( + x => x.Name.ToUpper().Contains(model.Name.ToUpper()), + model.Condition.Equals(ActionDto.AND)); + } + + if (!string.IsNullOrWhiteSpace(model.Email)) + { + filter = filter.AndOrCustom( + x => x.Email.ToUpper().Contains(model.Email.ToUpper()), + model.Condition.Equals(ActionDto.AND)); + } + + var providers = service.Search(filter); + return Ok(providers); + } + } +} diff --git a/Stock.Api/DTOs/ProviderDTO.cs b/Stock.Api/DTOs/ProviderDTO.cs new file mode 100644 index 0000000..f02c93a --- /dev/null +++ b/Stock.Api/DTOs/ProviderDTO.cs @@ -0,0 +1,20 @@ +using Stock.Model.Entities; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; + +namespace Stock.Api.DTOs +{ + public class ProviderDTO + { + [Required] + public string Id { get; set; } + + public string Name { get; set; } + + public string Phone { get; set; } + + public string Email { get; set; } + + public List OfferedProducts { get; set; } + } +} \ No newline at end of file diff --git a/Stock.Api/DTOs/ProviderSearchDTO.cs b/Stock.Api/DTOs/ProviderSearchDTO.cs new file mode 100644 index 0000000..fdc3fa5 --- /dev/null +++ b/Stock.Api/DTOs/ProviderSearchDTO.cs @@ -0,0 +1,10 @@ +namespace Stock.Api.DTOs +{ + public class ProviderSearchDTO + { + public string Email { get; set; } + public string Name { get; set; } + + public ActionDto Condition { get; set; } + } +} \ No newline at end of file diff --git a/Stock.Api/MapperProfiles/ModelProfile.cs b/Stock.Api/MapperProfiles/ModelProfile.cs index fba627b..12c8810 100644 --- a/Stock.Api/MapperProfiles/ModelProfile.cs +++ b/Stock.Api/MapperProfiles/ModelProfile.cs @@ -25,8 +25,8 @@ public ModelProfile() // .ForMember(s => s.Id, opt => opt.Ignore()) // .ForMember(s => s.ProductType, opt => opt.Ignore()); - // CreateMap() - // .ReverseMap(); + CreateMap() + .ReverseMap(); } } diff --git a/Stock.Api/Startup.cs b/Stock.Api/Startup.cs index 2a5e309..8a6da6c 100644 --- a/Stock.Api/Startup.cs +++ b/Stock.Api/Startup.cs @@ -30,7 +30,7 @@ public void ConfigureServices(IServiceCollection services) services.Configure(Configuration.GetSection("DomainSettings")); services.AddTransient(); //services.AddTransient(); - //services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/Stock.AppService/Services/ProviderService.cs b/Stock.AppService/Services/ProviderService.cs new file mode 100644 index 0000000..9164106 --- /dev/null +++ b/Stock.AppService/Services/ProviderService.cs @@ -0,0 +1,70 @@ +using Stock.AppService.Base; +using Stock.Model.Entities; +using Stock.Repository.LiteDb.Interface; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using System.Threading.Tasks; + +namespace Stock.AppService.Services +{ + /// + /// Provider service. + /// + public class ProviderService : BaseService + { + /// + /// Initializes a new instance of the class. + /// + /// Provider repository. + public ProviderService(IRepository repository) + : base(repository) + { + } + + /// + /// Creates a provider. + /// + /// Provider information. + /// + /// + public new Provider Create(Provider entity) + { + if (NombreUnico(entity.Name)) + { + return base.Create(entity); + } + + throw new Exception("The name is already in use"); + } + + /// + /// Checks if the provider name is unique or not. + /// + /// Provider name to check. + /// + private bool NombreUnico(string name) + { + if (string.IsNullOrWhiteSpace(name)) + { + return false; + } + + return Repository.List(x => x.Name.ToUpper().Equals(name.ToUpper())).Count == 0; + } + + /// + /// Search stores. + /// + /// + /// + public IEnumerable Search(Expression> filter) + { + return Repository.List(filter); + } + + + } +} diff --git a/Stock.Test/Stock.Test.csproj b/Stock.Test/Stock.Test.csproj index c5b05ee..4bc0f72 100644 --- a/Stock.Test/Stock.Test.csproj +++ b/Stock.Test/Stock.Test.csproj @@ -7,9 +7,9 @@ - - - + + +