Skip to content
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 #145

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions Stock.Api/Controllers/ProviderController.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// This controller manage all CRUD for a provider
/// </summary>
[Produces("application/json")]
[Route("api/provider")]
[ApiController]

public class ProviderController : ControllerBase
{
private readonly ProviderService service;
private readonly IMapper mapper;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Podes inyectar la interfaz Ilogger
Aqui un breve video, por si es la primera vez que escuchas sobre ILogger

https://www.youtube.com/watch?v=-P-MHeTZBSA&list=PLbnhLdYZXcqGJbb6qtJMalv7fh8nBxMcn&index=39

Y usarla por ejemplo, en bloques try - catch.
En que metodos que creaste pensas que pueden ir esos bloques try - catch?

/// <summary>
/// Main constructor
/// </summary>
/// <param name="providerService">CRUD and search service for a provider</param>
/// <param name="mapper">Mapper configurator</param>
public ProviderController(ProviderService providerService, IMapper mapper)
{
this.service = providerService ?? throw new ArgumentException(nameof(providerService));
this.mapper = mapper ?? throw new ArgumentException(nameof(mapper));


}

/// <summary>
/// Get all provider
/// </summary>
/// <returns>Return a list of provider</returns>
[HttpGet]
public ActionResult<IEnumerable<ProviderDTO>> Get()
{
return (mapper.Map<IEnumerable<ProviderDTO>>(service.GetAll()).ToList());
}

/// <summary>
/// Get provider by provider id
/// </summary>
/// <param name="id">Represent an id of a provider </param>
/// <returns>Return a provider</returns>
[HttpGet("{id}")]

public ActionResult<ProviderDTO> Get(string id)
{
return this.mapper.Map<ProviderDTO>(service.Get(id));
}

/// <summary>
/// Create a new provider
/// </summary>
/// <param name="value">Represent all information need it for a provider</param>
/// <returns>Return a provider and its provider id </returns>
[HttpPost]

public Provider Post([FromBody] ProviderDTO value)
{
TryValidateModel(value);
var provider = service.Create(mapper.Map<Provider>(value));
return mapper.Map<Provider>(provider);

}

/// <summary>
/// Update a provider by provider id
/// </summary>
/// <param name="id">Represent an id of a provider</param>
/// <param name="value">Represent all information need it for a provider</param>
[HttpPut("{id}")]

public void Put(string id, [FromBody] ProviderDTO value)
{
var provider = service.Get(id);
TryValidateModel(value);

mapper.Map<ProviderDTO, Provider>(value, provider);

service.Update(provider);
}

/// <summary>
/// Delete a provider by provider id
/// </summary>
/// <param name="id">Represent an id of a provider</param>
/// <returns></returns>
[HttpDelete("{id}")]

public ActionResult Delete(string id)
{
var provider = service.Get(id);
if (provider == null) { return NotFound(); }

service.Delete(provider);
return Ok();
}

/// <summary>
/// Search a provider by criteria
/// </summary>
/// <param name="dto">Represent a search of provider by name</param>
/// <returns>Return a list of provider by criteria</returns>
[HttpPost("search")]

public ActionResult<IEnumerable<ProviderDTO>> Search([FromBody] ProviderSearchDTO dto)
{
Expression<Func<Provider, bool>> 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<IEnumerable<ProviderDTO>>(providers));

}
}
}
22 changes: 22 additions & 0 deletions Stock.Api/DTOs/ProviderDTO.cs
Original file line number Diff line number Diff line change
@@ -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; }

}
}
9 changes: 9 additions & 0 deletions Stock.Api/DTOs/ProviderSearchDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Stock.Api.DTOs
{
public class ProviderSearchDTO
{
public string Name { get; set; }

public ActionDto Condition { get; set; }
}
}
6 changes: 3 additions & 3 deletions Stock.Api/MapperProfiles/ModelProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public ModelProfile()
.ReverseMap()
.ForMember(s => s.Id, opt => opt.Ignore());

CreateMap<Store, StoreDTO>()
CreateMap<Store, ProviderDTO>()
//.IgnoreAllNonExisting()
.ReverseMap()
.ForMember(s => s.Id, opt => opt.Ignore());
Expand All @@ -25,8 +25,8 @@ public ModelProfile()
// .ForMember(s => s.Id, opt => opt.Ignore())
// .ForMember(s => s.ProductType, opt => opt.Ignore());

// CreateMap<Provider, ProviderDTO>()
// .ReverseMap();
CreateMap<Provider, ProviderDTO>()
.ReverseMap();
}
}

Expand Down
13 changes: 11 additions & 2 deletions Stock.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -30,7 +33,7 @@ public void ConfigureServices(IServiceCollection services)
services.Configure<DomainSettings>(Configuration.GetSection("DomainSettings"));
services.AddTransient<StoreService>();
//services.AddTransient<ProductService>();
//services.AddTransient<ProviderService>();
services.AddTransient<ProviderService>();
services.AddTransient<ProductTypeService>();
services.AddTransient<Repository.LiteDb.Configuration.ConfigurationProvider>();
services.AddTransient<ILiteConfiguration, LiteConfiguration>();
Expand All @@ -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);
});
}

Expand Down
3 changes: 2 additions & 1 deletion Stock.Api/Stock.Api.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down
27 changes: 27 additions & 0 deletions Stock.AppService/Services/ProviderService.cs
Original file line number Diff line number Diff line change
@@ -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<Provider>
{
public ProviderService(IRepository<Provider> repository)
: base(repository)
{
}

public IEnumerable<Provider> Search(Expression<Func<Provider, bool>> filter)
{
return Repository.List(filter);
}
}

}