ranking app UD

main
syneffort 2 years ago
parent 2363a001f4
commit 38399bd863
  1. 28
      BlazorApp/RankingApp/Data/Services/RankingService.cs
  2. 29
      BlazorApp/RankingApp/Pages/Ranking.razor

@ -31,7 +31,35 @@ namespace RankingApp.Data.Services
}
// Update
public Task<bool> 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<bool> 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);
}
}
}

@ -20,6 +20,8 @@
<th>User name</th>
<th>Score</th>
<th>Date</th>
<th>Update</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
@ -29,6 +31,16 @@
<td>@gameResult.UserName</td>
<td>@gameResult.Score</td>
<td>@gameResult.Date.ToShortDateString()</td>
<td>
<button class="btn btn-primary" @onclick="(() => UpdateGameResult(gameResult))">
Edit
</button>
</td>
<td>
<button class="btn btn-danger" @onclick="(() => DeleteGameResult(gameResult))">
Delete
</button>
</td>
</tr>
}
</tbody>
@ -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();
}
}

Loading…
Cancel
Save