Skip to content
This repository has been archived by the owner on Apr 8, 2023. It is now read-only.

Fix player headshot preview. #197

Merged
merged 3 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Client/DynamoLeagueBlazor.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
<ItemGroup>
<PackageReference Include="Blazored.FluentValidation" Version="2.1.0" />
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.3" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="7.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.4" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="7.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="7.0.3" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="7.0.4" />
<PackageReference Include="MudBlazor" Version="6.1.9" />
<PackageReference Include="Serilog.Sinks.BrowserConsole" Version="1.0.0" />
<PackageReference Include="Serilog.Sinks.BrowserHttp" Version="1.0.0-dev-00021" />
Expand Down
45 changes: 26 additions & 19 deletions src/Server/Features/Admin/Shared/PlayerHeadshotService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ namespace DynamoLeagueBlazor.Server.Features.Admin.Shared;
public class PlayerHeadshotService : IPlayerHeadshotService
{
private readonly HttpClient _httpClient;
private ILogger<PlayerHeadshotService> _logger;
private static PlayerDataResult? _playerDataResult;
private readonly ILogger<PlayerHeadshotService> _logger;

public PlayerHeadshotService(HttpClient httpClient, ILogger<PlayerHeadshotService> logger)
{
Expand All @@ -17,23 +16,29 @@ public PlayerHeadshotService(HttpClient httpClient, ILogger<PlayerHeadshotServic

public async Task<string?> FindPlayerHeadshotUrlAsync(string fullName, string position, CancellationToken cancellationToken)
{
if (_playerDataResult == null)
try
{
_playerDataResult = await _httpClient.GetFromJsonAsync<PlayerDataResult>(PlayerHeadshotRouteFactory.GetPlayersUri, cancellationToken);
}
var playerDataResult = await _httpClient.GetFromJsonAsync<PlayerDataResult>(PlayerHeadshotRouteFactory.GetPlayersUri, cancellationToken);

var matchingPlayers = playerDataResult!.Data.Players
.Where(p => p.FullName == fullName && p.Position == position)
.ToList();

var matchingPlayers = _playerDataResult!.Data.Players
.Where(p => p.FullName == fullName && p.Position == position)
.ToList();
if (matchingPlayers.Count != 1)
{
return null;
}

if (matchingPlayers.Count != 1)
var metricResult = await _httpClient.GetFromJsonAsync<PlayerMetricDataResult>(PlayerHeadshotRouteFactory.GetPlayerUri(matchingPlayers.Single().PlayerId!), cancellationToken: cancellationToken);

return metricResult!.Data.Player.Core?.Avatar;
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to acquire player preview.");
return null;
}

var metricResult = await _httpClient.GetFromJsonAsync<PlayerMetricDataResult>(PlayerHeadshotRouteFactory.GetPlayerUri(matchingPlayers.Single().PlayerId), cancellationToken: cancellationToken);

return metricResult!.Data.Player.Core.Avatar;
}
}

Expand All @@ -54,12 +59,13 @@ public class PlayerListResult

public class PlayerResult
{
// Note: This provider has bad data whhere every property is null, thus these needing to be nullable.
[JsonPropertyName("Player_ID")]
public required string PlayerId { get; set; }
public string? PlayerId { get; set; }
[JsonPropertyName("Full Name")]
public required string FullName { get; set; }
public required string Team { get; set; }
public required string Position { get; set; }
public string? FullName { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? Position { get; set; }
}
}
}
Expand All @@ -74,14 +80,15 @@ public class PlayerMetricData

public class PlayerData
{
// Note: This provider has bad data whhere every property is null, thus these needing to be nullable.
[JsonPropertyName("Player_ID")]
public required string PlayerId { get; set; }
public string? PlayerId { get; set; }

public required Player Core { get; set; }
public Player? Core { get; set; }

public class Player
{
public required string Avatar { get; set; }
public string? Avatar { get; set; }
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@
builder.Services.AddMediatR(config => config.RegisterServicesFromAssemblyContaining<Program>());
builder.Services.AddValidatorsFromAssemblyContaining<AddFineRequestValidator>();
builder.Services.AddFluentValidationAutoValidation();

builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.PropertyNameCaseInsensitive = true;
});
builder.Services.AddScoped<IBidValidator, BidValidator>();
builder.Services.AddScoped<IPlayerHeadshotService, PlayerHeadshotService>();
builder.Services.AddScoped<IMatchPlayerValidator, MatchPlayerValidator>();
Expand Down
45 changes: 39 additions & 6 deletions src/Tests/Features/Admin/Shared/PlayerHeadshotServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public async Task WhenAPlayerIsMatchedOnNameAndPosition_ThenThePlayerHeadshotIsU

