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

94 #95

Merged
merged 12 commits into from
Jun 28, 2022
Merged

94 #95

Show file tree
Hide file tree
Changes from 7 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
2 changes: 1 addition & 1 deletion src/Client/Features/OfferMatching/List.razor
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</HeaderContent>
<RowTemplate>
<MudTd DataLabel="Actions">
<MudIconButton Icon="@Icons.Filled.Handshake" Color="Color.Primary" Variant="Variant.Outlined" Size="Size.Small" Class="ma-2" />
<MudIconButton Icon="@Icons.Filled.Handshake" Color="Color.Primary" Variant="Variant.Outlined" Size="Size.Small" Class="ma-2" OnClick="(e) => MatchPlayerAsync(context.Id,context.Offer)"/>
morkusporkus marked this conversation as resolved.
Show resolved Hide resolved
</MudTd>
<MudTd DataLabel="Player Name">
<NameWithImage Name="@context.Name" ImageUrl="@context.HeadShotUrl" />
Expand Down
29 changes: 25 additions & 4 deletions src/Client/Features/OfferMatching/List.razor.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
using System.Net.Http.Json;
using DynamoLeagueBlazor.Shared.Features.OfferMatching;
using DynamoLeagueBlazor.Shared.Features.OfferMatching;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
using MudBlazor;
using System.Net.Http.Json;

namespace DynamoLeagueBlazor.Client.Features.OfferMatching;

public sealed partial class List : IDisposable
{
[Inject] private HttpClient HttpClient { get; set; } = null!;

[Inject] private ISnackbar SnackBar { get; set; } = null!;
private const string _title = "Offer Matching";
private bool _loading;
private MatchPlayerRequest _player;
private readonly CancellationTokenSource _cts = new();
private OfferMatchingListResult _result = new();

Expand All @@ -19,7 +21,7 @@ protected override async Task OnInitializedAsync()
try
{
_loading = true;
_result = await HttpClient.GetFromJsonAsync<OfferMatchingListResult>(OfferMatchingListRouteFactory.Uri, _cts.Token) ?? new();
await LoadDataAsync();
}
catch (AccessTokenNotAvailableException exception)
{
Expand All @@ -30,7 +32,26 @@ protected override async Task OnInitializedAsync()
_loading = false;
}
}
private async Task LoadDataAsync()
{
_result = await HttpClient.GetFromJsonAsync<OfferMatchingListResult>(OfferMatchingListRouteFactory.Uri, _cts.Token) ?? new();
}

private async void MatchPlayerAsync(int playerId, int amount)
{
_player = new MatchPlayerRequest() { PlayerId = playerId, Amount = amount };
morkusporkus marked this conversation as resolved.
Show resolved Hide resolved
var response = await HttpClient.PostAsJsonAsync(OfferMatchingListRouteFactory.Uri, _player);

if (response.IsSuccessStatusCode)
{
SnackBar.Add("Successfully retained player.", Severity.Success);
}
else
{
SnackBar.Add("Something went wrong...", Severity.Error);
}
await LoadDataAsync();
}
public void Dispose()
{
_cts.Cancel();
Expand Down
44 changes: 41 additions & 3 deletions src/Server/Features/OfferMatching/List.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ namespace DynamoLeagueBlazor.Server.Features.OfferMatching;
[Route(OfferMatchingListRouteFactory.Uri)]
public class ListController : ControllerBase
{
private readonly IMapper _mapper;
private readonly IMediator _mediator;

public ListController(IMediator mediator)
public ListController(IMapper mapper, IMediator mediator)
{
_mapper = mapper;
_mediator = mediator;
}

Expand All @@ -26,6 +28,12 @@ public async Task<OfferMatchingListResult> GetAsync(CancellationToken cancellati
{
return await _mediator.Send(new ListQuery(), cancellationToken);
}
[HttpPost]
public async Task<int> PostAsync([FromBody] MatchPlayerRequest request, CancellationToken cancellationToken)
{
var query = _mapper.Map<MatchPlayerCommand>(request);
return await _mediator.Send(query, cancellationToken);
}
}

public record ListQuery : IRequest<OfferMatchingListResult> { }
Expand Down Expand Up @@ -62,17 +70,47 @@ public async Task<OfferMatchingListResult> Handle(ListQuery request, Cancellatio
};
}
}
public record MatchPlayerCommand(int PlayerId, int Amount) : IRequest<int> { }

public class MatchPlayerHandler : IRequestHandler<MatchPlayerCommand, int>
{
private readonly ApplicationDbContext _dbContext;

public MatchPlayerHandler(ApplicationDbContext dbContext)
{
_dbContext = dbContext;
}

public async Task<int> Handle(MatchPlayerCommand request, CancellationToken cancellationToken)
{
var player = (await _dbContext.Players
.AsTracking()
.SingleAsync(p => p.Id == request.PlayerId, cancellationToken));
player.ContractValue = request.Amount;
player.SetToUnsigned();
await _dbContext.SaveChangesAsync(cancellationToken);
return player.Id;
morkusporkus marked this conversation as resolved.
Show resolved Hide resolved
}

}
public class ListMappingProfile : Profile
{
private const int _minimumBid = 1;
public ListMappingProfile()
{
CreateMap<Player, OfferMatchingListResult.OfferMatchingItem>()
.ForMember(d => d.OfferingTeam, mo => mo.MapFrom(s => s.Team != null ? s.Team.Name : string.Empty))
.ForMember(d => d.Offer, mo => mo.MapFrom(s =>
s.Bids.Any()
? s.Bids.GetHighestBidder().Amount.ToString("C0")
: string.Empty)
? s.Bids.GetHighestBidder().Amount : _minimumBid)
);
}
}

