Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Primera versión de la actividad inicial para revisión. #142

Open
wants to merge 1 commit 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
34 changes: 25 additions & 9 deletions Stock.Api.sln
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
128 changes: 128 additions & 0 deletions Stock.Api/Controllers/ProviderController.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Provider endpoint.
/// </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

Y usarla por ejemplo, en bloques try - catch.
Donde pensas que podes incluir (en los metodos creados) o anterior ?

/// <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)
{
var provider = service.Get(id);
TryValidateModel(value);
mapper.Map<ProviderDTO, Provider>(value, provider);
service.Update(provider);
}

/// <summary>
/// Deletes a provider.
/// </summary>
/// <param name="id">Provider id to delete.</param>
[HttpDelete("{id}")]
public ActionResult Delete(string id)
{
var provider = service.Get(id);

if (provider is null)
return NotFound();

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

/// <summary>
/// Search some providers.
/// </summary>
/// <param name="model">Provider filters.</param>
/// <returns></returns>
[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.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);
}
}
}
20 changes: 20 additions & 0 deletions Stock.Api/DTOs/ProviderDTO.cs
Original file line number Diff line number Diff line change
@@ -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<Product> OfferedProducts { get; set; }
}
}
10 changes: 10 additions & 0 deletions Stock.Api/DTOs/ProviderSearchDTO.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
4 changes: 2 additions & 2 deletions Stock.Api/MapperProfiles/ModelProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion Stock.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,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 Down
70 changes: 70 additions & 0 deletions Stock.AppService/Services/ProviderService.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Provider service.
/// </summary>
public class ProviderService : BaseService<Provider>
{
/// <summary>
/// Initializes a new instance of the <see cref="ProviderService"/> class.
/// </summary>
/// <param name="repository">Provider repository.</param>
public ProviderService(IRepository<Provider> repository)
: base(repository)
{
}

/// <summary>
/// Creates a provider.
/// </summary>
/// <param name="entity">Provider information.</param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public new Provider Create(Provider entity)
{
if (NombreUnico(entity.Name))
{
return base.Create(entity);
}

throw new Exception("The name is already in use");
}

/// <summary>
/// Checks if the provider name is unique or not.
/// </summary>
/// <param name="name">Provider name to check.</param>
/// <returns></returns>
private bool NombreUnico(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
return false;
}

return Repository.List(x => x.Name.ToUpper().Equals(name.ToUpper())).Count == 0;
}

/// <summary>
/// Search stores.
/// </summary>
/// <param name="filter"></param>
/// <returns></returns>
public IEnumerable<Provider> Search(Expression<Func<Provider, bool>> filter)
{
return Repository.List(filter);
}


}
}
6 changes: 3 additions & 3 deletions Stock.Test/Stock.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
</ItemGroup>

<ItemGroup>
Expand Down