public async Task SaveNew(Announcement announcement) { var dbEntity = _mapper.Map <Db.Announcement>(announcement); _dbContext.Announcements.Add(dbEntity); await _dbContext.SaveChangesAsync(); }
public async Task Delete(Guid seasonId) { var dbSeason = await _dbContext.Seasons.SingleAsync(c => c.Id == seasonId) .ConfigureAwait(false); _dbContext.Seasons.Remove(dbSeason); await _dbContext.SaveChangesAsync().ConfigureAwait(false); }
public async Task <Guid> SaveNewClub(Club club) { if (_dbContext.Clubs.Any(c => c.Initials == club.Initials)) { throw new InvalidOperationException("Cannot create club." + " A club with those initials already exists."); } club.Id = Guid.NewGuid(); var defaultSystem = club.DefaultScoringSystem; if (defaultSystem.Id == default) { defaultSystem.Id = Guid.NewGuid(); } if (defaultSystem != null) { if (club.ScoringSystems == null) { club.ScoringSystems = new List <ScoringSystem>(); } if (!club.ScoringSystems.Any(ss => ss == defaultSystem)) { club.ScoringSystems.Add(defaultSystem); } } if ((club.ScoringSystems?.Count ?? 0) > 0) { foreach (var system in club.ScoringSystems) { system.ClubId = club.Id; } } var dbClub = _mapper.Map <Db.Club>(club); _dbContext.Clubs.Add(dbClub); dbClub.DefaultScoringSystem = null; dbClub.DefaultScoringSystemId = null; var dbFleet = new Db.Fleet { Id = Guid.NewGuid(), ClubId = dbClub.Id, FleetType = Api.Enumerations.FleetType.AllBoatsInClub, IsHidden = false, ShortName = "All", Name = "All Boats in Club" }; _dbContext.Fleets.Add(dbFleet); await _dbContext.SaveChangesAsync() .ConfigureAwait(false); dbClub.DefaultScoringSystemId = defaultSystem.Id; await _dbContext.SaveChangesAsync() .ConfigureAwait(false); return(club.Id); }
public async Task GetSeriesForFleet_returnsFromDb() { //Arrange var race = await _context.Races.FirstAsync(); var fleet = await _context.Fleets.FirstAsync(); var series = await _context.Series.FirstAsync(); race.Fleet = fleet; series.RaceSeries = new List <Database.Entities.SeriesRace> { new Database.Entities.SeriesRace { RaceId = race.Id, SeriesId = series.Id } }; await _context.SaveChangesAsync(); // Act var returnedValue = await _service.GetSeriesForFleet(fleet.Id); // Assert Assert.NotEmpty(returnedValue); }
public async Task Merge(Guid targetCompetitorId, Guid sourceCompetitorId, string mergedBy) { _logger.LogInformation("Merging competitors {0} and {1}", targetCompetitorId, sourceCompetitorId); var scoresToMove = _dbContext.Scores .Where(s => s.CompetitorId == sourceCompetitorId); var seriesIds = await scoresToMove .Select(s => s.Race) .SelectMany(s => s.SeriesRaces) .Select(s => s.SeriesId) .Distinct() .ToListAsync() .ConfigureAwait(false); await scoresToMove.ForEachAsync(s => s.CompetitorId = targetCompetitorId) .ConfigureAwait(false); await _dbContext.SaveChangesAsync() .ConfigureAwait(false); await _competitorService.DeleteCompetitorAsync(sourceCompetitorId) .ConfigureAwait(false); foreach (var seriesId in seriesIds) { await _seriesService.UpdateSeriesResults(seriesId, mergedBy) .ConfigureAwait(false); } }
public async Task Delete(Guid boatClassId) { var dbClass = await _dbContext.BoatClasses.SingleAsync(c => c.Id == boatClassId) .ConfigureAwait(false); var fleets = await _dbContext.Fleets .Where(f => f.FleetType == Api.Enumerations.FleetType.SelectedClasses && f.FleetBoatClasses.Any(fbc => fbc.BoatClassId == dbClass.Id)) .ToListAsync().ConfigureAwait(false); _dbContext.Fleets.RemoveRange(fleets); _dbContext.BoatClasses.Remove(dbClass); await _dbContext.SaveChangesAsync().ConfigureAwait(false); }
public async Task <Guid> SaveNew(Fleet fleet) { if (_dbContext.Fleets.Any(f => f.ClubId == fleet.ClubId && (f.ShortName == fleet.ShortName || f.Name == fleet.Name))) { throw new InvalidOperationException("Cannot create fleet. A fleet with this name or short name already exists."); } var dbFleet = _mapper.Map <Db.Fleet>(fleet); dbFleet.Id = Guid.NewGuid(); dbFleet.FleetBoatClasses = new List <Db.FleetBoatClass>(); if ((fleet.BoatClasses?.Count ?? 0) != 0) { foreach (var newClass in fleet.BoatClasses) { dbFleet.FleetBoatClasses.Add( new Db.FleetBoatClass { BoatClassId = newClass.Id, FleetId = dbFleet.Id }); } } dbFleet.CompetitorFleets = new List <Db.CompetitorFleet>(); if ((fleet.Competitors?.Count ?? 0) != 0) { foreach (var newComp in fleet.Competitors) { dbFleet.CompetitorFleets.Add( new Db.CompetitorFleet { CompetitorId = newComp.Id, FleetId = dbFleet.Id }); } } _dbContext.Fleets.Add(dbFleet); await _dbContext.SaveChangesAsync() .ConfigureAwait(false); return(dbFleet.Id); }
public async Task GetScoreCodesAsync_ReturnsOneDnc() { var scoringSystem = await _context.ScoringSystems.FirstAsync(ss => ss.ClubId != null); _context.ScoreCodes.Add(new Database.Entities.ScoreCode { ScoringSystemId = scoringSystem.Id, Name = "DNC", CameToStart = false }); await _context.SaveChangesAsync(); // Act var results = await _service.GetScoreCodesAsync(scoringSystem.ClubId.Value); // Assert Assert.Equal(1, results.Count(sc => sc.Name == "DNC")); }
public async Task <Guid> Submit(ClubRequest clubRequest) { var dbObj = _mapper.Map <Db.ClubRequest>(clubRequest); _dbContext.ClubRequests.Add(dbObj); await _dbContext.SaveChangesAsync().ConfigureAwait(false); return(dbObj.Id); }
public async Task SaveScoringSystemAsync(ScoringSystem scoringSystem) { if (scoringSystem.Id == Guid.Empty) { scoringSystem.Id = Guid.NewGuid(); } var dbObject = await _dbContext.ScoringSystems .Where(s => s.Id == scoringSystem.Id).SingleOrDefaultAsync() .ConfigureAwait(false); if (dbObject == null) { var newDbObject = _mapper.Map <Db.ScoringSystem>(scoringSystem); _dbContext.ScoringSystems.Add(newDbObject); } else { _mapper.Map(scoringSystem, dbObject); } await _dbContext.SaveChangesAsync().ConfigureAwait(false); }
private async Task SaveInternalAsync(Model.Competitor comp) { var dbObject = await _dbContext .Competitors .Include(c => c.CompetitorFleets) .FirstOrDefaultAsync( c => c.Id == comp.Id) .ConfigureAwait(false); if (dbObject == null) { if (comp.Id == Guid.Empty) { comp.Id = Guid.NewGuid(); } dbObject = _mapper.Map <Db.Competitor>(comp); await _dbContext.Competitors.AddAsync(dbObject) .ConfigureAwait(false); } else { dbObject.Name = comp.Name; dbObject.SailNumber = comp.SailNumber; dbObject.AlternativeSailNumber = comp.AlternativeSailNumber; dbObject.BoatName = comp.BoatName; dbObject.Notes = comp.Notes; dbObject.IsActive = comp.IsActive; dbObject.HomeClubName = comp.HomeClubName; // should scores get added here? // I don't think so. Those will be recorded as a race update or scores update. } AddFleetsToDbObject(comp, dbObject); await _dbContext.SaveChangesAsync() .ConfigureAwait(false); }
private async Task SaveHistoricalResults(Series series) { DateTime currentDate = DateTime.Today; FlatModel.FlatResults results = FlattenResults(series); var oldResults = await _dbContext .HistoricalResults .Where(r => r.SeriesId == series.Id).ToListAsync() .ConfigureAwait(false); oldResults.ForEach(r => r.IsCurrent = false); var todayPrevious = oldResults .Where(r => r.Created >= currentDate) .ToList(); todayPrevious.ForEach(r => _dbContext.HistoricalResults.Remove(r)); var older = oldResults .Where(r => r.Created < currentDate) .OrderByDescending(r => r.Created) .Skip(1) .ToList(); older.ForEach(r => _dbContext.HistoricalResults.Remove(r)); _dbContext.HistoricalResults.Add(new dbObj.HistoricalResults { SeriesId = series.Id, Results = Newtonsoft.Json.JsonConvert.SerializeObject(results), IsCurrent = true, Created = DateTime.Now }); await _dbContext.SaveChangesAsync() .ConfigureAwait(false); }
private async Task <Guid> SaveNewRegattaInternalAsync(Regatta regatta) { Database.Entities.Regatta dbRegatta = await _dbObjectBuilder.BuildDbRegattaAsync(regatta) .ConfigureAwait(false); dbRegatta.UrlName = UrlUtility.GetUrlName(dbRegatta.Name); dbRegatta.UpdatedDate = DateTime.UtcNow; if (dbRegatta.Season == null && regatta.Season.Id != Guid.Empty && regatta.Season.Start != default) { var season = _mapper.Map <dbObj.Season>(regatta.Season); _dbContext.Seasons.Add(season); dbRegatta.Season = season; } if (dbRegatta.Season == null) { throw new InvalidOperationException( "Could not find or create season for new Regatta."); } if (_dbContext.Regattas.Any(s => s.Id == regatta.Id || (s.ClubId == regatta.ClubId && s.UrlName == regatta.UrlName && s.Season.Id == dbRegatta.Season.Id))) { throw new InvalidOperationException( "Cannot create regatta. A regatta with this name in this season already exists."); } _dbContext.Regattas.Add(dbRegatta); await _dbContext.SaveChangesAsync() .ConfigureAwait(false); return(dbRegatta.Id); }
public async Task AddPermission( Guid clubId, string userEmail, string addedBy) { var existingPermision = await _dbContext.UserPermissions .FirstOrDefaultAsync(p => p.UserEmail == userEmail && p.ClubId == clubId) .ConfigureAwait(false); if (existingPermision == null) { _dbContext.UserPermissions.Add( new Database.Entities.UserClubPermission { ClubId = clubId, UserEmail = userEmail, CanEditAllClubs = false, Created = DateTime.UtcNow, CreatedBy = addedBy }); await _dbContext.SaveChangesAsync() .ConfigureAwait(false); } }
private async Task<Guid> SaveInternalAsync(RaceDto race) { var dbRace = new Db.Race { Id = Guid.NewGuid(), ClubId = race.ClubId }; if (race.Id != default) { dbRace = await _dbContext.Races .Include(r => r.Scores) .Include(r => r.SeriesRaces) .Include(r => r.Weather) .AsSingleQuery() .SingleAsync(r => r.Id == race.Id) .ConfigureAwait(false); } IEnumerable<Guid> seriesIdsToUpdate = dbRace.SeriesRaces?.Select(r => r.SeriesId)?.ToList() ?? new List<Guid>(); dbRace.Name = race.Name; dbRace.Order = race.Order; dbRace.Date = race.Date ?? DateTime.Today; dbRace.Description = race.Description; dbRace.State = race.State; dbRace.TrackingUrl = race.TrackingUrl; dbRace.UpdatedDate = DateTime.UtcNow; dbRace.UpdatedBy = race.UpdatedBy; PopulateWeather(race, dbRace); dbRace.Fleet = _dbContext.Fleets.SingleOrDefault(f => f.Id == race.FleetId); if (race.SeriesIds != null) { dbRace.SeriesRaces ??= new List<Db.SeriesRace>(); dbRace.SeriesRaces.Clear(); foreach (var seriesId in race.SeriesIds) { dbRace.SeriesRaces.Add(new Db.SeriesRace { SeriesId = seriesId, RaceId = dbRace.Id }); } } if (race.Scores != null) { dbRace.Scores ??= new List<Db.Score>(); dbRace.Scores.Clear(); foreach (var score in race.Scores) { var newScore = new Db.Score { Id = Guid.NewGuid(), CompetitorId = score.CompetitorId, Race = dbRace, Place = score.Place, Code = score.Code, CodePoints = score.CodePoints }; _dbContext.Scores.Add(newScore); } } if (race.Id == default) { _dbContext.Races.Add(dbRace); } else { _dbContext.Races.Update(dbRace); } await _dbContext.SaveChangesAsync() .ConfigureAwait(false); seriesIdsToUpdate = seriesIdsToUpdate.Union( dbRace.SeriesRaces?.Select(rs => rs.SeriesId).ToList() ?? new List<Guid>()); foreach (var seriesId in seriesIdsToUpdate) { AddUpdateSeriesJob(seriesId, race.UpdatedBy); } return dbRace.Id; }