From a2bb4a82f0cdbee7d5c2dcc2d6bf5506b134ead2 Mon Sep 17 00:00:00 2001 From: morkusporkus Date: Sun, 26 Jun 2022 14:39:35 -0500 Subject: [PATCH 01/11] add offer matching, still need page to update --- src/Client/Features/OfferMatching/List.razor | 2 +- .../Features/OfferMatching/List.razor.cs | 22 ++++++++-- src/Server/Features/OfferMatching/List.cs | 42 +++++++++++++++++-- src/Shared/Features/OfferMatching/List.cs | 2 +- .../Features/OfferMatching/MatchPlayer.cs | 8 ++++ src/Tests/FakeFactory.cs | 3 +- src/Tests/Features/Fines/ManageFineTests.cs | 3 +- src/Tests/Features/FreeAgents/AddBidTests.cs | 3 +- src/Tests/Features/OfferMatching/ListTests.cs | 5 +-- src/Tests/Features/Players/AddFineTests.cs | 3 +- src/Tests/Features/Teams/SignPlayerTests.cs | 3 +- 11 files changed, 75 insertions(+), 21 deletions(-) create mode 100644 src/Shared/Features/OfferMatching/MatchPlayer.cs diff --git a/src/Client/Features/OfferMatching/List.razor b/src/Client/Features/OfferMatching/List.razor index c162c8e..79b133d 100644 --- a/src/Client/Features/OfferMatching/List.razor +++ b/src/Client/Features/OfferMatching/List.razor @@ -20,7 +20,7 @@ - + diff --git a/src/Client/Features/OfferMatching/List.razor.cs b/src/Client/Features/OfferMatching/List.razor.cs index dbd5a67..48868b7 100644 --- a/src/Client/Features/OfferMatching/List.razor.cs +++ b/src/Client/Features/OfferMatching/List.razor.cs @@ -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(); @@ -30,7 +32,21 @@ protected override async Task OnInitializedAsync() _loading = false; } } + private async void MatchPlayerAsync(int playerId, int amount) + { + _player = new MatchPlayerRequest() { PlayerId = playerId, Amount = amount }; + 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 OnInitializedAsync(); + } public void Dispose() { _cts.Cancel(); diff --git a/src/Server/Features/OfferMatching/List.cs b/src/Server/Features/OfferMatching/List.cs index 6906ea5..6f2608f 100644 --- a/src/Server/Features/OfferMatching/List.cs +++ b/src/Server/Features/OfferMatching/List.cs @@ -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; } @@ -26,6 +28,12 @@ public async Task GetAsync(CancellationToken cancellati { return await _mediator.Send(new ListQuery(), cancellationToken); } + [HttpPost] + public async Task PostAsync([FromBody] MatchPlayerRequest request, CancellationToken cancellationToken) + { + var query = _mapper.Map(request); + return await _mediator.Send(query, cancellationToken); + } } public record ListQuery : IRequest { } @@ -62,7 +70,28 @@ public async Task Handle(ListQuery request, Cancellatio }; } } +public record MatchPlayerCommand(int PlayerId, int Amount) : IRequest { } + +public class MatchPlayerHandler : IRequestHandler +{ + private readonly ApplicationDbContext _dbContext; + public MatchPlayerHandler(ApplicationDbContext dbContext) + { + _dbContext = dbContext; + } + + public async Task 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; + } +} public class ListMappingProfile : Profile { public ListMappingProfile() @@ -71,8 +100,15 @@ public ListMappingProfile() .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 : 0) ); } } + +public class MatchPlayerMappingProfile : Profile +{ + public MatchPlayerMappingProfile() + { + CreateMap(); + } +} diff --git a/src/Shared/Features/OfferMatching/List.cs b/src/Shared/Features/OfferMatching/List.cs index c80977d..9e53d8e 100644 --- a/src/Shared/Features/OfferMatching/List.cs +++ b/src/Shared/Features/OfferMatching/List.cs @@ -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; } } } diff --git a/src/Shared/Features/OfferMatching/MatchPlayer.cs b/src/Shared/Features/OfferMatching/MatchPlayer.cs new file mode 100644 index 0000000..85eadd7 --- /dev/null +++ b/src/Shared/Features/OfferMatching/MatchPlayer.cs @@ -0,0 +1,8 @@ +namespace DynamoLeagueBlazor.Shared.Features.OfferMatching +{ + public class MatchPlayerRequest + { + public int PlayerId { get; set; } + public int Amount { get; set; } + } +} diff --git a/src/Tests/FakeFactory.cs b/src/Tests/FakeFactory.cs index fcb8401..ef40fc1 100644 --- a/src/Tests/FakeFactory.cs +++ b/src/Tests/FakeFactory.cs @@ -1,5 +1,4 @@ -using AutoBogus; -using DynamoLeagueBlazor.Server.Infrastructure.Identity; +using DynamoLeagueBlazor.Server.Infrastructure.Identity; using DynamoLeagueBlazor.Server.Models; using DynamoLeagueBlazor.Shared.Enums; diff --git a/src/Tests/Features/Fines/ManageFineTests.cs b/src/Tests/Features/Fines/ManageFineTests.cs index 5ce599d..c7f2a3e 100644 --- a/src/Tests/Features/Fines/ManageFineTests.cs +++ b/src/Tests/Features/Fines/ManageFineTests.cs @@ -1,5 +1,4 @@ -using AutoBogus; -using DynamoLeagueBlazor.Server.Models; +using DynamoLeagueBlazor.Server.Models; using DynamoLeagueBlazor.Shared.Features.Fines; using System.Net.Http.Json; diff --git a/src/Tests/Features/FreeAgents/AddBidTests.cs b/src/Tests/Features/FreeAgents/AddBidTests.cs index 6bd224e..788a474 100644 --- a/src/Tests/Features/FreeAgents/AddBidTests.cs +++ b/src/Tests/Features/FreeAgents/AddBidTests.cs @@ -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; diff --git a/src/Tests/Features/OfferMatching/ListTests.cs b/src/Tests/Features/OfferMatching/ListTests.cs index 042bd9a..901dc60 100644 --- a/src/Tests/Features/OfferMatching/ListTests.cs +++ b/src/Tests/Features/OfferMatching/ListTests.cs @@ -1,7 +1,6 @@ -using System.Net.Http.Json; -using AutoBogus; -using DynamoLeagueBlazor.Client.Features.OfferMatching; +using DynamoLeagueBlazor.Client.Features.OfferMatching; using DynamoLeagueBlazor.Shared.Features.OfferMatching; +using System.Net.Http.Json; namespace DynamoLeagueBlazor.Tests.Features.OfferMatching; diff --git a/src/Tests/Features/Players/AddFineTests.cs b/src/Tests/Features/Players/AddFineTests.cs index e89b771..00b67f2 100644 --- a/src/Tests/Features/Players/AddFineTests.cs +++ b/src/Tests/Features/Players/AddFineTests.cs @@ -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; diff --git a/src/Tests/Features/Teams/SignPlayerTests.cs b/src/Tests/Features/Teams/SignPlayerTests.cs index 3491f89..4aae67e 100644 --- a/src/Tests/Features/Teams/SignPlayerTests.cs +++ b/src/Tests/Features/Teams/SignPlayerTests.cs @@ -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; From 042024260f46cc8944ddaa540d025e2f76fb2ec9 Mon Sep 17 00:00:00 2001 From: morkusporkus Date: Mon, 27 Jun 2022 08:39:22 -0500 Subject: [PATCH 02/11] LoadDataAsync --- src/Client/Features/OfferMatching/List.razor.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Client/Features/OfferMatching/List.razor.cs b/src/Client/Features/OfferMatching/List.razor.cs index 48868b7..697752d 100644 --- a/src/Client/Features/OfferMatching/List.razor.cs +++ b/src/Client/Features/OfferMatching/List.razor.cs @@ -21,7 +21,7 @@ protected override async Task OnInitializedAsync() try { _loading = true; - _result = await HttpClient.GetFromJsonAsync(OfferMatchingListRouteFactory.Uri, _cts.Token) ?? new(); + await LoadDataAsync(); } catch (AccessTokenNotAvailableException exception) { @@ -32,6 +32,11 @@ protected override async Task OnInitializedAsync() _loading = false; } } + private async Task LoadDataAsync() + { + _result = await HttpClient.GetFromJsonAsync(OfferMatchingListRouteFactory.Uri, _cts.Token) ?? new(); + } + private async void MatchPlayerAsync(int playerId, int amount) { _player = new MatchPlayerRequest() { PlayerId = playerId, Amount = amount }; @@ -45,7 +50,7 @@ private async void MatchPlayerAsync(int playerId, int amount) { SnackBar.Add("Something went wrong...", Severity.Error); } - await OnInitializedAsync(); + await LoadDataAsync(); } public void Dispose() { From 16578ad3331de0277b2c9e71e386b2bf0a852901 Mon Sep 17 00:00:00 2001 From: morkusporkus Date: Mon, 27 Jun 2022 08:44:38 -0500 Subject: [PATCH 03/11] make null = 1 --- src/Server/Features/OfferMatching/List.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Server/Features/OfferMatching/List.cs b/src/Server/Features/OfferMatching/List.cs index 6f2608f..f43e267 100644 --- a/src/Server/Features/OfferMatching/List.cs +++ b/src/Server/Features/OfferMatching/List.cs @@ -100,7 +100,7 @@ public ListMappingProfile() .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 : 0) + ? s.Bids.GetHighestBidder().Amount : 1) ); } } From c4b217dba2a3571f9fc48f4eff513d3c454ca419 Mon Sep 17 00:00:00 2001 From: morkusporkus Date: Mon, 27 Jun 2022 10:00:57 -0500 Subject: [PATCH 04/11] update amount --- src/Server/Features/OfferMatching/List.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Server/Features/OfferMatching/List.cs b/src/Server/Features/OfferMatching/List.cs index f43e267..40f7a25 100644 --- a/src/Server/Features/OfferMatching/List.cs +++ b/src/Server/Features/OfferMatching/List.cs @@ -94,13 +94,14 @@ public async Task Handle(MatchPlayerCommand request, CancellationToken canc } public class ListMappingProfile : Profile { + private const int _minimumBid = 1; public ListMappingProfile() { CreateMap() .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 : 1) + ? s.Bids.GetHighestBidder().Amount : _minimumBid) ); } } From 21925170bab2ec6923394c08173130b9c617fc1f Mon Sep 17 00:00:00 2001 From: morkusporkus Date: Mon, 27 Jun 2022 13:38:06 -0500 Subject: [PATCH 05/11] close --- src/Tests/Features/OfferMatching/ListTests.cs | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Tests/Features/OfferMatching/ListTests.cs b/src/Tests/Features/OfferMatching/ListTests.cs index 901dc60..c15381d 100644 --- a/src/Tests/Features/OfferMatching/ListTests.cs +++ b/src/Tests/Features/OfferMatching/ListTests.cs @@ -1,4 +1,5 @@ using DynamoLeagueBlazor.Client.Features.OfferMatching; +using DynamoLeagueBlazor.Shared.Enums; using DynamoLeagueBlazor.Shared.Features.OfferMatching; using System.Net.Http.Json; @@ -63,9 +64,34 @@ 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; + player.YearContractExpires = DateTime.Now.Year; + await application.AddAsync(player); + var request = CreateFakeValidRequest(); + request.PlayerId = player.Id; + var client = application.CreateClient(); + var result = await client.PostAsJsonAsync(OfferMatchingListRouteFactory.Uri, request); + + result.Should().NotBeNull(); + player.Rostered.Should().Be(false); + player.YearContractExpires.Should().Be(null); + player.EndOfFreeAgency.Should().Be(null); + player.YearAcquired.Should().Be(DateTime.Today.Year); + } + private static MatchPlayerRequest CreateFakeValidRequest() + { + var faker = new AutoFaker(); + return faker.Generate(); + } +} public class ListClientTests : UITestBase { From 4bff549697a3c7952e1b6212e5aa3f215cc76fff Mon Sep 17 00:00:00 2001 From: morkusporkus Date: Mon, 27 Jun 2022 14:11:21 -0500 Subject: [PATCH 06/11] test --- src/Server/Features/OfferMatching/List.cs | 1 + src/Tests/Features/OfferMatching/ListTests.cs | 13 +++++++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Server/Features/OfferMatching/List.cs b/src/Server/Features/OfferMatching/List.cs index 40f7a25..4db1062 100644 --- a/src/Server/Features/OfferMatching/List.cs +++ b/src/Server/Features/OfferMatching/List.cs @@ -91,6 +91,7 @@ public async Task Handle(MatchPlayerCommand request, CancellationToken canc await _dbContext.SaveChangesAsync(cancellationToken); return player.Id; } + } public class ListMappingProfile : Profile { diff --git a/src/Tests/Features/OfferMatching/ListTests.cs b/src/Tests/Features/OfferMatching/ListTests.cs index c15381d..0eb230a 100644 --- a/src/Tests/Features/OfferMatching/ListTests.cs +++ b/src/Tests/Features/OfferMatching/ListTests.cs @@ -1,4 +1,5 @@ using DynamoLeagueBlazor.Client.Features.OfferMatching; +using DynamoLeagueBlazor.Server.Models; using DynamoLeagueBlazor.Shared.Enums; using DynamoLeagueBlazor.Shared.Features.OfferMatching; using System.Net.Http.Json; @@ -78,13 +79,13 @@ public async Task GivenAnyAuthenticatedUser_AllowPlayerToBeMatched() var request = CreateFakeValidRequest(); request.PlayerId = player.Id; var client = application.CreateClient(); - var result = await client.PostAsJsonAsync(OfferMatchingListRouteFactory.Uri, request); + await client.PostAsJsonAsync(OfferMatchingListRouteFactory.Uri, request); + var result = await application.FirstOrDefaultAsync(); - result.Should().NotBeNull(); - player.Rostered.Should().Be(false); - player.YearContractExpires.Should().Be(null); - player.EndOfFreeAgency.Should().Be(null); - player.YearAcquired.Should().Be(DateTime.Today.Year); + 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() { From 2cd4697c70caa84e879d05f486de61cad4ae6684 Mon Sep 17 00:00:00 2001 From: morkusporkus Date: Mon, 27 Jun 2022 14:12:23 -0500 Subject: [PATCH 07/11] clean --- src/Server/Features/OfferMatching/List.cs | 2 +- src/Tests/Features/OfferMatching/ListTests.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Server/Features/OfferMatching/List.cs b/src/Server/Features/OfferMatching/List.cs index 4db1062..c775654 100644 --- a/src/Server/Features/OfferMatching/List.cs +++ b/src/Server/Features/OfferMatching/List.cs @@ -91,7 +91,7 @@ public async Task Handle(MatchPlayerCommand request, CancellationToken canc await _dbContext.SaveChangesAsync(cancellationToken); return player.Id; } - + } public class ListMappingProfile : Profile { diff --git a/src/Tests/Features/OfferMatching/ListTests.cs b/src/Tests/Features/OfferMatching/ListTests.cs index 0eb230a..069eb01 100644 --- a/src/Tests/Features/OfferMatching/ListTests.cs +++ b/src/Tests/Features/OfferMatching/ListTests.cs @@ -92,7 +92,7 @@ private static MatchPlayerRequest CreateFakeValidRequest() var faker = new AutoFaker(); return faker.Generate(); } -} +} public class ListClientTests : UITestBase { From 7e7c9512ee66b406a00b7bafa3f5147af7c8ea1d Mon Sep 17 00:00:00 2001 From: morkusporkus Date: Mon, 27 Jun 2022 17:29:03 -0500 Subject: [PATCH 08/11] update to not return id --- src/Server/Features/OfferMatching/List.cs | 12 ++++++------ src/Tests/Features/OfferMatching/ListTests.cs | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Server/Features/OfferMatching/List.cs b/src/Server/Features/OfferMatching/List.cs index c775654..56e7a6a 100644 --- a/src/Server/Features/OfferMatching/List.cs +++ b/src/Server/Features/OfferMatching/List.cs @@ -29,7 +29,7 @@ public async Task GetAsync(CancellationToken cancellati return await _mediator.Send(new ListQuery(), cancellationToken); } [HttpPost] - public async Task PostAsync([FromBody] MatchPlayerRequest request, CancellationToken cancellationToken) + public async Task PostAsync([FromBody] MatchPlayerRequest request, CancellationToken cancellationToken) { var query = _mapper.Map(request); return await _mediator.Send(query, cancellationToken); @@ -70,9 +70,9 @@ public async Task Handle(ListQuery request, Cancellatio }; } } -public record MatchPlayerCommand(int PlayerId, int Amount) : IRequest { } +public record MatchPlayerCommand(int PlayerId, int Amount) : IRequest { } -public class MatchPlayerHandler : IRequestHandler +public class MatchPlayerHandler : IRequestHandler { private readonly ApplicationDbContext _dbContext; @@ -81,7 +81,7 @@ public MatchPlayerHandler(ApplicationDbContext dbContext) _dbContext = dbContext; } - public async Task Handle(MatchPlayerCommand request, CancellationToken cancellationToken) + public async Task Handle(MatchPlayerCommand request, CancellationToken cancellationToken) { var player = (await _dbContext.Players .AsTracking() @@ -89,9 +89,9 @@ public async Task Handle(MatchPlayerCommand request, CancellationToken canc player.ContractValue = request.Amount; player.SetToUnsigned(); await _dbContext.SaveChangesAsync(cancellationToken); - return player.Id; + return Unit.Value; } - + } public class ListMappingProfile : Profile { diff --git a/src/Tests/Features/OfferMatching/ListTests.cs b/src/Tests/Features/OfferMatching/ListTests.cs index 069eb01..0eb230a 100644 --- a/src/Tests/Features/OfferMatching/ListTests.cs +++ b/src/Tests/Features/OfferMatching/ListTests.cs @@ -92,7 +92,7 @@ private static MatchPlayerRequest CreateFakeValidRequest() var faker = new AutoFaker(); return faker.Generate(); } -} +} public class ListClientTests : UITestBase { From d4b38d0a2fe10506e39db0a9fe52f3853b82691c Mon Sep 17 00:00:00 2001 From: morkusporkus Date: Mon, 27 Jun 2022 21:06:56 -0500 Subject: [PATCH 09/11] REMOVE THE EVIL VOID --- src/Client/Features/OfferMatching/List.razor.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Client/Features/OfferMatching/List.razor.cs b/src/Client/Features/OfferMatching/List.razor.cs index 697752d..0205e83 100644 --- a/src/Client/Features/OfferMatching/List.razor.cs +++ b/src/Client/Features/OfferMatching/List.razor.cs @@ -12,7 +12,6 @@ public sealed partial class List : IDisposable [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(); @@ -37,10 +36,9 @@ private async Task LoadDataAsync() _result = await HttpClient.GetFromJsonAsync(OfferMatchingListRouteFactory.Uri, _cts.Token) ?? new(); } - private async void MatchPlayerAsync(int playerId, int amount) + private async Task MatchPlayerAsync(int playerId, int amount) { - _player = new MatchPlayerRequest() { PlayerId = playerId, Amount = amount }; - var response = await HttpClient.PostAsJsonAsync(OfferMatchingListRouteFactory.Uri, _player); + var response = await HttpClient.PostAsJsonAsync(OfferMatchingListRouteFactory.Uri, new MatchPlayerRequest() { PlayerId = playerId, Amount = amount }); if (response.IsSuccessStatusCode) { From f30b3e99b5e53393219601136df7f614af896b92 Mon Sep 17 00:00:00 2001 From: morkusporkus Date: Mon, 27 Jun 2022 21:32:48 -0500 Subject: [PATCH 10/11] clean up offermatching --- .../Features/OfferMatching/List.razor.cs | 4 +- src/Server/Features/FreeAgents/List.cs | 4 +- src/Server/Features/OfferMatching/List.cs | 48 ++++++++----------- src/Server/Models/Bid.cs | 2 +- .../Features/OfferMatching/MatchPlayer.cs | 1 - src/Tests/Features/OfferMatching/ListTests.cs | 25 +++++----- 6 files changed, 37 insertions(+), 47 deletions(-) diff --git a/src/Client/Features/OfferMatching/List.razor.cs b/src/Client/Features/OfferMatching/List.razor.cs index 0205e83..6113b19 100644 --- a/src/Client/Features/OfferMatching/List.razor.cs +++ b/src/Client/Features/OfferMatching/List.razor.cs @@ -38,17 +38,17 @@ private async Task LoadDataAsync() private async Task MatchPlayerAsync(int playerId, int amount) { - var response = await HttpClient.PostAsJsonAsync(OfferMatchingListRouteFactory.Uri, new MatchPlayerRequest() { PlayerId = playerId, Amount = amount }); + var response = await HttpClient.PostAsJsonAsync(OfferMatchingListRouteFactory.Uri, new MatchPlayerRequest() { PlayerId = playerId }); if (response.IsSuccessStatusCode) { SnackBar.Add("Successfully retained player.", Severity.Success); + await LoadDataAsync(); } else { SnackBar.Add("Something went wrong...", Severity.Error); } - await LoadDataAsync(); } public void Dispose() { diff --git a/src/Server/Features/FreeAgents/List.cs b/src/Server/Features/FreeAgents/List.cs index 718529a..0c319f0 100644 --- a/src/Server/Features/FreeAgents/List.cs +++ b/src/Server/Features/FreeAgents/List.cs @@ -74,12 +74,12 @@ public ListMappingProfile() .ForMember(d => d.Team, mo => mo.MapFrom(s => s.Team != null ? s.Team.Name : string.Empty)) .ForMember(d => d.CurrentUserIsHighestBidder, mo => mo.MapFrom(s => s.Bids.Any() && - s.Bids.GetHighestBidder().TeamId == currentUserTeamId) + s.Bids.GetHighestBid().TeamId == currentUserTeamId) ) .ForMember(d => d.BiddingEnds, mo => mo.MapFrom(s => s.EndOfFreeAgency!.Value.ToShortDateString())) .ForMember(d => d.HighestBid, mo => mo.MapFrom(s => s.Bids.Any() - ? s.Bids.GetHighestBidder().Amount.ToString("C0") + ? s.Bids.GetHighestBid().Amount.ToString("C0") : string.Empty) ); } diff --git a/src/Server/Features/OfferMatching/List.cs b/src/Server/Features/OfferMatching/List.cs index 56e7a6a..8022f94 100644 --- a/src/Server/Features/OfferMatching/List.cs +++ b/src/Server/Features/OfferMatching/List.cs @@ -14,12 +14,10 @@ namespace DynamoLeagueBlazor.Server.Features.OfferMatching; [Route(OfferMatchingListRouteFactory.Uri)] public class ListController : ControllerBase { - private readonly IMapper _mapper; private readonly IMediator _mediator; - public ListController(IMapper mapper, IMediator mediator) + public ListController(IMediator mediator) { - _mapper = mapper; _mediator = mediator; } @@ -31,8 +29,7 @@ public async Task GetAsync(CancellationToken cancellati [HttpPost] public async Task PostAsync([FromBody] MatchPlayerRequest request, CancellationToken cancellationToken) { - var query = _mapper.Map(request); - return await _mediator.Send(query, cancellationToken); + return await _mediator.Send(new MatchPlayerCommand(request.PlayerId), cancellationToken); } } @@ -70,7 +67,20 @@ public async Task Handle(ListQuery request, Cancellatio }; } } -public record MatchPlayerCommand(int PlayerId, int Amount) : IRequest { } +public class ListMappingProfile : Profile +{ + private const int _minimumBid = 1; + public ListMappingProfile() + { + CreateMap() + .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.GetHighestBid().Amount : _minimumBid) + ); + } +} +public record MatchPlayerCommand(int PlayerId) : IRequest { } public class MatchPlayerHandler : IRequestHandler { @@ -85,32 +95,14 @@ public async Task Handle(MatchPlayerCommand request, CancellationToken can { var player = (await _dbContext.Players .AsTracking() + .Include(p => p.Bids) .SingleAsync(p => p.Id == request.PlayerId, cancellationToken)); - player.ContractValue = request.Amount; + player.ContractValue = player.Bids.GetHighestBid().Amount; player.SetToUnsigned(); + await _dbContext.SaveChangesAsync(cancellationToken); + return Unit.Value; } - -} -public class ListMappingProfile : Profile -{ - private const int _minimumBid = 1; - public ListMappingProfile() - { - CreateMap() - .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 : _minimumBid) - ); - } -} -public class MatchPlayerMappingProfile : Profile -{ - public MatchPlayerMappingProfile() - { - CreateMap(); - } } diff --git a/src/Server/Models/Bid.cs b/src/Server/Models/Bid.cs index bb764e0..1608f6a 100644 --- a/src/Server/Models/Bid.cs +++ b/src/Server/Models/Bid.cs @@ -20,6 +20,6 @@ public Bid(int amount, int teamId, int playerId) public static class BidExtensions { - public static Bid GetHighestBidder(this ICollection bids) + public static Bid GetHighestBid(this ICollection bids) => bids.OrderByDescending(b => b.Amount).First(); } diff --git a/src/Shared/Features/OfferMatching/MatchPlayer.cs b/src/Shared/Features/OfferMatching/MatchPlayer.cs index 85eadd7..c6cb874 100644 --- a/src/Shared/Features/OfferMatching/MatchPlayer.cs +++ b/src/Shared/Features/OfferMatching/MatchPlayer.cs @@ -3,6 +3,5 @@ public class MatchPlayerRequest { public int PlayerId { get; set; } - public int Amount { get; set; } } } diff --git a/src/Tests/Features/OfferMatching/ListTests.cs b/src/Tests/Features/OfferMatching/ListTests.cs index 0eb230a..4ffa0dc 100644 --- a/src/Tests/Features/OfferMatching/ListTests.cs +++ b/src/Tests/Features/OfferMatching/ListTests.cs @@ -1,6 +1,5 @@ using DynamoLeagueBlazor.Client.Features.OfferMatching; using DynamoLeagueBlazor.Server.Models; -using DynamoLeagueBlazor.Shared.Enums; using DynamoLeagueBlazor.Shared.Features.OfferMatching; using System.Net.Http.Json; @@ -68,31 +67,31 @@ public async Task GivenAnyAuthenticatedUser_WhenThereIsOnePlayerWhoIsInOfferMatc freeAgent.Offer.Should().Be(bidAmount); } [Fact] - public async Task GivenAnyAuthenticatedUser_AllowPlayerToBeMatched() + public async Task GivenAnyAuthenticatedUser_WhenPlayerIsMatched_ThenPlayerIsMovedToUnsignedStatus() { var application = CreateUserAuthenticatedApplication(); + var team = CreateFakeTeam(); + await application.AddAsync(team); var player = CreateFakePlayer(); - player.Position = Position.QuarterBack.Name; - player.YearContractExpires = DateTime.Now.Year; + player.YearContractExpires = DateTime.MaxValue.Year; + player.AddBid(int.MaxValue, team.Id); await application.AddAsync(player); - var request = CreateFakeValidRequest(); + + var request = AutoFaker.Generate(); request.PlayerId = player.Id; var client = application.CreateClient(); - await client.PostAsJsonAsync(OfferMatchingListRouteFactory.Uri, request); - var result = await application.FirstOrDefaultAsync(); + await client.PostAsJsonAsync(OfferMatchingListRouteFactory.Uri, request); + + var result = await application.FirstOrDefaultAsync(); result!.Rostered.Should().Be(false); result.YearContractExpires.Should().Be(null); result.EndOfFreeAgency.Should().Be(null); result.YearAcquired.Should().Be(DateTime.Today.Year); + result.ContractValue.Should().Be(int.MaxValue); } - private static MatchPlayerRequest CreateFakeValidRequest() - { - var faker = new AutoFaker(); - return faker.Generate(); - } -} +} public class ListClientTests : UITestBase { From 972f4183958b0b843633644335c5219f83068d4f Mon Sep 17 00:00:00 2001 From: morkusporkus Date: Mon, 27 Jun 2022 21:41:26 -0500 Subject: [PATCH 11/11] merge --- src/Server/Features/FreeAgents/List.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Server/Features/FreeAgents/List.cs b/src/Server/Features/FreeAgents/List.cs index 36041dc..7382c6d 100644 --- a/src/Server/Features/FreeAgents/List.cs +++ b/src/Server/Features/FreeAgents/List.cs @@ -75,11 +75,11 @@ public ListMappingProfile() .ForMember(d => d.Team, mo => mo.MapFrom(s => s.Team != null ? s.Team.Name : string.Empty)) .ForMember(d => d.CurrentUserIsHighestBidder, mo => mo.MapFrom(s => s.Bids.Any() && - s.Bids.GetHighestBidder().TeamId == currentUserTeamId) + s.Bids.GetHighestBid().TeamId == currentUserTeamId) ) .ForMember(d => d.BiddingEnds, mo => mo.MapFrom(s => s.EndOfFreeAgency!.Value)) .ForMember(d => d.HighestBid, mo => mo.MapFrom(s => s.Bids.Any() - ? s.Bids.GetHighestBidder().Amount : 0) + ? s.Bids.GetHighestBid().Amount : 0) ); } }