private async Task <Result> ValidateAndGenerateResultAsync(PostResultRequest toAdd) { var season = Season.FromISOString(toAdd.Season); var standing = await _dbContext.Standings .Include(s => s.ResultsDuringTheSeason) .Include(s => s.League) .ThenInclude(l => l.Teams) .SingleOrDefaultAsync(s => s.SeasonId == season.FullName && s.LeagueId == toAdd.LeagueName); bool standingExists = standing != null; if (!standingExists) { throw new ArgumentException("Can't add the given result as there is no such standing that corresponds to this combination of year/league"); } var homeTeamEntity = standing.League.Teams.FirstOrDefault( t => t.Name.Equals(toAdd.HomeTeamName)); var awayTeamEntity = standing.League.Teams.FirstOrDefault( t => t.Name.Equals(toAdd.AwayTeamName)); if (homeTeamEntity == null || awayTeamEntity == null) { throw new ArgumentException("One of the teams, part of this result, does not exist"); } var entityToAdd = Result.FromRequest(toAdd); entityToAdd.HomeTeam = homeTeamEntity; entityToAdd.AwayTeam = awayTeamEntity; return(entityToAdd); }
public async Task <IEnumerable <StandingsDTO> > FindAsync(FindStandingsParams findTeamParams) { IQueryable <Standings> standingsQuery = _dbContext.Standings .Include(s => s.League) .Include(s => s.StandingRows) .ThenInclude(r => r.Team); if (findTeamParams.LeagueName != null) { standingsQuery = standingsQuery.Where(s => s.LeagueId == findTeamParams.LeagueName); } if (findTeamParams.Season != null) { var toSearch = Season.FromISOString(findTeamParams.Season); standingsQuery = standingsQuery.Where(s => s.SeasonId == toSearch.FullName); } return((await standingsQuery.ToListAsync()) .Select(s => s.ToDto())); }
public async Task <(string url, StandingsDTO createdDto)> InitiateAsync(InitiateStandingsRequest initiateStandingsRequest) { var newSeason = Season.FromISOString(initiateStandingsRequest.Season); bool standingsAlreadyExist = await _dbContext.Standings.FindAsync(newSeason.FullName, initiateStandingsRequest.LeagueName) != null; if (standingsAlreadyExist) { throw new ArgumentException("Standings for this league/season already exists"); } var existingLeague = await _dbContext.Leagues .Include(l => l.Teams) .SingleOrDefaultAsync(l => l.Name == initiateStandingsRequest.LeagueName); bool leagueAlreadyExists = existingLeague != null; if (!leagueAlreadyExists) { throw new ArgumentException("Cannot instantiate standings for the given League as it doesn't exist"); } var existingSeason = await _dbContext.Seasons.FindAsync(newSeason.FullName); var seasonToUse = existingSeason ?? newSeason; var newStandings = new Standings { League = existingLeague, Season = seasonToUse, StandingRows = existingLeague.Teams.Select(t => StandingRow.EmptyRow(t)).ToArray() }; _dbContext.Standings.Add(newStandings); await _dbContext.SaveChangesAsync(); return("todo: generate URL", newStandings.ToDto()); }