Files
ASP.NET-MVC-Study/BlazorApp/RankingApp/Pages/Ranking.razor

138 lines
4.2 KiB
Plaintext
Raw Normal View History

2023-08-25 13:52:59 +09:00
@page "/ranking"
@using RankingApp.Data.Models;
@using RankingApp.Data.Services;
@inject RankingService _rankingService;
<h3>Ranking</h3>
<AuthorizeView>
<Authorized>
@if (_gameResults == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>User name</th>
<th>Score</th>
<th>Date</th>
2023-08-25 14:19:51 +09:00
<th>Update</th>
<th>Delete</th>
2023-08-25 13:52:59 +09:00
</tr>
</thead>
<tbody>
@foreach (var gameResult in _gameResults)
{
<tr>
<td>@gameResult.UserName</td>
<td>@gameResult.Score</td>
<td>@gameResult.Date.ToShortDateString()</td>
2023-08-25 14:19:51 +09:00
<td>
<button class="btn btn-primary" @onclick="(() => UpdateGameResult(gameResult))">
Edit
</button>
</td>
<td>
<button class="btn btn-danger" @onclick="(() => DeleteGameResult(gameResult))">
Delete
</button>
</td>
2023-08-25 13:52:59 +09:00
</tr>
}
</tbody>
</table>
<p>
<button class="btn btn-primary" @onclick="AddGameResult">
Add Result
</button>
</p>
@if (_showPopup)
{
<div class="modal" style="display:block" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title">Add/Update Game Result</h3>
<button type="button" class="btn btn-close" @onclick="ClosePopup"/>
</div>
<div class="modal-body">
<label for="UserName">User name</label>
<input class="form-control" type="text" placeholder="user name" @bind-value="_gameResult.UserName"/>
<label for="Score">Score</label>
<input class="form-control" type="text" placeholder="score" @bind-value="_gameResult.Score" />
<button class="btn btn-primary" @onclick="SaveGameResult">
Save
</button>
</div>
</div>
</div>
</div>
}
}
</Authorized>
<NotAuthorized>
<p>You have no authorization to access.</p>
</NotAuthorized>
</AuthorizeView>
@code {
List<GameResult> _gameResults;
bool _showPopup;
GameResult _gameResult;
protected async override Task OnInitializedAsync()
{
await base.OnInitializedAsync();
_gameResults = await _rankingService.GetGameResultsAsync();
}
void AddGameResult()
{
_showPopup = true;
_gameResult = new GameResult() { Id = 0 };
}
2023-08-25 14:19:51 +09:00
async Task UpdateGameResult(GameResult gameResult)
{
_gameResult = gameResult;
_showPopup = true;
}
async Task DeleteGameResult(GameResult gameResult)
{
var result = _rankingService.DeleteGameResult(gameResult);
_gameResults = await _rankingService.GetGameResultsAsync();
}
2023-08-25 13:52:59 +09:00
void ClosePopup()
{
_showPopup = false;
}
async Task SaveGameResult()
{
2023-08-25 14:19:51 +09:00
ClosePopup();
2023-08-25 13:52:59 +09:00
if (_gameResult.Id == 0)
{
_gameResult.Date = DateTime.Now;
var result = _rankingService.AddGameResult(_gameResult);
}
else
{
2023-08-25 14:19:51 +09:00
var result = _rankingService.UpdateGameResult(_gameResult);
2023-08-25 13:52:59 +09:00
}
_gameResults = await _rankingService.GetGameResultsAsync();
}
}