diff --git a/Stock.Api/Controllers/ProviderController.cs b/Stock.Api/Controllers/ProviderController.cs new file mode 100644 index 0000000..9420600 --- /dev/null +++ b/Stock.Api/Controllers/ProviderController.cs @@ -0,0 +1,133 @@ +using AutoMapper; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Routing; +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; + +namespace Stock.Api.Controllers +{ + /// + /// This controller manage all CRUD for a provider + /// + [Produces("application/json")] + [Route("api/provider")] + [ApiController] + + public class ProviderController : ControllerBase + { + private readonly ProviderService service; + private readonly IMapper mapper; + /// + /// Main constructor + /// + /// CRUD and search service for a provider + /// Mapper configurator + public ProviderController(ProviderService providerService, IMapper mapper) + { + this.service = providerService ?? throw new ArgumentException(nameof(providerService)); + this.mapper = mapper ?? throw new ArgumentException(nameof(mapper)); + + + } + + /// + /// Get all provider + /// + /// Return a list of provider + [HttpGet] + public ActionResult> Get() + { + return (mapper.Map>(service.GetAll()).ToList()); + } + + /// + /// Get provider by provider id + /// + /// Represent an id of a provider + /// Return a provider + [HttpGet("{id}")] + + public ActionResult Get(string id) + { + return this.mapper.Map(service.Get(id)); + } + + /// + /// Create a new provider + /// + /// Represent all information need it for a provider + /// Return a provider and its provider id + [HttpPost] + + public Provider Post([FromBody] ProviderDTO value) + { + TryValidateModel(value); + var provider = service.Create(mapper.Map(value)); + return mapper.Map(provider); + + } + + /// + /// Update a provider by provider id + /// + /// Represent an id of a provider + /// Represent all information need it for a provider + [HttpPut("{id}")] + + public void Put(string id, [FromBody] ProviderDTO value) + { + var provider = service.Get(id); + TryValidateModel(value); + + mapper.Map(value, provider); + + service.Update(provider); + } + + /// + /// Delete a provider by provider id + /// + /// Represent an id of a provider + /// + [HttpDelete("{id}")] + + public ActionResult Delete(string id) + { + var provider = service.Get(id); + if (provider == null) { return NotFound(); } + + service.Delete(provider); + return Ok(); + } + + /// + /// Search a provider by criteria + /// + /// Represent a search of provider by name + /// Return a list of provider by criteria + [HttpPost("search")] + + public ActionResult> Search([FromBody] ProviderSearchDTO dto) + { + Expression> filter = x => dto != null; + if (!string.IsNullOrWhiteSpace(dto.Name)) + { + filter = filter.AndOrCustom( + x => x.Name.ToUpper().Contains(dto.Name.ToUpper()), + dto.Condition.Equals(ActionDto.AND)); + } + + var providers = service.Search(filter); + + + return Ok(mapper.Map>(providers)); + + } + } +} diff --git a/Stock.Api/DTOs/ProviderDTO.cs b/Stock.Api/DTOs/ProviderDTO.cs new file mode 100644 index 0000000..c02b99c --- /dev/null +++ b/Stock.Api/DTOs/ProviderDTO.cs @@ -0,0 +1,22 @@ +using Stock.Model.Entities; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; + +namespace Stock.Api.DTOs +{ + public class ProviderDTO + { + + public string Id { get; set; } + + [Required] + public string Name { get; set; } + + [Required] + public string Email { get; set; } + + [Required] + public string Phone { get; set; } + + } +} diff --git a/Stock.Api/DTOs/ProviderSearchDTO.cs b/Stock.Api/DTOs/ProviderSearchDTO.cs new file mode 100644 index 0000000..c8fe041 --- /dev/null +++ b/Stock.Api/DTOs/ProviderSearchDTO.cs @@ -0,0 +1,9 @@ +namespace Stock.Api.DTOs +{ + public class ProviderSearchDTO + { + public string Name { get; set; } + + public ActionDto Condition { get; set; } + } +} diff --git a/Stock.Api/MapperProfiles/ModelProfile.cs b/Stock.Api/MapperProfiles/ModelProfile.cs index fba627b..d8801aa 100644 --- a/Stock.Api/MapperProfiles/ModelProfile.cs +++ b/Stock.Api/MapperProfiles/ModelProfile.cs @@ -13,7 +13,7 @@ public ModelProfile() .ReverseMap() .ForMember(s => s.Id, opt => opt.Ignore()); - CreateMap() + CreateMap() //.IgnoreAllNonExisting() .ReverseMap() .ForMember(s => s.Id, opt => opt.Ignore()); @@ -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..d187153 100644 --- a/Stock.Api/Startup.cs +++ b/Stock.Api/Startup.cs @@ -12,6 +12,9 @@ using Stock.Repository.LiteDb.Interface; using Stock.Repository.LiteDb.Repository; using Stock.Settings; +using System; +using System.IO; +using System.Reflection; namespace Stock.Api { @@ -30,7 +33,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(); @@ -43,10 +46,16 @@ public void ConfigureServices(IServiceCollection services) services.AddControllers(); services.AddAutoMapper(typeof(Startup).Assembly); - // Register the Swagger generator, defining 1 or more Swagger documents + services.AddSwaggerGen(c => { + // Set Title and version c.SwaggerDoc("v1", new OpenApiInfo { Title = "Stock API", Version = "v1", Description = "Stock API v1" }); + // Set the comments path for the Swagger JSON and UI. + var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; + var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); + // pick comments from classes, including controller summary comments + c.IncludeXmlComments(xmlPath); }); } diff --git a/Stock.Api/Stock.Api.csproj b/Stock.Api/Stock.Api.csproj index 242c399..7a201d8 100644 --- a/Stock.Api/Stock.Api.csproj +++ b/Stock.Api/Stock.Api.csproj @@ -1,7 +1,8 @@ - + net5.0 + True diff --git a/Stock.AppService/Services/ProviderService.cs b/Stock.AppService/Services/ProviderService.cs new file mode 100644 index 0000000..3ad1b97 --- /dev/null +++ b/Stock.AppService/Services/ProviderService.cs @@ -0,0 +1,27 @@ +using Stock.AppService.Base; +using Stock.Model.Entities; +using Stock.Repository.LiteDb.Interface; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Linq.Expressions; + + +namespace Stock.AppService.Services +{ + public class ProviderService : BaseService + { + public ProviderService(IRepository repository) + : base(repository) + { + } + + public IEnumerable Search(Expression> filter) + { + return Repository.List(filter); + } + } + +}