Initial Commit
This commit is contained in:
312
src/SportsDivision.Web/Controllers/TournamentController.cs
Normal file
312
src/SportsDivision.Web/Controllers/TournamentController.cs
Normal file
@@ -0,0 +1,312 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SportsDivision.Application.DTOs;
|
||||
using SportsDivision.Application.Interfaces;
|
||||
using SportsDivision.Domain.Enums;
|
||||
|
||||
namespace SportsDivision.Web.Controllers;
|
||||
|
||||
[Authorize]
|
||||
public class TournamentController : Controller
|
||||
{
|
||||
private readonly ITournamentService _tournamentService;
|
||||
private readonly IEventService _eventService;
|
||||
|
||||
public TournamentController(ITournamentService tournamentService, IEventService eventService)
|
||||
{
|
||||
_tournamentService = tournamentService;
|
||||
_eventService = eventService;
|
||||
}
|
||||
|
||||
public async Task<IActionResult> Index(TournamentStatus? status, bool showArchived = false)
|
||||
{
|
||||
var tournaments = await _tournamentService.GetAllAsync(includeArchived: showArchived);
|
||||
|
||||
if (status.HasValue)
|
||||
{
|
||||
tournaments = tournaments.Where(t => t.Status == status.Value);
|
||||
}
|
||||
|
||||
ViewBag.SelectedStatus = status;
|
||||
ViewBag.ShowArchived = showArchived;
|
||||
|
||||
return View(tournaments);
|
||||
}
|
||||
|
||||
public async Task<IActionResult> Details(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var tournament = await _tournamentService.GetByIdAsync(id);
|
||||
if (tournament == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var eventLevels = await _tournamentService.GetEventLevelsAsync(id);
|
||||
ViewBag.EventLevels = eventLevels;
|
||||
|
||||
return View(tournament);
|
||||
}
|
||||
catch (KeyNotFoundException)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult Create()
|
||||
{
|
||||
return View(new TournamentCreateDto());
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Create(TournamentCreateDto dto)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return View(dto);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var tournament = await _tournamentService.CreateAsync(dto);
|
||||
TempData["SuccessMessage"] = "Tournament created successfully.";
|
||||
return RedirectToAction(nameof(Details), new { id = tournament.TournamentId });
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
TempData["ErrorMessage"] = ex.Message;
|
||||
return View(dto);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Edit(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var tournament = await _tournamentService.GetByIdAsync(id);
|
||||
if (tournament == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var dto = new TournamentUpdateDto
|
||||
{
|
||||
TournamentId = tournament.TournamentId,
|
||||
Name = tournament.Name,
|
||||
StartDate = tournament.StartDate,
|
||||
EndDate = tournament.EndDate,
|
||||
ZoneId = tournament.ZoneId,
|
||||
SchoolLevel = tournament.SchoolLevel
|
||||
};
|
||||
|
||||
return View(dto);
|
||||
}
|
||||
catch (KeyNotFoundException)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Edit(TournamentUpdateDto dto)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return View(dto);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await _tournamentService.UpdateAsync(dto);
|
||||
TempData["SuccessMessage"] = "Tournament updated successfully.";
|
||||
return RedirectToAction(nameof(Details), new { id = dto.TournamentId });
|
||||
}
|
||||
catch (KeyNotFoundException)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
TempData["ErrorMessage"] = ex.Message;
|
||||
return View(dto);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Delete(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _tournamentService.DeleteAsync(id);
|
||||
TempData["SuccessMessage"] = "Tournament deleted successfully.";
|
||||
}
|
||||
catch (KeyNotFoundException)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
TempData["ErrorMessage"] = ex.Message;
|
||||
}
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> SetupEventLevels(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var tournament = await _tournamentService.GetByIdAsync(id);
|
||||
if (tournament == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var eventLevels = await _tournamentService.GetEventLevelsAsync(id);
|
||||
var events = await _eventService.GetAllAsync();
|
||||
|
||||
ViewBag.Tournament = tournament;
|
||||
ViewBag.Events = events;
|
||||
ViewBag.EventLevels = eventLevels;
|
||||
|
||||
return View(eventLevels);
|
||||
}
|
||||
catch (KeyNotFoundException)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> AddEventLevel(TournamentEventLevelCreateDto dto)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _tournamentService.AddEventLevelAsync(dto);
|
||||
TempData["SuccessMessage"] = "Event level added to tournament successfully.";
|
||||
}
|
||||
catch (KeyNotFoundException)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
TempData["ErrorMessage"] = ex.Message;
|
||||
}
|
||||
|
||||
return RedirectToAction(nameof(SetupEventLevels), new { id = dto.TournamentId });
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> RemoveEventLevel(int tournamentEventLevelId, int tournamentId)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _tournamentService.RemoveEventLevelAsync(tournamentEventLevelId);
|
||||
TempData["SuccessMessage"] = "Event level removed from tournament successfully.";
|
||||
}
|
||||
catch (KeyNotFoundException)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
TempData["ErrorMessage"] = ex.Message;
|
||||
}
|
||||
|
||||
return RedirectToAction(nameof(SetupEventLevels), new { id = tournamentId });
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> UpdateStatus(int id, TournamentStatus status)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _tournamentService.UpdateStatusAsync(id, status);
|
||||
TempData["SuccessMessage"] = $"Tournament status updated to {status}.";
|
||||
}
|
||||
catch (KeyNotFoundException)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
TempData["ErrorMessage"] = ex.Message;
|
||||
}
|
||||
|
||||
return RedirectToAction(nameof(Details), new { id });
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Archive(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _tournamentService.ArchiveAsync(id);
|
||||
TempData["SuccessMessage"] = "Tournament archived successfully.";
|
||||
}
|
||||
catch (KeyNotFoundException)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
TempData["ErrorMessage"] = ex.Message;
|
||||
}
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Unarchive(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _tournamentService.UnarchiveAsync(id);
|
||||
TempData["SuccessMessage"] = "Tournament unarchived successfully.";
|
||||
}
|
||||
catch (KeyNotFoundException)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
TempData["ErrorMessage"] = ex.Message;
|
||||
}
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> ToggleAgeWaiver(int tournamentEventLevelId, int tournamentId)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _tournamentService.ToggleAgeWaiverAsync(tournamentEventLevelId);
|
||||
TempData["SuccessMessage"] = "Age waiver toggled successfully.";
|
||||
}
|
||||
catch (KeyNotFoundException)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
TempData["ErrorMessage"] = ex.Message;
|
||||
}
|
||||
|
||||
return RedirectToAction(nameof(SetupEventLevels), new { id = tournamentId });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user