diff --git a/Stock.Api/Controllers/ProviderController.cs b/Stock.Api/Controllers/ProviderController.cs
new file mode 100644
index 0000000..3ba03ef
--- /dev/null
+++ b/Stock.Api/Controllers/ProviderController.cs
@@ -0,0 +1,157 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Linq.Expressions;
+using AutoMapper;
+using Microsoft.AspNetCore.Mvc;
+using Stock.Api.DTOs;
+using Stock.Api.Extensions;
+using Stock.AppService.Services;
+using Stock.Model.Entities;
+using Microsoft.Extensions.Logging;
+
+namespace Stock.Api.Controllers
+{
+ ///
+ /// Provider endpoint.
+ ///
+ [Produces("application/json")]
+ [Route("api/provider")]
+ [ApiController]
+ public class ProviderController : ControllerBase
+ {
+ private readonly ProviderService service;
+ private readonly ILogger logger;
+ private readonly IMapper mapper;
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// Provider service.
+ /// Mapper configurator.
+ /// Logger service.
+ public ProviderController(ProviderService service, IMapper mapper, ILogger logger)
+ {
+ this.service = service ?? throw new ArgumentException(nameof(service));
+ this.mapper = mapper ?? throw new ArgumentException(nameof(mapper));
+ this.logger = logger ?? throw new ArgumentException(nameof(logger));
+ }
+
+ ///
+ /// Adds a provider.
+ ///
+ /// Provider info.
+ ///
+ [HttpPost]
+ public ActionResult Post([FromBody] ProviderDTO value)
+ {
+ TryValidateModel(value);
+
+ try
+ {
+ var provider = mapper.Map(value);
+ service.Create(provider);
+ value.Id = provider.Id;
+ return Ok(new { Success = true, Message = "", data = value });
+ }
+ catch (Exception ex)
+ {
+ logger.LogCritical(ex.StackTrace);
+ return Ok(new { Success = false, Message = "The name is already in use" });
+ }
+ }
+
+ ///
+ /// Gets all providers.
+ ///
+ ///
+ [HttpGet]
+ public ActionResult> Get()
+ {
+ try
+ {
+ var result = service.GetAll();
+ return mapper.Map>(result).ToList();
+ }
+ catch (Exception)
+ {
+ return StatusCode(500);
+ }
+ }
+
+ ///
+ /// Gets a provider by id.
+ ///
+ /// Provider id.
+ ///
+ [HttpGet("{id}")]
+ public ActionResult Get(string id)
+ {
+ try
+ {
+ var result = service.Get(id);
+ return mapper.Map(result);
+ }
+ catch (Exception)
+ {
+ return StatusCode(500);
+ }
+ }
+
+ ///
+ /// Updates a provider.
+ ///
+ /// Provider id.
+ /// 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(new { Success = true, Message = "", data = id });
+ }
+
+ ///
+ /// 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);
+ }
+ }
+}
\ No newline at end of file
diff --git a/Stock.Api/DTOs/ProviderDTO.cs b/Stock.Api/DTOs/ProviderDTO.cs
new file mode 100644
index 0000000..643acac
--- /dev/null
+++ b/Stock.Api/DTOs/ProviderDTO.cs
@@ -0,0 +1,18 @@
+using System.ComponentModel.DataAnnotations;
+
+namespace Stock.Api.DTOs
+{
+ public class ProviderDTO
+ {
+
+ public string Id { get; set; }
+
+ [Required]
+ public string Name { get; set; }
+
+ public string Phone { get; set; }
+
+ public string Email{ 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..ff11d03
--- /dev/null
+++ b/Stock.Api/DTOs/ProviderSearchDTO.cs
@@ -0,0 +1,12 @@
+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..14981ac 100644
--- a/Stock.Api/MapperProfiles/ModelProfile.cs
+++ b/Stock.Api/MapperProfiles/ModelProfile.cs
@@ -25,10 +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..6bfde54 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();
@@ -40,6 +40,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddTransient, BaseRepository>();
services.AddTransient, BaseRepository>();
+
services.AddControllers();
services.AddAutoMapper(typeof(Startup).Assembly);
diff --git a/Stock.AppService/Services/ProviderService.cs b/Stock.AppService/Services/ProviderService.cs
new file mode 100644
index 0000000..f12ab9e
--- /dev/null
+++ b/Stock.AppService/Services/ProviderService.cs
@@ -0,0 +1,64 @@
+using System;
+using System.Collections.Generic;
+using System.Linq.Expressions;
+using Stock.AppService.Base;
+using Stock.Model.Entities;
+using Stock.Repository.LiteDb.Interface;
+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 providers.
+ ///
+ ///
+ ///
+ public IEnumerable Search(Expression> filter)
+ {
+ return Repository.List(filter);
+ }
+ }
+}
\ No newline at end of file