/// <summary> /// Updates all non-DIHMT-specific fields of the /// game in the database /// </summary> /// <param name="dGame">The game to be updated</param> /// <param name="includeGenres">Indicates whether or not the DbGameGenres-table should be updated, too</param> private static void UpdateGameFromGb(DisplayGame dGame, bool includeGenres = false) { var gbGame = GbGateway.GetGame(dGame.Id); var dbGame = CreateDbGameObjectWithoutNavigation(gbGame); dbGame.IsRated = dGame.IsRated; dbGame.Basically = dGame.Basically; dbGame.RatingExplanation = dGame.RatingExplanation; dbGame.RatingLastUpdated = dGame.RatingLastUpdated; dbGame.LastUpdated = DateTime.UtcNow; DbAccess.SaveGame(dbGame); var dbGamePlatforms = CreateDbGamePlatformsListWithoutNavigation(gbGame); DbAccess.SaveGamePlatforms(dbGamePlatforms); if (!includeGenres || gbGame.Genres == null || !gbGame.Genres.Any()) { return; } var dbGameGenres = CreateDbGameGenresListWithoutNavigation(gbGame); DbAccess.SaveGameGenres(dbGameGenres); }
/// <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); } } }
/// <summary> /// Saves a GbGame object to the database, including /// relevant GamePlatform and GameGenre-rows /// </summary> /// <param name="input">The object to save</param> public static void SaveGameToDb(Game input) { var dbGame = CreateDbGameObjectWithoutNavigation(input); var dbGamePlatforms = CreateDbGamePlatformsListWithoutNavigation(input); DbAccess.SaveGame(dbGame); DbAccess.SaveGamePlatforms(dbGamePlatforms); if (input.Genres == null || !input.Genres.Any()) { return; } var dbGameGenres = CreateDbGameGenresListWithoutNavigation(input); DbAccess.SaveGameGenres(dbGameGenres); }