-
Notifications
You must be signed in to change notification settings - Fork 240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Provider #143
base: master
Are you sure you want to change the base?
Add Provider #143
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
using System; | ||
using AutoMapper; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Stock.Api.DTOs; | ||
using Stock.AppService.Services; | ||
using Stock.Model.Entities; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using Stock.Api.Extensions; | ||
|
||
namespace Stock.Api.Controllers | ||
{ | ||
/// <summary> | ||
/// Product type endpoint. | ||
/// </summary> | ||
[Produces("application/json")] | ||
[Route("api/provider")] | ||
[ApiController] | ||
public class ProviderController : ControllerBase | ||
{ | ||
private readonly ProviderService service; | ||
private readonly IMapper mapper; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ProviderController"/> class. | ||
/// </summary> | ||
/// <param name="service">Provider service.</param> | ||
/// <param name="mapper">Mapper configurator.</param> | ||
public ProviderController(ProviderService service, IMapper mapper) | ||
{ | ||
this.service = service ?? throw new ArgumentException(nameof(service)); | ||
this.mapper = mapper ?? throw new ArgumentException(nameof(mapper)); | ||
} | ||
|
||
/// <summary> | ||
/// Get all providers. | ||
/// </summary> | ||
/// <returns>List of <see cref="ProviderDTO"/></returns> | ||
[HttpGet] | ||
public ActionResult<IEnumerable<ProviderDTO>> Get() | ||
{ | ||
return Ok(mapper.Map<IEnumerable<ProviderDTO>>(service.GetAll()).ToList()); | ||
} | ||
|
||
/// <summary> | ||
/// Gets a provider by id. | ||
/// </summary> | ||
/// <param name="id">Provider id to return.</param> | ||
/// <returns>A <see cref="ProviderDTO"/></returns> | ||
[HttpGet("{id}")] | ||
public ActionResult<ProviderDTO> Get(string id) | ||
{ | ||
return Ok(mapper.Map<ProviderDTO>(service.Get(id))); | ||
} | ||
|
||
/// <summary> | ||
/// Add a provider. | ||
/// </summary> | ||
/// <param name="value">Provider information.</param> | ||
[HttpPost] | ||
public Provider Post([FromBody] ProviderDTO value) | ||
{ | ||
TryValidateModel(value); | ||
var provider = service.Create(mapper.Map<Provider>(value)); | ||
return mapper.Map<Provider>(provider); | ||
} | ||
|
||
/// <summary> | ||
/// Updates a provider. | ||
/// </summary> | ||
/// <param name="id">provider id to edit.</param> | ||
/// <param name="value">provider information.</param> | ||
[HttpPut("{id}")] | ||
public void Put(string id, [FromBody] ProviderDTO value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. En el caso de que ocurra un error, se te ocurre alguna forma de manejar la respuesta para el consumidor de la API? LINK |
||
{ | ||
var provider = service.Get(id); | ||
TryValidateModel(value); | ||
mapper.Map<ProviderDTO, Provider>(value, provider); | ||
service.Update(provider); | ||
} | ||
|
||
/// <summary> | ||
/// Deletes a product. | ||
/// </summary> | ||
/// <param name="id">Product id to delete.</param> | ||
[HttpDelete("{id}")] | ||
public ActionResult Delete(string id) | ||
{ | ||
var productType = service.Get(id); | ||
|
||
if (productType is null) | ||
return NotFound(); | ||
|
||
service.Delete(productType); | ||
return Ok(); | ||
} | ||
|
||
/// <summary> | ||
/// Search a provider. | ||
/// </summary> | ||
/// <param name="id">provider id to edit.</param> | ||
/// <param name="value">provider information.</param> | ||
[HttpPost("search")] | ||
public ActionResult Search([FromBody] ProviderSearchDTO model) | ||
{ | ||
Expression<Func<Provider, bool>> filter = x => !string.IsNullOrWhiteSpace(x.Id); | ||
|
||
if (!string.IsNullOrWhiteSpace(model.Name)) | ||
{ | ||
filter = filter.AndOrCustom( | ||
x => x.Name.ToUpper().Contains(model.Name.ToUpper()), | ||
model.Name.Equals(ActionDto.AND)); | ||
} | ||
|
||
if (!string.IsNullOrWhiteSpace(model.Email)) | ||
{ | ||
filter = filter.AndOrCustom( | ||
x => x.Email.ToUpper().Contains(model.Email.ToUpper()), | ||
model.Email.Equals(ActionDto.AND)); | ||
} | ||
|
||
var providers = service.Search(filter); | ||
return Ok(providers); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Stock.Model.Entities; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace Stock.Api.DTOs | ||
{ | ||
public class ProviderDTO | ||
{ | ||
public string Id { get; set; } | ||
|
||
public string Name { get; set; } | ||
|
||
public string Phone { get; set; } | ||
|
||
public string Email { get; set; } | ||
|
||
public List<Product> OfferedProducts { get; set; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. En el DTO estas incluyendo una entity directo, que problemas te podría generar eso? |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Stock.Model.Entities; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace Stock.Api.DTOs | ||
{ | ||
public class ProviderSearchDTO | ||
{ | ||
public string Id { get; set; } | ||
|
||
public string Name { get; set; } | ||
|
||
public string Phone { get; set; } | ||
|
||
public string Email { get; set; } | ||
|
||
public List<Product> OfferedProducts { get; set; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Stock.AppService.Base; | ||
using Stock.Model.Entities; | ||
using Stock.Repository.LiteDb.Interface; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq.Expressions; | ||
|
||
namespace Stock.AppService.Services | ||
{ | ||
/// <summary> | ||
/// Product type service. | ||
/// </summary> | ||
public class ProviderService: BaseService<Provider> | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ProviderService"/> class. | ||
/// </summary> | ||
/// <param name="repository">Product type repository.</param> | ||
public ProviderService(IRepository<Provider> repository) | ||
: base(repository) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Search Providers. | ||
/// </summary> | ||
/// <param name="filter"></param> | ||
/// <returns></returns> | ||
public IEnumerable<Provider> Search(Expression<Func<Provider, bool>> filter) | ||
{ | ||
return Repository.List(filter); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Te falto descomentar el mappeo del Provider-ProviderDTO, por lo que todos los mappeos que se realicen van a fallar. Esta bueno que más allá de los test automáticos, se haga por lo menos una prueba básica de los nuevos endpoints, verificando que hagan lo que dicen hacer. Swagger UI es una interfaz amigable para probarlos.