Esempio n. 1
0
        private async Task CreateCollectionAsync(Type collectionType, IMongoDatabase db)
        {
            var name = CollectionResolver.GetName(collectionType);
            await db.CreateCollectionAsync(name);

            await Indexes.CreateForTypeAsync(collectionType, db);
        }
Esempio n. 2
0
        public async Task UpdateAsync(Guid id, PlayerUpdate update)
        {
            if (id == Guid.Empty)
            {
                throw new ArgumentException("", nameof(id));
            }
            if (update == null)
            {
                throw new ArgumentNullException(nameof(update), "Player update model must be provided.");
            }

            var collectionName = CollectionResolver.GetName <PlayerDocument>();

            Logger.LogDebug($"Updating player '{id}'..");

            var updateDefinition = Builders <PlayerDocument> .Update
                                   .Set(p => p.Number, update.Number)
                                   .Set(p => p.Position, update.Position)
                                   .Set(p => p.Status, update.Status);

            UpdateResult result = await GetMongoDbContext().UpdateOneAsync(p => p.Id == id, updateDefinition);

            if (result.MatchedCount != 1)
            {
                throw new InvalidOperationException($"Failed to update player '{id}' because it doesn't exist.");
            }
        }
Esempio n. 3
0
        public async Task <List <Player> > GetAllAsync()
        {
            var collectionName = CollectionResolver.GetName <PlayerDocument>();

            List <PlayerDocument> documents = await GetMongoDbContext().FindAsync <PlayerDocument>();

            Logger.LogDebug($"Retrieved all players from '{collectionName}' collection.");

            return(documents.Select(PlayerDocument.ToCoreEntity).ToList());
        }
Esempio n. 4
0
        public async Task <List <WeekInfo> > GetAsync()
        {
            var collectionName = CollectionResolver.GetName <UpdateLogDocument>();

            var logs = await GetMongoDbContext().FindAsync <UpdateLogDocument>();

            Logger.LogDebug($"Retrieved updated weeks from '{collectionName}' collection.");

            return(logs.Select(l => new WeekInfo(l.Season, l.Week)).ToList());
        }
Esempio n. 5
0
        private async Task <List <Type> > GetMissingCollectionTypesAsync(IMongoDatabase db)
        {
            HashSet <string> existing = (await db.ListCollectionNames().ToListAsync())
                                        .Where(n => n.StartsWith(Collection.FfdbPrefix))
                                        .ToHashSet();

            return(CollectionResolver.GetDocumentTypes()
                   .Where(t => !existing.Contains(
                              CollectionResolver.GetName(t)))
                   .ToList());
        }
Esempio n. 6
0
        public async Task <bool> HasUpdatedWeekAsync(WeekInfo week)
        {
            var collectionName = CollectionResolver.GetName <UpdateLogDocument>();

            var builder = Builders <UpdateLogDocument> .Filter;
            var filter  = builder.Eq(l => l.Season, week.Season)
                          & builder.Eq(l => l.Week, week.Week);

            UpdateLogDocument log = await GetMongoDbContext().FindOneAsync(filter);

            return(log != null);
        }
Esempio n. 7
0
        public async Task <List <TeamWeekStats> > GetAsync(WeekInfo week)
        {
            var collectionName = CollectionResolver.GetName <WeekStatsTeamDocument>();

            var builder = Builders <WeekStatsTeamDocument> .Filter;
            var filter  = builder.Eq(s => s.Season, week.Season)
                          & builder.Eq(s => s.Week, week.Week);

            List <WeekStatsTeamDocument> documents = await GetMongoDbContext().FindAsync(filter);

            Logger.LogDebug($"Retrieved team week stats for week '{week}' from '{collectionName}' collection.");

            return(documents.Select(WeekStatsTeamDocument.ToCoreEntity).ToList());
        }
Esempio n. 8
0
        public async Task AddAsync(PlayerAdd player)
        {
            if (player == null)
            {
                throw new ArgumentNullException(nameof(player), "Player add model must be provided.");
            }

            var collectionName = CollectionResolver.GetName <PlayerDocument>();

            PlayerDocument document = PlayerDocument.FromCoreAddEntity(player);

            await GetMongoDbContext().InsertOneAsync(document);

            Logger.LogDebug($"Added player '{player.NflId}' as '{document.Id}' to '{collectionName}' collection.");
        }
