using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization; using PlotLine.Services; using PlotLine.ViewModels; namespace PlotLine.Controllers; [Authorize] public sealed class ProjectsController(IProjectService projects, IProjectActivityService activity) : Controller { public async Task Index() => View(await projects.ListAsync()); public async Task Details(int id) { var model = await projects.GetDetailAsync(id); return model is null ? NotFound() : View(model); } public async Task Create() => View("Edit", await projects.GetCreateAsync()); public async Task Activity(int id) { var model = await activity.GetActivityAsync(id); return model is null ? NotFound() : View(model); } public async Task Edit(int id) { var model = await projects.GetEditAsync(id); return model is null ? NotFound() : View(model); } [HttpPost] [ValidateAntiForgeryToken] public async Task Save(ProjectEditViewModel model) { if (!ModelState.IsValid) { if (model.ProjectID == 0) { await projects.PopulateGenrePresetOptionsAsync(model); } return View("Edit", model); } var projectId = await projects.SaveAsync(model); if (projectId == 0) { return NotFound(); } return RedirectToAction(nameof(Details), new { id = projectId }); } [HttpPost] [ValidateAntiForgeryToken] public async Task Archive(int id) { var archived = await projects.ArchiveAsync(id); if (!archived) { return NotFound(); } TempData["ArchiveMessage"] = "Project archived. You can restore it from Archived Projects."; return RedirectToAction(nameof(Index)); } [HttpPost] [ValidateAntiForgeryToken] public async Task Restore(int id) { var restored = await projects.RestoreAsync(id); if (!restored) { return NotFound(); } TempData["ArchiveMessage"] = "Project restored."; return RedirectToAction(nameof(Index)); } [HttpGet] public IActionResult HardDeleteArchivedProject() { TempData["ArchiveError"] = "The project could not be deleted. Please try again."; return RedirectToAction(nameof(Index)); } [HttpPost] [ValidateAntiForgeryToken] public async Task HardDeleteArchivedProject(int projectId, string? confirmationName) { var project = await projects.GetArchivedOwnerProjectAsync(projectId); if (project is null) { TempData["ArchiveError"] = "Only an archived project owner can permanently delete a project."; return RedirectToAction(nameof(Index)); } if (!string.Equals(confirmationName?.Trim(), project.ProjectName, StringComparison.Ordinal)) { TempData["ArchiveError"] = "Type the project name exactly to permanently delete it."; return RedirectToAction(nameof(Index)); } var result = await projects.HardDeleteAsync(projectId); if (result.Succeeded) { TempData["ArchiveMessage"] = "Project permanently deleted."; } else { TempData["ArchiveError"] = result.NotArchived ? "Only archived projects can be permanently deleted." : result.Message; } return RedirectToAction(nameof(Index)); } }