public async Task <Unit> Handle(CreateFreeThrowCommand request, CancellationToken cancellationToken) { var incident = new Incident { MatchId = request.MatchId, Minutes = request.Minutes, Seconds = request.Seconds, IncidentType = IncidentType.FOUL, Quater = request.Quater, Flagged = request.Flagged }; var foul = new Foul { PlayerWhoFouledId = request.PlayerWhoFouledId, PlayerWhoWasFouledId = request.PlayerWhoWasFouledId, CoachId = request.CoachId, FoulType = request.FoulType, Incident = incident }; var freeThrow = new Domain.Entities.FreeThrows { PlayerShooterId = request.PlayerShooterId, AccurateShots = request.AccurateShots, Attempts = request.Attempts, Foul = foul }; _context.FreeThrow.Add(freeThrow); var success = await _context.SaveChangesAsync(cancellationToken) > 0; if (success) { return(Unit.Value); } throw new Exception("Problem saving changes"); }
public async Task <int> Handle(CreateFoulCommand request, CancellationToken cancellationToken) { Match match = await _context.Match .Include(x => x.TeamGuest) .Include(x => x.TeamHome) .FirstOrDefaultAsync(x => x.Id == request.MatchId, cancellationToken); TeamMatch teamMatchWhoFouled; TeamMatch teamMatchWhoWasFouled; if (request.IsGuest) { teamMatchWhoFouled = match.TeamGuest; teamMatchWhoWasFouled = match.TeamHome; } else { teamMatchWhoFouled = match.TeamHome; teamMatchWhoWasFouled = match.TeamGuest; } switch (request.Quater) { case 1: teamMatchWhoFouled.Fouls1Qtr++; break; case 2: teamMatchWhoFouled.Fouls2Qtr++; break; case 3: teamMatchWhoFouled.Fouls3Qtr++; break; case 4: teamMatchWhoFouled.Fouls4Qtr++; break; } var incident = new Incident { MatchId = request.MatchId, Minutes = request.Minutes, Seconds = request.Seconds, IncidentType = IncidentType.FOUL, Quater = request.Quater, Flagged = request.Flagged, IsGuest = request.IsGuest }; var playerMatchWhoFouled = await _context.PlayerMatch.Include(x => x.PlayerSeason).FirstOrDefaultAsync( x => x.PlayerSeason.PlayerId == request.PlayerWhoFouledId && x.MatchId == request.MatchId, cancellationToken); var playerMatchWhoWasFouled = await _context.PlayerMatch.Include(x => x.PlayerSeason).FirstOrDefaultAsync( x => x.PlayerSeason.PlayerId == request.PlayerWhoWasFouledId && x.MatchId == request.MatchId, cancellationToken); if (playerMatchWhoFouled == null) { var playerSeason = await _context.PlayerSeason.FirstOrDefaultAsync(x => x.PlayerId == request.PlayerWhoFouledId, cancellationToken); playerMatchWhoFouled = new PlayerMatch { PlayerSeasonId = playerSeason.Id, MatchId = request.MatchId }; _context.PlayerMatch.Add(playerMatchWhoFouled); } if (playerMatchWhoWasFouled == null) { var playerSeason = await _context.PlayerSeason.FirstOrDefaultAsync(x => x.PlayerId == request.PlayerWhoWasFouledId, cancellationToken); playerMatchWhoWasFouled = new PlayerMatch { PlayerSeasonId = playerSeason.Id, MatchId = request.MatchId }; _context.PlayerMatch.Add(playerMatchWhoWasFouled); } if (request.FoulType == FoulType.OFFENSIVE) { playerMatchWhoFouled.OffFouls++; teamMatchWhoFouled.OffFouls++; } playerMatchWhoFouled.Fouls++; teamMatchWhoFouled.Fouls++; var foul = new Foul { PlayerWhoFouledId = request.PlayerWhoFouledId, PlayerWhoWasFouledId = request.PlayerWhoWasFouledId, CoachId = request.CoachId, FoulType = request.FoulType, Incident = incident }; if (request.AccurateShots.HasValue && request.Attempts.HasValue) { teamMatchWhoWasFouled.Fta += request.Attempts.Value; teamMatchWhoWasFouled.Ftm += request.AccurateShots.Value; playerMatchWhoWasFouled.Fta += request.Attempts.Value; playerMatchWhoWasFouled.Ftm += request.AccurateShots.Value; var freeThrow = new Domain.Entities.FreeThrows { AccurateShots = request.AccurateShots.Value, Attempts = request.Attempts.Value, Foul = foul, PlayerShooterId = request.PlayerWhoWasFouledId.Value }; _context.FreeThrow.Add(freeThrow); if (request.PlayerAssistId.HasValue) { var assist = new Assist { FreeThrows = freeThrow, PlayerId = request.PlayerAssistId.Value }; var playerMatchAssist = await _context.PlayerMatch.Include(x => x.PlayerSeason).FirstOrDefaultAsync( x => x.PlayerSeason.PlayerId == request.PlayerAssistId && x.MatchId == request.MatchId, cancellationToken); if (playerMatchAssist == null) { var playerSeason = await _context.PlayerSeason.FirstOrDefaultAsync(x => x.PlayerId == request.PlayerAssistId, cancellationToken); playerMatchAssist = new PlayerMatch { PlayerSeasonId = playerSeason.Id, MatchId = request.MatchId }; _context.PlayerMatch.Add(playerMatchAssist); } playerMatchAssist.Ast++; teamMatchWhoWasFouled.Ast++; _context.Assist.Add(assist); } if (request.PlayerReboundId.HasValue || request.TeamReboundId.HasValue) { var rebound = new Rebound { FreeThrows = freeThrow, ReboundType = request.ReboundType.Value }; if (request.PlayerReboundId.HasValue) { rebound.PlayerId = request.PlayerReboundId.Value; var playerMatchRebound = await _context.PlayerMatch.Include(x => x.PlayerSeason).FirstOrDefaultAsync( x => x.PlayerSeason.PlayerId == request.PlayerReboundId && x.MatchId == request.MatchId, cancellationToken); if (playerMatchRebound == null) { var playerSeason = await _context.PlayerSeason.FirstOrDefaultAsync(x => x.PlayerId == request.PlayerReboundId, cancellationToken); playerMatchRebound = new PlayerMatch { PlayerSeasonId = playerSeason.Id, MatchId = request.MatchId }; _context.PlayerMatch.Add(playerMatchRebound); } if (request.ReboundType == Domain.Common.ReboundType.PLAYER_DEF) { playerMatchRebound.Drb++; teamMatchWhoFouled.Drb++; } else { playerMatchRebound.Orb++; teamMatchWhoWasFouled.Orb++; } } else { rebound.TeamId = request.TeamReboundId.Value; if (request.ReboundType == Domain.Common.ReboundType.TEAM_DEF) { teamMatchWhoFouled.Drb++; } else { teamMatchWhoWasFouled.Orb++; } } _context.Rebound.Add(rebound); } } _context.Foul.Add(foul); var success = await _context.SaveChangesAsync(cancellationToken) > 0; if (success) { return(incident.Id); } throw new Exception("Problem saving changes"); }