From c66a6bc76b2d5555d62b956117d226b8c5aee528 Mon Sep 17 00:00:00 2001 From: Turner Bell Date: Sun, 19 May 2024 11:56:56 -0400 Subject: [PATCH] add fix to put method for games --- src/Controllers/SavePointGamesController.cs | 27 +++++---- test/RequestTests.cs | 66 ++++++++++----------- 2 files changed, 50 insertions(+), 43 deletions(-) diff --git a/src/Controllers/SavePointGamesController.cs b/src/Controllers/SavePointGamesController.cs index 638374d..4e70fa5 100644 --- a/src/Controllers/SavePointGamesController.cs +++ b/src/Controllers/SavePointGamesController.cs @@ -54,22 +54,29 @@ public async Task PutSavePointGame(int id, SavePointGameDTO saveP return BadRequest(); } - _context.Entry(savePointGameDTO).State = EntityState.Modified; + var savePointGame = await _context.SavePointGames.FindAsync(id); + if (savePointGame == null) + { + return NotFound(); + } + + savePointGame.GameName = savePointGameDTO.GameName; + savePointGame.GameConsole = savePointGameDTO.GameConsole; + savePointGame.GameGenre = savePointGameDTO.GameGenre; + savePointGame.GameDeveloper = savePointGameDTO.GameDeveloper; + savePointGame.GamePublisher = savePointGameDTO.GamePublisher; + savePointGame.GameReleaseDate = savePointGameDTO.GameReleaseDate; + savePointGame.GameDescription = savePointGameDTO.GameDescription; + savePointGame.GameRating = savePointGameDTO.GameRating; + savePointGame.GameImage = savePointGameDTO.GameImage; try { await _context.SaveChangesAsync(); } - catch (DbUpdateConcurrencyException) + catch (DbUpdateConcurrencyException) when (!SavePointGameExists(id)) { - if (!SavePointGameExists(id)) - { - return NotFound(); - } - else - { - throw; - } + return NotFound(); } return NoContent(); diff --git a/test/RequestTests.cs b/test/RequestTests.cs index 1dbf758..db59153 100644 --- a/test/RequestTests.cs +++ b/test/RequestTests.cs @@ -66,39 +66,39 @@ public async void TestGetGameById() } // TOOD: Fix this test - // [Fact] - // public async void TestPutGame() - // { - // // Arrange - // var options = new DbContextOptionsBuilder() - // .UseInMemoryDatabase(databaseName: "SavePointGames") - // .Options; - // var context = new SavePointContext(options); - // var controller = new SavePointGamesController(context); - - // // Mocking the SavePointGameDTO object - // var savePointGameDTO = new SavePointGameDTO - // { - // SavePointGameId = 1, - // GameName = "Test Game", - // GameGenre = "Test Genre", - // GameConsole = "Test Platform" - // }; - // await controller.PostSavePointGame(savePointGameDTO); - - // savePointGameDTO = new SavePointGameDTO - // { - // SavePointGameId = 1, - // GameName = "Test Game Updated", - // GameGenre = "Test Genre Updated", - // GameConsole = "Test Platform Updated" - // }; - // // Act - // var result = await controller.PutSavePointGame(1, savePointGameDTO); - - // // Assert - // Assert.IsType(result); - // } + [Fact] + public async void TestPutGame() + { + // Arrange + var options = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName: "SavePointGames") + .Options; + var context = new SavePointContext(options); + var controller = new SavePointGamesController(context); + + // Mocking the SavePointGameDTO object + var savePointGameDTO = new SavePointGameDTO + { + SavePointGameId = 1, + GameName = "Test Game", + GameGenre = "Test Genre", + GameConsole = "Test Platform" + }; + await controller.PostSavePointGame(savePointGameDTO); + + savePointGameDTO = new SavePointGameDTO + { + SavePointGameId = 1, + GameName = "Test Game Updated", + GameGenre = "Test Genre Updated", + GameConsole = "Test Platform Updated" + }; + // Act + var result = await controller.PutSavePointGame(1, savePointGameDTO); + + // Assert + Assert.IsType(result); + } [Fact] public async void TestPostNote()