Files
website/Controllers/MembreController.cs
T

142 lines
4.8 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Phenix.Services;
namespace Phenix.Controllers;
[Route("/membre")]
public class MembreController : Controller
{
private readonly IGameService _games;
private readonly LoginService _login;
private const string SessionCookie = "phenix-session";
private const string SessionSignCookie = "phenix-session-sign";
public MembreController(IGameService games, LoginService login)
{
_games = games;
_login = login;
}
[HttpGet("")]
public IActionResult Index()
{
ViewBag.IsLoggedIn = IsLoggedIn();
return View();
}
[HttpGet("/login")]
public IActionResult Login(string? username, string? password)
{
if (username is null || password is null || !_login.ValidateCredentials(username, password))
return StatusCode(403);
var expiresAt = DateTimeOffset.UtcNow.AddDays(90).ToUnixTimeSeconds();
Response.Cookies.Append(SessionCookie, expiresAt.ToString(), new CookieOptions
{
MaxAge = TimeSpan.FromDays(90),
});
Response.Cookies.Append(SessionSignCookie, _login.SignSession(expiresAt), new CookieOptions
{
MaxAge = TimeSpan.FromDays(90),
});
return Redirect("/membre/games-editor/today");
}
[HttpGet("games-editor/today")]
public async Task<IActionResult> GamesEditorToday()
{
if (!IsLoggedIn()) return Redirect("/membre");
var games = await _games.GetAllGames();
var today = DateOnly.FromDateTime(TimeZoneInfo.ConvertTime(
DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById("America/New_York")));
var lastGameDay = games
.Where(g => DateOnly.Parse(g.Date) <= today)
.Select(g => g.Date)
.OrderByDescending(d => d)
.FirstOrDefault();
ViewBag.SelectedYear = (int?)null;
ViewBag.IsToday = true;
ViewBag.Years = games.Select(g => g.Year).Distinct().OrderByDescending(y => y).ToList();
ViewBag.Games = games.Where(g => g.Date == lastGameDay).ToList();
return PartialView("GamesEditor");
}
[HttpGet("games-editor/{year:int}")]
public async Task<IActionResult> GamesEditorByYear(int year)
{
if (!IsLoggedIn()) return Redirect("/membre");
var games = await _games.GetAllGames();
ViewBag.SelectedYear = year;
ViewBag.IsToday = false;
ViewBag.Years = games.Select(g => g.Year).Distinct().OrderByDescending(y => y).ToList();
ViewBag.Games = games.Where(g => g.Year == year).ToList();
return PartialView("GamesEditor");
}
[HttpGet("new-game-editor")]
public IActionResult NewGameEditor()
{
if (!IsLoggedIn()) return Redirect("/membre");
return PartialView();
}
[HttpPost("new-game")]
public async Task<IActionResult> NewGame(string gameDate, string gameTime, string homeTeam, string awayTeam)
{
if (!IsLoggedIn()) return Redirect("/membre");
var (game, error) = await _games.CreateNewGame(gameDate, gameTime, homeTeam, awayTeam);
if (error != "")
{
ViewBag.Error = error;
return StatusCode(500, PartialView("NewGameError"));
}
Response.Headers["game-id"] = game!.Id;
return PartialView("SingleGameEditor", game);
}
[HttpDelete("games/{gameId}")]
public async Task<IActionResult> DeleteGame(string gameId)
{
if (!IsLoggedIn()) return Redirect("/membre");
var ok = await _games.DeleteGameById(gameId);
return Content(ok ? "true" : "false");
}
[HttpPost("games/{gameId}")]
public async Task<IActionResult> UpdateGame(string gameId, int? awayScore, int? homeScore, string? gameType)
{
if (!IsLoggedIn()) return Redirect("/membre");
var success = await _games.UpdateGame(gameId, homeScore, awayScore,
rain: gameType == "rain", practice: gameType == "practice");
return Content(success
? "<i class=\"fa fa-floppy-o\" aria-hidden=\"true\"></i>"
: "<i class=\"fa fa-exclamation-triangle\" aria-hidden=\"true\"></i>");
}
[HttpPost("games/{gameId}/switch")]
public async Task<IActionResult> SwitchVisitors(string gameId)
{
if (!IsLoggedIn()) return Redirect("/membre");
var success = await _games.SwitchVisitors(gameId);
if (!success)
return Content("<i class=\"fa fa-exclamation-triangle\" aria-hidden=\"true\"></i>");
var game = await _games.GetGameById(gameId);
Response.Headers["game-id"] = gameId;
return PartialView("SingleGameEditor", game);
}
private bool IsLoggedIn()
{
Request.Cookies.TryGetValue(SessionCookie, out var expires);
Request.Cookies.TryGetValue(SessionSignCookie, out var sig);
return _login.VerifySession(expires, sig);
}
}