[issue #139] client lookup and stats api
This commit is contained in:
69
Plugins/Web/StatsWeb/API/StatsController.cs
Normal file
69
Plugins/Web/StatsWeb/API/StatsController.cs
Normal file
@ -0,0 +1,69 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SharedLibraryCore;
|
||||
using SharedLibraryCore.Dtos;
|
||||
using SharedLibraryCore.Interfaces;
|
||||
using Stats.Dtos;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StatsWeb.API
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/stats")]
|
||||
public class StatsController : ControllerBase
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IResourceQueryHelper<StatsInfoRequest, StatsInfoResult> _statsQueryHelper;
|
||||
|
||||
public StatsController(ILogger logger, IResourceQueryHelper<StatsInfoRequest, StatsInfoResult> statsQueryHelper)
|
||||
{
|
||||
_statsQueryHelper = statsQueryHelper;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
[HttpGet("{clientId}")]
|
||||
public async Task<IActionResult> ClientStats(int clientId)
|
||||
{
|
||||
if (clientId < 1 || !ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(new ErrorResponse
|
||||
{
|
||||
Messages = new[] { $"Client Id must be between 1 and {int.MaxValue}" }
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
var request = new StatsInfoRequest()
|
||||
{
|
||||
ClientId = clientId
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var result = await _statsQueryHelper.QueryResource(request);
|
||||
|
||||
if (result.RetrievedResultCount == 0)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return Ok(result.Results);
|
||||
}
|
||||
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.WriteWarning($"Could not get client stats for client id {clientId}");
|
||||
_logger.WriteDebug(e.GetExceptionInfo());
|
||||
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, new ErrorResponse
|
||||
{
|
||||
Messages = new[] { e.Message }
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user