From 38399bd863c36b662a72927c5b9e503cce5db6e6 Mon Sep 17 00:00:00 2001 From: syneffort Date: Fri, 25 Aug 2023 14:19:51 +0900 Subject: [PATCH] ranking app UD --- .../Data/Services/RankingService.cs | 28 ++++++++++++++++++ BlazorApp/RankingApp/Pages/Ranking.razor | 29 +++++++++++++++++-- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/BlazorApp/RankingApp/Data/Services/RankingService.cs b/BlazorApp/RankingApp/Data/Services/RankingService.cs index 09d06ac..ea4714d 100644 --- a/BlazorApp/RankingApp/Data/Services/RankingService.cs +++ b/BlazorApp/RankingApp/Data/Services/RankingService.cs @@ -31,7 +31,35 @@ namespace RankingApp.Data.Services } // Update + public Task UpdateGameResult(GameResult gameResult) + { + var findResult = _context.GameResults + .Where(x => x.Id == gameResult.Id) + .FirstOrDefault(); + if (findResult == null) + return Task.FromResult(false); + + findResult.UserName = gameResult.UserName; + findResult.Score = gameResult.Score; + findResult.Date = DateTime.Now; + _context.SaveChanges(); + + return Task.FromResult(true); + } // Delete + public Task DeleteGameResult(GameResult gameResult) + { + var findResult = _context.GameResults + .Where(x => x.Id == gameResult.Id) + .FirstOrDefault(); + if (findResult == null) + return Task.FromResult(false); + + _context.GameResults.Remove(findResult); + _context.SaveChanges(); + + return Task.FromResult(true); + } } } diff --git a/BlazorApp/RankingApp/Pages/Ranking.razor b/BlazorApp/RankingApp/Pages/Ranking.razor index 41381f4..2c49ea4 100644 --- a/BlazorApp/RankingApp/Pages/Ranking.razor +++ b/BlazorApp/RankingApp/Pages/Ranking.razor @@ -20,6 +20,8 @@ User name Score Date + Update + Delete @@ -29,6 +31,16 @@ @gameResult.UserName @gameResult.Score @gameResult.Date.ToShortDateString() + + + + + + } @@ -89,6 +101,19 @@ _gameResult = new GameResult() { Id = 0 }; } + async Task UpdateGameResult(GameResult gameResult) + { + _gameResult = gameResult; + _showPopup = true; + } + + async Task DeleteGameResult(GameResult gameResult) + { + var result = _rankingService.DeleteGameResult(gameResult); + + _gameResults = await _rankingService.GetGameResultsAsync(); + } + void ClosePopup() { _showPopup = false; @@ -96,6 +121,7 @@ async Task SaveGameResult() { + ClosePopup(); if (_gameResult.Id == 0) { _gameResult.Date = DateTime.Now; @@ -103,10 +129,9 @@ } else { - + var result = _rankingService.UpdateGameResult(_gameResult); } _gameResults = await _rankingService.GetGameResultsAsync(); - ClosePopup(); } }