public async Task SaveNewSeries(Series series) { Database.Entities.Series dbSeries = await _dbObjectBuilder.BuildDbSeriesAsync(series) .ConfigureAwait(false); dbSeries.UrlName = UrlUtility.GetUrlName(series.Name); dbSeries.UpdatedDate = DateTime.UtcNow; if (dbSeries.Season == null && series.Season.Id != Guid.Empty && series.Season.Start != default) { var season = _mapper.Map <dbObj.Season>(series.Season); _dbContext.Seasons.Add(season); dbSeries.Season = season; } if (dbSeries.Season == null) { throw new InvalidOperationException("Could not find or create season for new series."); } if (_dbContext.Series.Any(s => s.Id == series.Id || (s.ClubId == series.ClubId && s.Name == series.Name && s.Season.Id == series.Season.Id))) { throw new InvalidOperationException("Cannot create series. A series with this name in this season already exists."); } _dbContext.Series.Add(dbSeries); await _dbContext.SaveChangesAsync() .ConfigureAwait(false); await UpdateSeriesResults(dbSeries.Id, series.UpdatedBy) .ConfigureAwait(false); }
private async Task AddRaceToRegattaInternalAsync(Race race, Guid regattaId) { var dbFleet = await _dbContext.Fleets.SingleAsync(f => f.Id == race.Fleet.Id) .ConfigureAwait(false); var dbRegatta = await _dbContext.Regattas .Include(r => r.Season) .Include(r => r.RegattaSeries) .SingleAsync(c => c.Id == regattaId) .ConfigureAwait(false); var series = await _dbContext.Regattas.SelectMany(r => r.RegattaSeries.Select(rs => rs.Series)) .Include(s => s.RaceSeries) .SingleOrDefaultAsync(s => s.FleetId == dbFleet.Id) .ConfigureAwait(false); if (series == null) { // create a new series for this fleet. var seriesName = $"{dbRegatta.Season.Name} {dbRegatta.Name} {dbFleet.NickName}"; series = new Database.Entities.Series { ClubId = race.ClubId, Name = seriesName, Season = dbRegatta.Season, UpdatedDate = DateTime.UtcNow, ScoringSystem = dbRegatta.ScoringSystem, TrendOption = Api.Enumerations.TrendOption.PreviousRace, FleetId = dbFleet.Id, RaceSeries = new List <Database.Entities.SeriesRace>(), UrlName = UrlUtility.GetUrlName(seriesName) }; dbRegatta.RegattaSeries.Add(new dbObj.RegattaSeries { Regatta = dbRegatta, Series = series }); _dbContext.Series.Add(series); } series.RaceSeries.Add(new dbObj.SeriesRace { RaceId = race.Id, SeriesId = series.Id }); await _dbContext.SaveChangesAsync().ConfigureAwait(false); await _seriesService.UpdateSeriesResults(series.Id, race.UpdatedBy) .ConfigureAwait(false); }