-
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
Actividad inicial finalizada #152
base: master
Are you sure you want to change the base?
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,35 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
// Use IntelliSense to find out which attributes exist for C# debugging | ||
// Use hover for the description of the existing attributes | ||
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md | ||
"name": ".NET Core Launch (web)", | ||
"type": "coreclr", | ||
"request": "launch", | ||
"preLaunchTask": "build", | ||
// If you have changed target frameworks, make sure to update the program path. | ||
"program": "${workspaceFolder}/Stock.Api/bin/Debug/net5.0/Stock.Api.dll", | ||
"args": [], | ||
"cwd": "${workspaceFolder}/Stock.Api", | ||
"stopAtEntry": false, | ||
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser | ||
"serverReadyAction": { | ||
"action": "openExternally", | ||
"pattern": "\\bNow listening on:\\s+(https?://\\S+)" | ||
}, | ||
"env": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
}, | ||
"sourceFileMap": { | ||
"/Views": "${workspaceFolder}/Views" | ||
} | ||
}, | ||
{ | ||
"name": ".NET Core Attach", | ||
"type": "coreclr", | ||
"request": "attach" | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"label": "build", | ||
"command": "dotnet", | ||
"type": "process", | ||
"args": [ | ||
"build", | ||
"${workspaceFolder}/Stock.Api/Stock.Api.csproj", | ||
"/property:GenerateFullPaths=true", | ||
"/consoleloggerparameters:NoSummary" | ||
], | ||
"problemMatcher": "$msCompile" | ||
}, | ||
{ | ||
"label": "publish", | ||
"command": "dotnet", | ||
"type": "process", | ||
"args": [ | ||
"publish", | ||
"${workspaceFolder}/Stock.Api/Stock.Api.csproj", | ||
"/property:GenerateFullPaths=true", | ||
"/consoleloggerparameters:NoSummary" | ||
], | ||
"problemMatcher": "$msCompile" | ||
}, | ||
{ | ||
"label": "watch", | ||
"command": "dotnet", | ||
"type": "process", | ||
"args": [ | ||
"watch", | ||
"run", | ||
"${workspaceFolder}/Stock.Api/Stock.Api.csproj", | ||
"/property:GenerateFullPaths=true", | ||
"/consoleloggerparameters:NoSummary" | ||
], | ||
"problemMatcher": "$msCompile" | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
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 | ||
{ | ||
/// <summary> | ||
/// Provider endpoint. | ||
/// </summary> | ||
[Produces("application/json")] | ||
[Route("api/provider")] | ||
[ApiController] | ||
public class ProviderController : ControllerBase | ||
{ | ||
private readonly ProviderService service; | ||
private readonly ILogger<ProviderController> logger; | ||
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> | ||
/// <param name="logger">Logger service.</param> | ||
public ProviderController(ProviderService service, IMapper mapper, ILogger<ProviderController> 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)); | ||
} | ||
|
||
/// <summary> | ||
/// Adds a provider. | ||
/// </summary> | ||
/// <param name="value">Provider info.</param> | ||
/// <returns></returns> | ||
[HttpPost] | ||
public ActionResult Post([FromBody] ProviderDTO value) | ||
{ | ||
TryValidateModel(value); | ||
|
||
try | ||
{ | ||
var provider = mapper.Map<Provider>(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" }); | ||
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. Aqui, al retornar un "Ok", estas retornando un HTTP status 200 al cliente. Que no seria correcto... Quizas este articulo te de una pista sobre que retornar ahi. |
||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets all providers. | ||
/// </summary> | ||
/// <returns></returns> | ||
[HttpGet] | ||
public ActionResult<IEnumerable<ProviderDTO>> Get() | ||
{ | ||
try | ||
{ | ||
var result = service.GetAll(); | ||
return mapper.Map<IEnumerable<ProviderDTO>>(result).ToList(); | ||
} | ||
catch (Exception) | ||
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. Se podria loggear la Exception |
||
{ | ||
return StatusCode(500); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets a provider by id. | ||
/// </summary> | ||
/// <param name="id">Provider id.</param> | ||
/// <returns></returns> | ||
[HttpGet("{id}")] | ||
public ActionResult<ProviderDTO> Get(string id) | ||
{ | ||
try | ||
{ | ||
var result = service.Get(id); | ||
return mapper.Map<ProviderDTO>(result); | ||
} | ||
catch (Exception) | ||
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. Se podria loggear la Exception |
||
{ | ||
return StatusCode(500); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Updates a provider. | ||
/// </summary> | ||
/// <param name="id">Provider id.</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(new { Success = true, Message = "", data = id }); | ||
} | ||
|
||
/// <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.Address)) | ||
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. No es buena practica dejar codigo comentado |
||
{ | ||
filter = filter.AndOrCustom( | ||
x => x.Address.ToUpper().Contains(model.Address.ToUpper()), | ||
model.Condition.Equals(ActionDto.AND)); | ||
}*/ | ||
|
||
var providers = service.Search(filter); | ||
return Ok(providers); | ||
} | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
|
||
//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. No es buena practica dejar codigo comentado |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace Stock.Api.DTOs | ||
{ | ||
public class ProviderSearchDTO | ||
{ | ||
public string Name { get; set; } | ||
|
||
public string Phone { get; set; } | ||
|
||
public string Email { get; set; } | ||
|
||
public ActionDto Condition { 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. No es buena practica dejar codigo comentado |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
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 | ||
{ | ||
/// <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 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.
Se podria usar otro metodo del logger que no sea LogCritical.
Segun tu punto de vista, cual podria ser y porque ?
https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.ilogger?view=dotnet-plat-ext-6.0