[issue #139] client lookup and stats api
This commit is contained in:
66
WebfrontCore/Controllers/API/ClientController.cs
Normal file
66
WebfrontCore/Controllers/API/ClientController.cs
Normal file
@ -0,0 +1,66 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SharedLibraryCore;
|
||||
using SharedLibraryCore.Dtos;
|
||||
using SharedLibraryCore.Interfaces;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using WebfrontCore.Controllers.API.Dtos;
|
||||
|
||||
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;
|
||||
|
||||
public ClientController(ILogger logger, IResourceQueryHelper<FindClientRequest, FindClientResult> clientQueryHelper)
|
||||
{
|
||||
_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)
|
||||
{
|
||||
_logger.WriteWarning($"Failed to retrieve clients with query - {request.ToDebugString()}");
|
||||
_logger.WriteDebug(e.GetExceptionInfo());
|
||||
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, new ErrorResponse()
|
||||
{
|
||||
Messages = new[] { e.Message }
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
18
WebfrontCore/Controllers/API/Dtos/FindClientResponse.cs
Normal file
18
WebfrontCore/Controllers/API/Dtos/FindClientResponse.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using SharedLibraryCore.Dtos;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace WebfrontCore.Controllers.API.Dtos
|
||||
{
|
||||
public class FindClientResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// total number of client found matching the query
|
||||
/// </summary>
|
||||
public long TotalFoundClients { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// collection of doun clients
|
||||
/// </summary>
|
||||
public IEnumerable<FindClientResult> Clients { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
using FluentValidation;
|
||||
using SharedLibraryCore.Database.Models;
|
||||
using SharedLibraryCore.Dtos;
|
||||
|
||||
namespace WebfrontCore.Controllers.API.Validation
|
||||
{
|
||||
/// <summary>
|
||||
/// validator for FindClientRequest
|
||||
/// </summary>
|
||||
public class FindClientRequestValidator : AbstractValidator<FindClientRequest>
|
||||
{
|
||||
public FindClientRequestValidator()
|
||||
{
|
||||
RuleFor(_request => _request.Name)
|
||||
.NotEmpty()
|
||||
.When(_request => string.IsNullOrEmpty(_request.Xuid));
|
||||
|
||||
RuleFor(_request => _request.Name)
|
||||
.MinimumLength(EFAlias.MIN_NAME_LENGTH)
|
||||
.MaximumLength(EFAlias.MAX_NAME_LENGTH);
|
||||
|
||||
RuleFor(_request => _request.Xuid)
|
||||
.NotEmpty()
|
||||
.When(_request => string.IsNullOrEmpty(_request.Name));
|
||||
|
||||
RuleFor(_request => _request.Count)
|
||||
.InclusiveBetween(1, 100);
|
||||
|
||||
RuleFor(_request => _request.Offset)
|
||||
.GreaterThanOrEqualTo(0);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using FluentValidation;
|
||||
using FluentValidation.AspNetCore;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
@ -9,8 +11,12 @@ using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SharedLibraryCore;
|
||||
using SharedLibraryCore.Database;
|
||||
using SharedLibraryCore.Dtos;
|
||||
using SharedLibraryCore.Helpers;
|
||||
using SharedLibraryCore.Interfaces;
|
||||
using SharedLibraryCore.Services;
|
||||
using Stats.Dtos;
|
||||
using Stats.Helpers;
|
||||
using StatsWeb;
|
||||
using StatsWeb.Dtos;
|
||||
using System.Collections.Generic;
|
||||
@ -19,6 +25,8 @@ using System.Linq;
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using WebfrontCore.Controllers.API.Dtos;
|
||||
using WebfrontCore.Controllers.API.Validation;
|
||||
using WebfrontCore.Middleware;
|
||||
|
||||
namespace WebfrontCore
|
||||
@ -55,6 +63,7 @@ namespace WebfrontCore
|
||||
|
||||
// Add framework services.
|
||||
var mvcBuilder = services.AddMvc(_options => _options.SuppressAsyncSuffixInActionNames = false)
|
||||
.AddFluentValidation()
|
||||
.ConfigureApplicationPartManager(_partManager =>
|
||||
{
|
||||
foreach (var assembly in pluginAssemblies())
|
||||
@ -105,6 +114,9 @@ namespace WebfrontCore
|
||||
|
||||
services.AddSingleton(Program.Manager);
|
||||
services.AddSingleton<IResourceQueryHelper<ChatSearchQuery, ChatSearchResult>, ChatResourceQueryHelper>();
|
||||
services.AddTransient<IValidator<FindClientRequest>, FindClientRequestValidator>();
|
||||
services.AddSingleton<IResourceQueryHelper<FindClientRequest, FindClientResult>, ClientService>();
|
||||
services.AddSingleton<IResourceQueryHelper<StatsInfoRequest, StatsInfoResult>, StatsResourceQueryHelper>();
|
||||
|
||||
// todo: this needs to be handled more gracefully
|
||||
services.AddSingleton(Program.ApplicationServiceProvider.GetService<IConfigurationHandlerFactory>());
|
||||
|
@ -66,6 +66,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="BuildBundlerMinifier" Version="3.2.435" />
|
||||
<PackageReference Include="BuildWebCompiler" Version="1.12.405" />
|
||||
<PackageReference Include="FluentValidation.AspNetCore" Version="8.6.2" />
|
||||
<PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="2.0.96" />
|
||||
</ItemGroup>
|
||||
|
||||
|
Reference in New Issue
Block a user