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

Commit

Permalink
handle empty bid lists (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
morkusporkus authored Jul 1, 2022
1 parent ea2a9b7 commit d3a8e53
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 13 deletions.
6 changes: 2 additions & 4 deletions src/Server/Features/FreeAgents/List.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,9 @@ 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.GetHighestBid().TeamId == currentUserTeamId)
s.Bids.FindHighestBid()!.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.GetHighestBid().Amount : 0)
);
.ForMember(d => d.HighestBid, mo => mo.MapFrom(s => s.GetHighestBidAmount()));
}
}
9 changes: 3 additions & 6 deletions src/Server/Features/OfferMatching/List.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public async Task<OfferMatchingListResult> Handle(ListQuery request, Cancellatio
var currentUserTeamId = _httpContextAccessor.HttpContext!.User.GetTeamId();

var offerMatches = await _dbContext.Players
.Include(p => p.Bids)
.Where(p => p.TeamId == currentUserTeamId)
.WhereIsOfferMatching()
.ProjectTo<OfferMatchingListResult.OfferMatchingItem>(_mapper.ConfigurationProvider)
Expand All @@ -69,15 +70,11 @@ public async Task<OfferMatchingListResult> Handle(ListQuery request, Cancellatio
}
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.GetHighestBid().Amount : _minimumBid)
);
.ForMember(d => d.Offer, mo => mo.MapFrom(s => s.GetHighestBidAmount()));
}
}
public record MatchPlayerCommand(int PlayerId) : IRequest { }
Expand All @@ -97,7 +94,7 @@ public async Task<Unit> Handle(MatchPlayerCommand request, CancellationToken can
.AsTracking()
.Include(p => p.Bids)
.SingleAsync(p => p.Id == request.PlayerId, cancellationToken));
player.ContractValue = player.Bids.GetHighestBid().Amount;
player.ContractValue = player.Bids.FindHighestBid()?.Amount ?? Bid.MinimumAmount;
player.SetToUnsigned();

await _dbContext.SaveChangesAsync(cancellationToken);
Expand Down
7 changes: 4 additions & 3 deletions src/Server/Models/Bid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public record Bid : BaseEntity
{
public const int MinimumAmount = 1;

public Bid(int amount, int teamId, int playerId)
{
PlayerId = playerId;
Expand All @@ -13,13 +15,12 @@ public Bid(int amount, int teamId, int playerId)
public int PlayerId { get; private set; }
public int TeamId { get; private set; }
public DateTime CreatedOn { get; private set; } = DateTime.Now;

public Team Team { get; private set; } = null!;
public Player Player { get; private set; } = null!;
}

public static class BidExtensions
{
public static Bid GetHighestBid(this ICollection<Bid> bids)
=> bids.OrderByDescending(b => b.Amount).First();
public static Bid? FindHighestBid(this ICollection<Bid> bids)
=> bids.OrderByDescending(b => b.Amount).FirstOrDefault();
}
2 changes: 2 additions & 0 deletions src/Server/Models/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public Fine AddFine(decimal amount, string reason)
return fine;
}

public int GetHighestBidAmount() => Bids.Any() ? Bids.FindHighestBid()!.Amount : Bid.MinimumAmount;

private bool IsEligibleForFreeAgencyExtension(int teamId)
{
var isBidByTheSameTeam = teamId == TeamId;
Expand Down
23 changes: 23 additions & 0 deletions src/Tests/Features/OfferMatching/ListTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,29 @@ public async Task GivenAnyAuthenticatedUser_WhenPlayerIsMatched_ThenPlayerIsMove
result.YearAcquired.Should().Be(DateTime.Today.Year);
result.ContractValue.Should().Be(int.MaxValue);
}

[Fact]
public async Task GivenAnyAuthenticatedUser_WhenPlayerHasNoBids_ThenContractValueIsOne()
{
int minimumBid = 1;
var application = CreateUserAuthenticatedApplication();
var team = CreateFakeTeam();
await application.AddAsync(team);

var player = CreateFakePlayer();
player.YearContractExpires = DateTime.MaxValue.Year;
await application.AddAsync(player);

var request = AutoFaker.Generate<MatchPlayerRequest>();
request.PlayerId = player.Id;
var client = application.CreateClient();

await client.PostAsJsonAsync(OfferMatchingListRouteFactory.Uri, request);

var result = await application.FirstOrDefaultAsync<Player>();
result!.ContractValue.Should().Be(minimumBid);

}
}

public class ListClientTests : UITestBase
Expand Down
16 changes: 16 additions & 0 deletions src/Tests/Models/PlayerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,20 @@ public void WhereIsOfferMatching_GivenAPlayer_WhenTheirFreeAgencyIsFourOrMoreDay

sut.WhereIsOfferMatching().Should().NotContain(player);
}

[Fact]
public void GivenAPlayerWithNoBids_WhenFindingTheHighestBid_ThenTheHighestBidIsTheMinimumAmount()
=> CreateFakePlayer()
.GetHighestBidAmount()
.Should().Be(Bid.MinimumAmount);

[Fact]
public void GivenAPlayerWithABid_ThenReturnsThatBidAmount()
{
var player = CreateFakePlayer();

player.AddBid(int.MaxValue, int.MaxValue);

player.GetHighestBidAmount().Should().Be(int.MaxValue);
}
}

0 comments on commit d3a8e53

Please sign in to comment.