private static void DeleteGame(GameSaveDbContext context, Game gameContent) { foreach (var gameSave in context.GameSave) { Console.WriteLine($"{gameSave.GameSaveId} {gameSave.SaveName}"); } bool stateOfValue; int userInput; do { Console.Write("Select Game Number: "); string input2 = Console.ReadLine(); stateOfValue = int.TryParse(input2, out userInput); if (!stateOfValue) { Console.WriteLine("Error: " + input2 + " is not a valid number"); } } while (!stateOfValue); GameSave gameDelete = new GameSave(); { gameDelete.GameSaveId = userInput; } context.GameSave.Remove(gameDelete); context.SaveChanges(); }
private static void OverWriteGame(GameSaveDbContext context, Game gameContent) { var overWriitenGame = new GameSave(); { overWriitenGame.SaveName = gameContent.SaveName; overWriitenGame.BoardHeight = gameContent.BoardHeight; overWriitenGame.BoardWidth = gameContent.BoardWidth; overWriitenGame.NumberOfMines = gameContent.NumberOfMines; overWriitenGame.BoardState = JsonConvert.SerializeObject(gameContent.Board); } context.GameSave.Update(overWriitenGame); context.SaveChanges(); }
private static void SaveGameToDb(Game gameContent, GameSaveDbContext context) { bool savedSuccessfully = true; string saveName; do { Console.Write("Enter name of Save: "); saveName = Console.ReadLine()?.Trim() ?? ""; if (saveName == "") { Console.WriteLine("Enter a file Name! File Name cannot be empty"); } } while (saveName == ""); gameContent.SaveName = saveName; foreach (var gameSave in context.GameSave) { if (gameContent.SaveName == gameSave.SaveName) { savedSuccessfully = false; Console.WriteLine($" \"{gameContent.SaveName}\" already exists!"); Console.WriteLine( "O to OverWrite Existing Game|| R to Rename Game Name|| D(␡) to delete a Game"); ConsoleKeyInfo command; do { command = Console.ReadKey(true); switch (command.Key) { case ConsoleKey.O: OverWriteGame(context, gameContent); break; case ConsoleKey.R: //RenameGame(); break; case ConsoleKey.D: DeleteGame(context, gameContent); break; default: Console.WriteLine("Invalid Input"); break; } } while (!(command.Key == ConsoleKey.O || command.Key == ConsoleKey.R || command.Key == ConsoleKey.Delete)); } } if (savedSuccessfully) { context.GameSave.Add(new GameSave() { SaveName = gameContent.SaveName, BoardHeight = gameContent.BoardHeight, BoardWidth = gameContent.BoardWidth, NumberOfMines = gameContent.NumberOfMines, BoardState = JsonConvert.SerializeObject(gameContent.Board) }); context.SaveChanges(); } }