public async Task <Match> GetMatchAsync( int id, CancellationToken cancellationToken = default) { try { Entities.Match entity = await _context.Matches .Include(m => m.HomeTeam) .Include(m => m.AwayTeam) .FirstOrDefaultAsync(t => t.Id == id); if (entity == null) { throw new ObjectNotFoundException(); } Match match = _mapper.Map <Entities.Match, Match>(entity); return(match); } catch (ServiceException) { throw; } catch (Exception ex) { _logger.LogError(ex, $"Error retrieving Match with id: { id }"); throw Conversion.ConvertException(ex); } }
public static ViewModels.Match ToViewModel(this Entities.Match match) { if (match == null) { return(null); } return(new ViewModels.Match { MatchDate = match.MatchDate, Slug = match.Slug, Title = match.Title, ImageUrl = $"{match.ImageServer.ServerUrl}/{match.ImageName}" }); }
public async Task <int> CreateMatchAsync( CreateMatchRequest request, CancellationToken cancellationToken = default) { try { if (request == null) { throw new ArgumentNullException(nameof(request)); } if (request.Date == null) { throw new ArgumentException("Date cannot be null."); } if (request.Date > DateTime.Now) { throw new ArgumentException("Cannot register a match that is not yet played."); } if (request.HomeTeamId == request.AwayTeamId) { throw new ArgumentException("Cannot register match between the same team."); } bool homeTeamsExist = await _context.Teams .AnyAsync(t => t.Id == request.HomeTeamId); bool awayTeamsExist = await _context.Teams .AnyAsync(t => t.Id == request.AwayTeamId); if (!homeTeamsExist || !awayTeamsExist) { throw new ArgumentException("Either one of the teams does not exist in the database."); } int?winnerID = request.AwayTeamScore > request.HomeTeamScore ? request.AwayTeamId : request.AwayTeamScore < request.HomeTeamScore ? request.HomeTeamId : (int?)null; var entity = new Entities.Match { Date = request.Date, AwayTeamId = request.AwayTeamId, AwayTeamScore = request.AwayTeamScore, HomeTeamId = request.HomeTeamId, HomeTeamScore = request.HomeTeamScore, WinnerId = winnerID }; _context.Matches.Add(entity); await _context.SaveChangesAsync(cancellationToken); // Update team rankings if (winnerID.HasValue) { await UpdateTeamRankings(winnerID.Value, 3); } else { await UpdateTeamRankings(entity.AwayTeamId, 1); await UpdateTeamRankings(entity.HomeTeamId, 1); } return(entity.Id); } catch (ServiceException) { throw; } catch (Exception ex) { _logger.LogError(ex, "Error creating match."); throw Conversion.ConvertException(ex); } }
public async Task UpdateMatchAsync( UpdateMatchRequest request, CancellationToken cancellationToken = default) { try { if (request == null) { throw new ArgumentNullException(nameof(request)); } Entities.Match entity = await _context.Matches .FirstOrDefaultAsync(t => t.Id == request.MatchId); if (entity == null) { throw new ObjectNotFoundException($"Match with Id: { request.MatchId } does not exist."); } if (request.Date == null) { throw new ArgumentException("Date cannot be null."); } if (request.IsSet(x => x.Date)) { if (request.Date > DateTime.Now) { throw new ArgumentException("Cannot register a match that is not yet played."); } entity.Date = request.Date; } if (request.IsSet(x => x.HomeTeamScore)) { entity.HomeTeamScore = request.HomeTeamScore; } if (request.IsSet(x => x.AwayTeamScore)) { entity.AwayTeamScore = request.AwayTeamScore; } int?newWinnerID = entity.AwayTeamScore > entity.HomeTeamScore ? entity.AwayTeamId : entity.AwayTeamScore < entity.HomeTeamScore ? entity.HomeTeamId : (int?)null; bool hasWinnerChanged = newWinnerID != entity.WinnerId; bool wasDraw = !entity.WinnerId.HasValue; if (hasWinnerChanged) { int?loserID = wasDraw ? (int?)null : entity.AwayTeamId != entity.WinnerId ? entity.AwayTeamId : entity.HomeTeamId; // If old result was draw, remove 1 point from each team if (wasDraw) { await UpdateTeamRankings(entity.AwayTeamId, -1); await UpdateTeamRankings(entity.HomeTeamId, -1); } // If new result is draw, remove 2 points from old winner and add 1 point to other team, so both earn 1 point else if (entity.HomeTeamScore == entity.AwayTeamScore) { await UpdateTeamRankings(entity.WinnerId.Value, -2); await UpdateTeamRankings(loserID.Value, 1); } // If new result is not draw, remove 3 points from previous winner and add 3 points to new winner else { await UpdateTeamRankings(entity.WinnerId.Value, -3); await UpdateTeamRankings(loserID.Value, 3); } // Set new winner Id entity.WinnerId = newWinnerID; } await _context.SaveChangesAsync(cancellationToken); } catch (ServiceException) { throw; } catch (Exception ex) { _logger.LogError(ex, "Error updating match."); throw Conversion.ConvertException(ex); } }
public MatchScoreGroup(Entities.Match match, PlayerResults score) { Match = match; Score = score; }
private void Transcribe(Match match, LeagueTeam homeTeam, LeagueTeam guestTeam, Entities.Match entityMatch) { //trnsform din Match in entityMatch si din LeagueTeam in entityLeagueTeam, => update entityMatch.HomeGoals = match.HomeGoals; entityMatch.GuestGoals = match.GuestGoals; entityMatch.IsPlayed = true; homeTeam.Games = match.Home.GamesPlayed; homeTeam.Wins = match.Home.Wins; homeTeam.Draws = match.Home.Draws; homeTeam.Losses = match.Home.Losses; homeTeam.Points = match.Home.Points; homeTeam.GoalsFor = match.Home.GoalsFor; homeTeam.GoalsAgainst = match.Home.GoalsAgainst; homeTeam.GoalDifference = match.Home.GoalsAgainst; guestTeam.Games = match.Guest.GamesPlayed; guestTeam.Wins = match.Guest.Wins; guestTeam.Draws = match.Guest.Draws; guestTeam.Losses = match.Guest.Losses; guestTeam.Points = match.Guest.Points; guestTeam.GoalsFor = match.Guest.GoalsFor; guestTeam.GoalsAgainst = match.Guest.GoalsAgainst; guestTeam.GoalDifference = match.Guest.GoalsAgainst; }