huge commit for advanced stats feature.
broke data out into its own library. may be breaking changes with existing plugins
This commit is contained in:
69
WebfrontCore/Controllers/API/StatsController.cs
Normal file
69
WebfrontCore/Controllers/API/StatsController.cs
Normal file
@ -0,0 +1,69 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SharedLibraryCore.Dtos;
|
||||
using SharedLibraryCore.Interfaces;
|
||||
using Stats.Dtos;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
||||
|
||||
namespace StatsWeb.API
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/stats")]
|
||||
public class StatsController : ControllerBase
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IResourceQueryHelper<StatsInfoRequest, StatsInfoResult> _statsQueryHelper;
|
||||
|
||||
public StatsController(ILogger<StatsController> 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.LogWarning(e, "Could not get client stats for client id {clientId}", clientId);
|
||||
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, new ErrorResponse
|
||||
{
|
||||
Messages = new[] { e.Message }
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
using FluentValidation;
|
||||
using SharedLibraryCore.Database.Models;
|
||||
using Data.Models;
|
||||
using FluentValidation;
|
||||
using SharedLibraryCore.Dtos;
|
||||
|
||||
namespace WebfrontCore.Controllers.API.Validation
|
||||
|
Reference in New Issue
Block a user