public async Task <ActionResult> InitiateStandings(InitiateStandingsRequest initiateStandingsRequest) { try { var result = await _standingsService.InitiateAsync(initiateStandingsRequest); return(Created(result.url, result.createdDto)); } catch (ArgumentException aEx) { return(BadRequest(aEx.Message)); } }
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()); }