public class MatchPlayerMappingProfile : Profile
{
public MatchPlayerMappingProfile()
{
CreateMap<MatchPlayerRequest, MatchPlayerCommand>();
}
}
2 changes: 1 addition & 1 deletion src/Shared/Features/OfferMatching/List.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class OfferMatchingItem
public string Position { get; set; }
public string HeadShotUrl { get; set; }
public string OfferingTeam { get; set; }
public string Offer { get; set; }
public int Offer { get; set; }
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/Shared/Features/OfferMatching/MatchPlayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace DynamoLeagueBlazor.Shared.Features.OfferMatching
{
public class MatchPlayerRequest
{
public int PlayerId { get; set; }
public int Amount { get; set; }
}
}
3 changes: 1 addition & 2 deletions src/Tests/FakeFactory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using AutoBogus;
using DynamoLeagueBlazor.Server.Infrastructure.Identity;
using DynamoLeagueBlazor.Server.Infrastructure.Identity;
using DynamoLeagueBlazor.Server.Models;
using DynamoLeagueBlazor.Shared.Enums;

Expand Down
3 changes: 1 addition & 2 deletions src/Tests/Features/Fines/ManageFineTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using AutoBogus;
using DynamoLeagueBlazor.Server.Models;
using DynamoLeagueBlazor.Server.Models;
using DynamoLeagueBlazor.Shared.Features.Fines;
using System.Net.Http.Json;

Expand Down
3 changes: 1 addition & 2 deletions src/Tests/Features/FreeAgents/AddBidTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using AutoBogus;
using DynamoLeagueBlazor.Server.Models;
using DynamoLeagueBlazor.Server.Models;
using DynamoLeagueBlazor.Shared.Features.FreeAgents;
using Microsoft.Extensions.DependencyInjection;
using System.Net.Http.Json;
Expand Down
34 changes: 30 additions & 4 deletions src/Tests/Features/OfferMatching/ListTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System.Net.Http.Json;
using AutoBogus;
using DynamoLeagueBlazor.Client.Features.OfferMatching;
using DynamoLeagueBlazor.Client.Features.OfferMatching;
using DynamoLeagueBlazor.Server.Models;
using DynamoLeagueBlazor.Shared.Enums;
using DynamoLeagueBlazor.Shared.Features.OfferMatching;
using System.Net.Http.Json;

namespace DynamoLeagueBlazor.Tests.Features.OfferMatching;

Expand Down Expand Up @@ -64,7 +65,32 @@ public async Task GivenAnyAuthenticatedUser_WhenThereIsOnePlayerWhoIsInOfferMatc
freeAgent.Position.Should().Be(mockPlayer.Position);
freeAgent.HeadShotUrl.Should().Be(mockPlayer.HeadShotUrl);
freeAgent.OfferingTeam.Should().Be(mockTeam.Name);
freeAgent.Offer.Should().Be(bidAmount.ToString("C0"));
freeAgent.Offer.Should().Be(bidAmount);
}
[Fact]
public async Task GivenAnyAuthenticatedUser_AllowPlayerToBeMatched()
{
var application = CreateUserAuthenticatedApplication();

var player = CreateFakePlayer();
player.Position = Position.QuarterBack.Name;
morkusporkus marked this conversation as resolved.
Show resolved Hide resolved
player.YearContractExpires = DateTime.Now.Year;
await application.AddAsync(player);
var request = CreateFakeValidRequest();
morkusporkus marked this conversation as resolved.
Show resolved Hide resolved
request.PlayerId = player.Id;
var client = application.CreateClient();
await client.PostAsJsonAsync<MatchPlayerRequest>(OfferMatchingListRouteFactory.Uri, request);
var result = await application.FirstOrDefaultAsync<Player>();

result!.Rostered.Should().Be(false);
result.YearContractExpires.Should().Be(null);
result.EndOfFreeAgency.Should().Be(null);
result.YearAcquired.Should().Be(DateTime.Today.Year);
}
private static MatchPlayerRequest CreateFakeValidRequest()
{
var faker = new AutoFaker<MatchPlayerRequest>();
return faker.Generate();
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/Tests/Features/Players/AddFineTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using AutoBogus;
using DynamoLeagueBlazor.Server.Models;
using DynamoLeagueBlazor.Server.Models;
using DynamoLeagueBlazor.Shared.Features.Players;
using DynamoLeagueBlazor.Shared.Utilities;
using Microsoft.Extensions.DependencyInjection;
Expand Down
3 changes: 1 addition & 2 deletions src/Tests/Features/Teams/SignPlayerTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using AutoBogus;
using DynamoLeagueBlazor.Client.Features.Teams;
using DynamoLeagueBlazor.Client.Features.Teams;
using DynamoLeagueBlazor.Server.Models;
using DynamoLeagueBlazor.Shared.Enums;
using DynamoLeagueBlazor.Shared.Features.Teams;
Expand Down