[issue #139] client lookup and stats api
This commit is contained in:
@ -24,5 +24,8 @@ namespace SharedLibraryCore.Database.Models
|
||||
|
||||
[NotMapped]
|
||||
public const int MAX_NAME_LENGTH = 24;
|
||||
|
||||
[NotMapped]
|
||||
public const int MIN_NAME_LENGTH = 3;
|
||||
}
|
||||
}
|
||||
|
15
SharedLibraryCore/Dtos/ErrorResponse.cs
Normal file
15
SharedLibraryCore/Dtos/ErrorResponse.cs
Normal file
@ -0,0 +1,15 @@
|
||||
namespace SharedLibraryCore.Dtos
|
||||
{
|
||||
public class ErrorResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// todo: type of error
|
||||
/// </summary>
|
||||
public string Type { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// relevant error messages
|
||||
/// </summary>
|
||||
public string[] Messages { get; set; }
|
||||
}
|
||||
}
|
17
SharedLibraryCore/Dtos/FindClientRequest.cs
Normal file
17
SharedLibraryCore/Dtos/FindClientRequest.cs
Normal file
@ -0,0 +1,17 @@
|
||||
namespace SharedLibraryCore.Dtos
|
||||
{
|
||||
public class FindClientRequest : PaginationInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// name of client
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// network id of client
|
||||
/// </summary>
|
||||
public string Xuid { get; set; }
|
||||
|
||||
public string ToDebugString() => $"[Name={Name}, Xuid={Xuid}]";
|
||||
}
|
||||
}
|
20
SharedLibraryCore/Dtos/FindClientResult.cs
Normal file
20
SharedLibraryCore/Dtos/FindClientResult.cs
Normal file
@ -0,0 +1,20 @@
|
||||
namespace SharedLibraryCore.Dtos
|
||||
{
|
||||
public class FindClientResult
|
||||
{
|
||||
/// <summary>
|
||||
/// client identifier
|
||||
/// </summary>
|
||||
public int ClientId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// networkid of client
|
||||
/// </summary>
|
||||
public string Xuid { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// name of client
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
@ -13,7 +13,7 @@
|
||||
/// <summary>
|
||||
/// how many itesm to take
|
||||
/// </summary>
|
||||
public int Count { get; set; }
|
||||
public int Count { get; set; } = 100;
|
||||
|
||||
/// <summary>
|
||||
/// filter query
|
||||
|
@ -2,6 +2,8 @@
|
||||
using SharedLibraryCore.Database;
|
||||
using SharedLibraryCore.Database.Models;
|
||||
using SharedLibraryCore.Dtos;
|
||||
using SharedLibraryCore.Helpers;
|
||||
using SharedLibraryCore.Interfaces;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -10,8 +12,15 @@ using static SharedLibraryCore.Database.Models.EFClient;
|
||||
|
||||
namespace SharedLibraryCore.Services
|
||||
{
|
||||
public class ClientService : Interfaces.IEntityService<EFClient>
|
||||
public class ClientService : IEntityService<EFClient>, IResourceQueryHelper<FindClientRequest, FindClientResult>
|
||||
{
|
||||
private readonly IDatabaseContextFactory _contextFactory;
|
||||
|
||||
public ClientService(IDatabaseContextFactory databaseContextFactory)
|
||||
{
|
||||
_contextFactory = databaseContextFactory;
|
||||
}
|
||||
|
||||
public async Task<EFClient> Create(EFClient entity)
|
||||
{
|
||||
using (var context = new DatabaseContext())
|
||||
@ -105,7 +114,7 @@ namespace SharedLibraryCore.Services
|
||||
|
||||
private async Task UpdateAlias(string originalName, int? ip, EFClient entity, DatabaseContext context)
|
||||
{
|
||||
string name = originalName.CapClientName(EFAlias.MAX_NAME_LENGTH);
|
||||
string name = originalName.CapClientName(EFAlias.MAX_NAME_LENGTH);
|
||||
|
||||
// entity is the tracked db context item
|
||||
// get all aliases by IP address and LinkId
|
||||
@ -724,5 +733,56 @@ namespace SharedLibraryCore.Services
|
||||
await ctx.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// find clients matching the given query
|
||||
/// </summary>
|
||||
/// <param name="query">query filters</param>
|
||||
/// <returns></returns>
|
||||
public async Task<ResourceQueryHelperResult<FindClientResult>> QueryResource(FindClientRequest query)
|
||||
{
|
||||
var result = new ResourceQueryHelperResult<FindClientResult>();
|
||||
using var context = _contextFactory.CreateContext(enableTracking: false);
|
||||
|
||||
IQueryable<EFClient> iqClients = null;
|
||||
|
||||
if (!string.IsNullOrEmpty(query.Xuid))
|
||||
{
|
||||
long networkId = query.Xuid.ConvertGuidToLong(System.Globalization.NumberStyles.HexNumber);
|
||||
iqClients = context.Clients.Where(_client => _client.NetworkId == networkId);
|
||||
}
|
||||
|
||||
else if (!string.IsNullOrEmpty(query.Name))
|
||||
{
|
||||
iqClients = context.Clients.Where(_client => EF.Functions.Like(_client.CurrentAlias.Name.ToLower(), $"%{query.Name.ToLower()}%"));
|
||||
}
|
||||
|
||||
if (query.Direction == SortDirection.Ascending)
|
||||
{
|
||||
iqClients = iqClients.OrderBy(_client => _client.LastConnection);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
iqClients = iqClients.OrderByDescending(_client => _client.LastConnection);
|
||||
}
|
||||
|
||||
var queryResults = await iqClients
|
||||
.Select(_client => new FindClientResult
|
||||
{
|
||||
ClientId = _client.ClientId,
|
||||
Xuid = _client.NetworkId.ToString("X"),
|
||||
Name = _client.CurrentAlias.Name
|
||||
})
|
||||
.Skip(query.Offset)
|
||||
.Take(query.Count)
|
||||
.ToListAsync();
|
||||
|
||||
result.TotalResultCount = await iqClients.CountAsync();
|
||||
result.Results = queryResults;
|
||||
result.RetrievedResultCount = queryResults.Count;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,12 +6,12 @@
|
||||
<ApplicationIcon />
|
||||
<StartupObject />
|
||||
<PackageId>RaidMax.IW4MAdmin.SharedLibraryCore</PackageId>
|
||||
<Version>2.4.0</Version>
|
||||
<Version>2.4.2</Version>
|
||||
<Authors>RaidMax</Authors>
|
||||
<Company>Forever None</Company>
|
||||
<Configurations>Debug;Release;Prerelease</Configurations>
|
||||
<PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest>
|
||||
<LangVersion>7.1</LangVersion>
|
||||
<LangVersion>8.0</LangVersion>
|
||||
<PackageTags>IW4MAdmin</PackageTags>
|
||||
<RepositoryUrl>https://github.com/RaidMax/IW4M-Admin/</RepositoryUrl>
|
||||
<PackageProjectUrl>https://www.raidmax.org/IW4MAdmin/</PackageProjectUrl>
|
||||
@ -20,8 +20,6 @@
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
<Description>Shared Library for IW4MAdmin</Description>
|
||||
<AssemblyVersion>2.4.0.0</AssemblyVersion>
|
||||
<FileVersion>2.4.0.0</FileVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Prerelease|AnyCPU'">
|
||||
@ -29,22 +27,6 @@
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="Migrations\20181126232438_AddEndpointToEFServer.cs" />
|
||||
<Compile Remove="Migrations\20181126233300_AddEndpointToEFServer.cs" />
|
||||
<Compile Remove="Migrations\20181127143920_AddEndpointToEFServerUpdateServerIdType.cs" />
|
||||
<Compile Remove="Migrations\20190222234606_AddIndexToEFMeta-KeyAndClientId.cs" />
|
||||
<Compile Remove="Migrations\20190223012312_SetMaxLengthForMetaKey.cs" />
|
||||
<Compile Remove="Migrations\20190907222702_AddMomentViewAnglesToAcSnapshot.cs" />
|
||||
<Compile Remove="Migrations\20190907222702_AddMomentViewAnglesToAcSnapshot.Designer.cs" />
|
||||
<Compile Remove="Migrations\20191120170503_AddDateAuditColumnsToSharedEntity.cs" />
|
||||
<Compile Remove="Migrations\20191120170503_AddDateAuditColumnsToSharedEntity.Designer.cs" />
|
||||
<Compile Remove="Migrations\20191120204934_AddDateAuditColumnsToSharedEntity.cs" />
|
||||
<Compile Remove="Migrations\20191120204934_AddDateAuditColumnsToSharedEntity.Designer.cs" />
|
||||
<Compile Remove="Migrations\20191120205633_AddDateAuditColumnsToSharedEntity.cs" />
|
||||
<Compile Remove="Migrations\20191120205633_AddDateAuditColumnsToSharedEntity.Designer.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FluentValidation" Version="8.6.2" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="2.2.0" />
|
||||
|
Reference in New Issue
Block a user