public async Task SaveNew(BoatClass boatClass)
        {
            var dbBoatClass = _mapper.Map <Db.BoatClass>(boatClass);

            dbBoatClass.Id = Guid.NewGuid();
            _dbContext.BoatClasses.Add(dbBoatClass);
            var defaultShortName = new string(
                boatClass.Name
                .Where(c => !Char.IsWhiteSpace(c))
                .ToArray());

            var fleetId = Guid.NewGuid();

            var fleetName = boatClass.Name.EndsWith('s') ? boatClass.Name : $"{boatClass.Name}s";
            var dbFleet   = new Db.Fleet
            {
                Id               = fleetId,
                ClubId           = boatClass.ClubId,
                FleetType        = Api.Enumerations.FleetType.SelectedClasses,
                IsHidden         = false,
                ShortName        = defaultShortName,
                Name             = fleetName,
                FleetBoatClasses = new List <Db.FleetBoatClass>
                {
                    new Db.FleetBoatClass
                    {
                        BoatClassId = dbBoatClass.Id,
                        FleetId     = Guid.NewGuid()
                    }
                }
            };

            _dbContext.Fleets.Add(dbFleet);
            await _dbContext.SaveChangesAsync().ConfigureAwait(false);
        }
        private static void CleanUpCompetitors(Fleet fleet, Db.Fleet existingFleet)
        {
            var competitorsToRemove = existingFleet.CompetitorFleets.ToList();
            var competitorsToAdd    =
                fleet.Competitors != null?
                fleet.Competitors
                .Where(c =>
                       !(existingFleet.CompetitorFleets.Any(f => c.Id == f.CompetitorId)))
                .Select(c => new Db.CompetitorFleet {
                CompetitorId = c.Id, FleetId = existingFleet.Id
            })
                    : new List <Db.CompetitorFleet>();

            if (existingFleet.FleetType == Api.Enumerations.FleetType.SelectedBoats &&
                fleet.Competitors != null)
            {
                competitorsToRemove =
                    existingFleet.CompetitorFleets
                    .Where(c => !(fleet.Competitors.Any(cp => cp.Id == c.CompetitorId)))
                    .ToList();
            }


            foreach (var removingCompetitor in competitorsToRemove)
            {
                existingFleet.CompetitorFleets.Remove(removingCompetitor);
            }
            foreach (var addCompetitor in competitorsToAdd)
            {
                existingFleet.CompetitorFleets.Add(addCompetitor);
            }
        }
Exemple #3
0
        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);
        }
        private static void CleanUpClasses(Fleet fleet, Db.Fleet existingFleet)
        {
            var classesToRemove = existingFleet.FleetBoatClasses.ToList();

            if (existingFleet.FleetType == Api.Enumerations.FleetType.SelectedClasses &&
                fleet.BoatClasses != null)
            {
                classesToRemove =
                    existingFleet.FleetBoatClasses
                    .Where(f => !(fleet.BoatClasses.Any(c => c.Id == f.BoatClassId)))
                    .ToList();
            }
            var classesToAdd =
                fleet.BoatClasses != null?
                fleet.BoatClasses
                .Where(c =>
                       !(existingFleet.FleetBoatClasses.Any(f => c.Id == f.BoatClassId)))
                .Select(c => new Db.FleetBoatClass {
                BoatClassId = c.Id, FleetId = existingFleet.Id
            })
                    : new List <Db.FleetBoatClass>();

            foreach (var removingClass in classesToRemove)
            {
                existingFleet.FleetBoatClasses.Remove(removingClass);
            }
            foreach (var addClass in classesToAdd)
            {
                existingFleet.FleetBoatClasses.Add(addClass);
            }
        }