Esempio n. 9
0
        public async Task AddAsync(WeekInfo week)
        {
            var collectionName = CollectionResolver.GetName <UpdateLogDocument>();

            var log = new UpdateLogDocument
            {
                Season     = week.Season,
                Week       = week.Week,
                UpdateTime = DateTime.UtcNow
            };

            await GetMongoDbContext().InsertOneAsync(log);

            Logger.LogDebug($"Successfully added update log for {week} to '{collectionName}' collection.");
        }
Esempio n. 10
0
        public async Task <List <string> > GetPlayerNflIdsAsync(WeekInfo week)
        {
            var collectionName = CollectionResolver.GetName <WeekStatsPlayerDocument>();

            MongoDbContext mongoDbContext = GetMongoDbContext();

            List <Guid> ids = await GetPlayerIdsAsync(week, mongoDbContext);

            Dictionary <Guid, string> idNflMap = await GetIdNflMapAsync(mongoDbContext);

            return(ids
                   .Where(id => idNflMap.ContainsKey(id))
                   .Select(id => idNflMap[id])
                   .ToList());
        }
Esempio n. 11
0
        public async Task AddAsync(List <TeamWeekStats> stats)
        {
            if (stats == null)
            {
                throw new ArgumentNullException(nameof(stats), "Stats must be provided.");
            }

            var collectionName = CollectionResolver.GetName <WeekStatsTeamDocument>();

            Logger.LogDebug($"Adding {stats.Count} team week stats to '{collectionName}' collection..");

            List <WeekStatsTeamDocument> documents = stats.Select(WeekStatsTeamDocument.FromCoreEntity).ToList();

            await GetMongoDbContext().InsertManyAsync(documents);

            Logger.LogDebug($"Added team week stats to '{collectionName}' collection.");
        }
Esempio n. 12
0
        public async Task AddAsync(List <WeekMatchup> matchups)
        {
            if (matchups == null)
            {
                throw new ArgumentNullException(nameof(matchups), "Week matchups must be provided.");
            }

            var collectionName = CollectionResolver.GetName <WeekMatchupDocument>();

            Logger.LogDebug($"Adding {matchups.Count} week matchups to '{collectionName}' collection..");

            List <WeekMatchupDocument> documents = matchups.Select(WeekMatchupDocument.FromCoreEntity).ToList();

            await GetMongoDbContext().InsertManyAsync(documents);

            Logger.LogDebug($"Added week matchups to '{collectionName}' collection.");
        }
Esempio n. 13
0
        public async Task AddAsync(List <Team> teams)
        {
            if (teams == null)
            {
                throw new ArgumentNullException(nameof(teams), "Teams must be provided.");
            }

            var collectionName = CollectionResolver.GetName <TeamDocument>();

            if (!teams.Any())
            {
                return;
            }

            var docs = teams.Select(TeamDocument.FromCoreEntity).ToList();

            await GetMongoDbContext().InsertManyAsync(docs);

            Logger.LogDebug($"Added {teams.Count} teams to '{collectionName}' collection.");
        }
Esempio n. 14
0
        public async Task AddAsync(List <PlayerWeekStats> stats)
        {
            if (stats == null)
            {
                throw new ArgumentNullException(nameof(stats), "Stats must be provided.");
            }

            MongoDbContext mongoDbContext = GetMongoDbContext();

            Logger.LogDebug($"Adding {stats.Count} week stats..");

            var(playerStats, dstStats) = GroupStats(stats);

            await AddPlayerStatsAsync(playerStats, mongoDbContext);

            Logger.LogDebug("Added player week stats to '{0}' collection.",
                            CollectionResolver.GetName <WeekStatsPlayerDocument>());

            await AddDstStatsAsync(dstStats, mongoDbContext);

            Logger.LogDebug("Added DST week stats to '{0}' collection.",
                            CollectionResolver.GetName <WeekStatsDstDocument>());
        }
Esempio n. 15
0
        // first set all player's team ids to null, then update
        public async Task UpdateRosterMappingsAsync(List <Roster> rosters)
        {
            if (rosters == null)
            {
                throw new ArgumentNullException(nameof(rosters), "Rosters must be provided.");
            }

            var collectionName = CollectionResolver.GetName <PlayerDocument>();

            Logger.LogDebug($"Updating roster mappings..");

            MongoDbContext mongoDbContext = GetMongoDbContext();

            await ClearRosterMappingsAsync(mongoDbContext);

            Dictionary <string, Guid> nflIdMap = await GetNflIdMapAsync(mongoDbContext);

            foreach (Roster roster in rosters)
            {
                await UpdateForRosterAsync(roster, nflIdMap, mongoDbContext);
            }

            Logger.LogDebug($"Updated roster mappings for players in '{collectionName}' collection.");
        }