コード例 #1
0
        /// <summary>
        /// Saves multiple GbGame objects to the database, including
        /// relevaing joining rows. Ignores games that already exist in the DB.
        /// Also updates games whose info is at least 7 days old.
        /// </summary>
        /// <param name="input">A list of objects to save</param>
        public static void SaveGamesToDb(List <Game> input)
        {
            var dbGames = input.Select(CreateDbGameObjectWithoutNavigation).ToList();

            DbAccess.SaveListOfNewGames(dbGames, out var newGameIds, out var gamesToUpdate);

            var dbGamePlatforms = input
                                  .Where(x => newGameIds.Contains(x.Id))
                                  .SelectMany(CreateDbGamePlatformsListWithoutNavigation)
                                  .ToList();

            if (dbGamePlatforms.Any())
            {
                DbAccess.SaveGamePlatforms(dbGamePlatforms);
            }

            var dbGameGenres = input
                               .Where(x => newGameIds.Contains(x.Id))
                               .Where(x => x.Genres != null && x.Genres.Any())
                               .SelectMany(CreateDbGameGenresListWithoutNavigation)
                               .ToList();

            if (dbGameGenres.Any())
            {
                DbAccess.SaveGameGenres(dbGameGenres);
            }

            // Updating existing games with out-of-date info
            if (gamesToUpdate.Any())
            {
                var gameObjectsToUpdate = input.Where(x => gamesToUpdate.Contains(x.Id)).ToList();

                foreach (var v in gameObjectsToUpdate)
                {
                    var dGame = CreateDisplayGameObject(v.Id);

                    var dbGame = CreateDbGameObjectWithoutNavigation(v);
                    var dbGamePlatformsUpdate = CreateDbGamePlatformsListWithoutNavigation(v);

                    dbGame.IsRated           = dGame.IsRated;
                    dbGame.Basically         = dGame.Basically;
                    dbGame.RatingExplanation = dGame.RatingExplanation;
                    dbGame.RatingLastUpdated = dGame.RatingLastUpdated;
                    dbGame.LastUpdated       = DateTime.UtcNow;

                    DbAccess.SaveGame(dbGame);
                    DbAccess.SaveGamePlatforms(dbGamePlatformsUpdate);
                }
            }
        }