var stubPlayerMetricDataResult = new AutoFaker<PlayerMetricDataResult>().Generate();
stubPlayerMetricDataResult.Data.Player.PlayerId = matchingPlayer.PlayerId;
mockHttpHandler.When(HttpMethod.Get, PlayerHeadshotRouteFactory.GetPlayerUri(matchingPlayer.PlayerId))
mockHttpHandler.When(HttpMethod.Get, PlayerHeadshotRouteFactory.GetPlayerUri(matchingPlayer.PlayerId!))
.RespondsWithJson(stubPlayerMetricDataResult);

var sut = new PlayerHeadshotService(httpClient, Mock.Of<ILogger<PlayerHeadshotService>>());

var headshot = await sut.FindPlayerHeadshotUrlAsync(matchingPlayer.FullName, matchingPlayer.Position, CancellationToken.None);
var headshot = await sut.FindPlayerHeadshotUrlAsync(matchingPlayer.FullName!, matchingPlayer.Position!, CancellationToken.None);

headshot.Should().NotBeNullOrEmpty();
}
Expand All @@ -43,12 +43,12 @@ public async Task WhenAPlayerIsNotMatchedOnName_ThenReturnsNothing()

var stubPlayerMetricDataResult = new AutoFaker<PlayerMetricDataResult>().Generate();
stubPlayerMetricDataResult.Data.Player.PlayerId = matchingPlayer.PlayerId;
mockHttpHandler.When(HttpMethod.Get, PlayerHeadshotRouteFactory.GetPlayerUri(matchingPlayer.PlayerId))
mockHttpHandler.When(HttpMethod.Get, PlayerHeadshotRouteFactory.GetPlayerUri(matchingPlayer.PlayerId!))
.RespondsWithJson(stubPlayerMetricDataResult);

var sut = new PlayerHeadshotService(httpClient, Mock.Of<ILogger<PlayerHeadshotService>>());

var headshot = await sut.FindPlayerHeadshotUrlAsync(RandomString, matchingPlayer.Position, CancellationToken.None);
var headshot = await sut.FindPlayerHeadshotUrlAsync(RandomString, matchingPlayer.Position!, CancellationToken.None);

headshot.Should().BeNull();
}
Expand All @@ -67,12 +67,45 @@ public async Task WhenAPlayerIsNotMatchedOnPosition_ThenReturnsNothing()

var stubPlayerMetricDataResult = new AutoFaker<PlayerMetricDataResult>().Generate();
stubPlayerMetricDataResult.Data.Player.PlayerId = matchingPlayer.PlayerId;
mockHttpHandler.When(HttpMethod.Get, PlayerHeadshotRouteFactory.GetPlayerUri(matchingPlayer.PlayerId))
mockHttpHandler.When(HttpMethod.Get, PlayerHeadshotRouteFactory.GetPlayerUri(matchingPlayer.PlayerId!))
.RespondsWithJson(stubPlayerMetricDataResult);

var sut = new PlayerHeadshotService(httpClient, Mock.Of<ILogger<PlayerHeadshotService>>());

var headshot = await sut.FindPlayerHeadshotUrlAsync(matchingPlayer.FullName, RandomString, CancellationToken.None);
var headshot = await sut.FindPlayerHeadshotUrlAsync(matchingPlayer.FullName!, RandomString, CancellationToken.None);

headshot.Should().BeNull();
}

[Fact]
public async Task WhenTheExternalServiceIsNotRespondingCorrectly_ThenReturnsNothing()
{
var mockHttpHandler = new MockHttpHandler();
var httpClient = new HttpClient(mockHttpHandler);

var stubPlayerDataResult = new AutoFaker<PlayerDataResult>().Generate();
mockHttpHandler.When(HttpMethod.Get, PlayerHeadshotRouteFactory.GetPlayersUri)
.Respond(with => with.StatusCode(HttpStatusCode.InternalServerError));

var sut = new PlayerHeadshotService(httpClient, Mock.Of<ILogger<PlayerHeadshotService>>());

var headshot = await sut.FindPlayerHeadshotUrlAsync(It.IsAny<string>(), RandomString, CancellationToken.None);

headshot.Should().BeNull();
}

[Fact]
public async Task WhenTheExternalServiceHasModifiedTheirResponse_ThenReturnsNothing()
{
var mockHttpHandler = new MockHttpHandler();
var httpClient = new HttpClient(mockHttpHandler);

mockHttpHandler.When(HttpMethod.Get, PlayerHeadshotRouteFactory.GetPlayersUri)
.RespondsWithJson(RandomString);

var sut = new PlayerHeadshotService(httpClient, Mock.Of<ILogger<PlayerHeadshotService>>());

var headshot = await sut.FindPlayerHeadshotUrlAsync(It.IsAny<string>(), RandomString, CancellationToken.None);

headshot.Should().BeNull();
}
Expand Down