73 lines
2.5 KiB
C#
73 lines
2.5 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using PlotLine.Services;
|
|
using PlotLine.ViewModels;
|
|
|
|
namespace PlotLine.Controllers;
|
|
|
|
public sealed class CharactersController(ICharacterService characters) : Controller
|
|
{
|
|
public async Task<IActionResult> Index(int projectId)
|
|
{
|
|
var model = await characters.GetCharactersAsync(projectId);
|
|
return model is null ? NotFound() : View(model);
|
|
}
|
|
|
|
public async Task<IActionResult> Create(int projectId)
|
|
{
|
|
var model = await characters.GetCreateCharacterAsync(projectId);
|
|
return model is null ? NotFound() : View("Edit", model);
|
|
}
|
|
|
|
public async Task<IActionResult> Edit(int id)
|
|
{
|
|
var model = await characters.GetEditCharacterAsync(id);
|
|
return model is null ? NotFound() : View(model);
|
|
}
|
|
|
|
public async Task<IActionResult> Details(int id)
|
|
{
|
|
var model = await characters.GetCharacterDetailAsync(id);
|
|
return model is null ? NotFound() : View(model);
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Save(CharacterEditViewModel model)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return View("Edit", model);
|
|
}
|
|
|
|
var characterId = await characters.SaveCharacterAsync(model);
|
|
return RedirectToAction(nameof(Details), new { id = characterId });
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Archive(int id, int projectId)
|
|
{
|
|
await characters.ArchiveCharacterAsync(id);
|
|
TempData["ArchiveMessage"] = "Character archived. You can restore it from Archived Items.";
|
|
return RedirectToAction(nameof(Index), new { projectId });
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> SaveInitialRelationship(InitialRelationshipEditViewModel model)
|
|
{
|
|
await characters.SaveInitialRelationshipAsync(model);
|
|
TempData["RelationshipMessage"] = model.CharacterRelationshipID == 0 ? "Initial relationship added." : "Initial relationship updated.";
|
|
return RedirectToAction(nameof(Details), new { id = model.ReturnCharacterID ?? model.CharacterID });
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> ArchiveRelationship(int id, int characterId)
|
|
{
|
|
await characters.ArchiveRelationshipAsync(id);
|
|
TempData["ArchiveMessage"] = "Relationship archived. You can restore it from Archived Items.";
|
|
return RedirectToAction(nameof(Details), new { id = characterId });
|
|
}
|
|
}
|