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

Commit

Permalink
Add states for offer matching-> unsigned and setup automated job to m…
Browse files Browse the repository at this point in the history
…ove players every night at midnight (#108)
  • Loading branch information
benjaminsampica authored Jul 6, 2022
1 parent c88bd3d commit 665fae7
Show file tree
Hide file tree
Showing 18 changed files with 902 additions and 73 deletions.
2 changes: 0 additions & 2 deletions src/Server/Features/Admin/AddPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ public async Task<int> Handle(AddPlayerCommand request, CancellationToken cancel
TeamId = request.TeamId
};

player.SetToUnsigned();

_dbContext.Add(player);
await _dbContext.SaveChangesAsync(cancellationToken);

Expand Down
2 changes: 1 addition & 1 deletion src/Server/Features/FreeAgents/EndBidding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public async Task Invoke()

foreach (var player in players)
{
player.SetToOfferMatching();
player.EndBidding();
}

await _dbContext.SaveChangesAsync();
Expand Down
39 changes: 39 additions & 0 deletions src/Server/Features/OfferMatching/ExpireOfferMatching.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Coravel.Invocable;
using DynamoLeagueBlazor.Server.Infrastructure;
using Microsoft.EntityFrameworkCore;
using static DynamoLeagueBlazor.Server.Models.Player;

namespace DynamoLeagueBlazor.Server.Features.OfferMatching;

public class ExpireOfferMatchingService : IInvocable
{
private readonly ApplicationDbContext _dbContext;

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

public async Task Invoke()
{
var players = _dbContext.Players
.AsTracking()
.Include(p => p.Bids)
.Where(p => p.State == PlayerState.OfferMatching
&& p.EndOfFreeAgency!.Value.AddDays(3) <= DateTime.Today);

foreach (var player in players)
{
if (!player.Bids.Any())
{
_dbContext.Remove(player);
}
else
{
player.ExpireMatch();
}
}

await _dbContext.SaveChangesAsync();
}
}
4 changes: 2 additions & 2 deletions src/Server/Features/OfferMatching/List.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ 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.FindHighestBid()?.Amount ?? Bid.MinimumAmount;
player.SetToUnsigned();

player.MatchOffer();

await _dbContext.SaveChangesAsync(cancellationToken);

Expand Down
5 changes: 3 additions & 2 deletions src/Server/Features/Teams/Detail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using static DynamoLeagueBlazor.Server.Models.Player;
using static DynamoLeagueBlazor.Shared.Features.Teams.TeamDetailResult;

namespace DynamoLeagueBlazor.Server.Features.Teams;
Expand Down Expand Up @@ -70,8 +71,8 @@ public async Task<TeamDetailResult> Handle(DetailQuery request, CancellationToke
var unrosteredPlayersContractValue = await unrosteredPlayersQuery.SumAsync(urp => urp.ContractValue, cancellationToken);

var unsignedPlayersQuery = _dbContext.Players
.Where(p => p.TeamId == request.TeamId)
.WhereIsUnsigned();
.Where(p => p.TeamId == request.TeamId
&& p.State == PlayerState.Unsigned);
var unsignedPlayers = await unsignedPlayersQuery
.ProjectTo<PlayerItem>(_mapper.ConfigurationProvider)
.ToListAsync(cancellationToken);
Expand Down
5 changes: 3 additions & 2 deletions src/Server/Features/Teams/List.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using static DynamoLeagueBlazor.Server.Models.Player;
using static DynamoLeagueBlazor.Shared.Features.Teams.TeamListResult;

namespace DynamoLeagueBlazor.Server.Features.Teams;
Expand Down Expand Up @@ -73,8 +74,8 @@ await Parallel.ForEachAsync(teams, async (team, cancellationToken) =>
var unrosteredPlayersContractValue = await unrosteredPlayersQuery.SumAsync(urp => urp.ContractValue, cancellationToken);

var unsignedPlayersQuery = dbContext.Players
.Where(p => p.TeamId == team.Id)
.WhereIsUnsigned();
.Where(p => p.TeamId == team.Id
&& p.State == PlayerState.Unsigned);
var unsignedPlayerCount = await unsignedPlayersQuery.CountAsync(cancellationToken);
team.UnsignedPlayerCount = unsignedPlayerCount.ToString();

Expand Down
Loading

0 comments on commit 665fae7

Please sign in to comment.