2020-05-24 22:22:26 -04:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using SharedLibraryCore.Dtos;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-05-24 22:22:26 -04:00
|
|
|
|
using WebfrontCore.Controllers.API.Dtos;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
2020-05-24 22:22:26 -04:00
|
|
|
|
|
|
|
|
|
namespace WebfrontCore.Controllers.API
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// api controller for client operations
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/client")]
|
|
|
|
|
public class ClientController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly IResourceQueryHelper<FindClientRequest, FindClientResult> _clientQueryHelper;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
public ClientController(ILogger<ClientController> logger, IResourceQueryHelper<FindClientRequest, FindClientResult> clientQueryHelper)
|
2020-05-24 22:22:26 -04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_clientQueryHelper = clientQueryHelper;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("find")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
|
|
|
public async Task<IActionResult> FindAsync([FromQuery]FindClientRequest request)
|
|
|
|
|
{
|
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest(new ErrorResponse()
|
|
|
|
|
{
|
|
|
|
|
Messages = ModelState.Values.SelectMany(_value => _value.Errors.Select(_error => _error.ErrorMessage)).ToArray()
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var results = await _clientQueryHelper.QueryResource(request);
|
|
|
|
|
|
|
|
|
|
return Ok(new FindClientResponse
|
|
|
|
|
{
|
|
|
|
|
TotalFoundClients = results.TotalResultCount,
|
|
|
|
|
Clients = results.Results
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_logger.LogWarning(e, "Failed to retrieve clients with query - {@request}", request);
|
2020-05-24 22:22:26 -04:00
|
|
|
|
|
|
|
|
|
return StatusCode(StatusCodes.Status500InternalServerError, new ErrorResponse()
|
|
|
|
|
{
|
|
|
|
|
Messages = new[] { e.Message }
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|