PlotDirector/PlotLine/Controllers/CharactersController.cs
Nick Beckley 34a8d94cb4 Implemented the Visual Identity infrastructure pass.
Changed
Added image fields for Characters and Story Assets in models, view models, repositories, services, and save flows.
Added reusable avatar/initials rendering via [AvatarHelper.cs](C:/Source/PlotLine/PlotLine/Services/AvatarHelper.cs) and [_Avatar.cshtml](C:/Source/PlotLine/PlotLine/Views/Shared/_Avatar.cshtml).
Added upload/remove processing through existing upload storage in [VisualIdentityImageService.cs](C:/Source/PlotLine/PlotLine/Services/VisualIdentityImageService.cs).
Updated Character and Story Asset list/detail/edit views with thumbnails, initials fallback, upload, replace, and remove controls.
Added styling in [site.css](C:/Source/PlotLine/PlotLine/wwwroot/css/site.css) and refreshed site.min.css.
Database
Added new migration [079_VisualIdentityCharacterAssetImages.sql](C:/Source/PlotLine/PlotLine/Sql/079_VisualIdentityCharacterAssetImages.sql).
Adds nullable ImagePath and ThumbnailPath to Characters and StoryAssets.
Applied locally and confirmed the columns exist.
Stored Procedures
Updated active procedure definitions only:
Character_ListByProject
Character_Get
Character_Save
Character_UpdateImage
StoryAsset_ListByProject
StoryAsset_Get
StoryAsset_Save
StoryAsset_UpdateImage
2026-06-22 10:10:39 +01:00

130 lines
4.9 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using PlotLine.Services;
using PlotLine.ViewModels;
namespace PlotLine.Controllers;
[Authorize]
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)
{
model.Project ??= (await characters.GetCreateCharacterAsync(model.ProjectID))?.Project;
return View("Edit", model);
}
int characterId;
try
{
characterId = await characters.SaveCharacterAsync(model);
}
catch (SubscriptionLimitException ex) when (model.CharacterID == 0)
{
ModelState.AddModelError(string.Empty, ex.Message);
var createModel = await characters.GetCreateCharacterAsync(model.ProjectID);
if (createModel is null)
{
return NotFound();
}
createModel.CharacterName = model.CharacterName;
createModel.ShortName = model.ShortName;
createModel.Sex = model.Sex;
createModel.BirthDate = model.BirthDate;
createModel.AgeAtSeriesStart = model.AgeAtSeriesStart;
createModel.Height = model.Height;
createModel.EyeColour = model.EyeColour;
createModel.CharacterImportance = model.CharacterImportance;
createModel.ShowInQuickAddBar = model.ShowInQuickAddBar;
createModel.DefaultDescription = model.DefaultDescription;
return View("Edit", createModel);
}
catch (EntityImageUploadException ex)
{
TempData["ImageError"] = ex.Message;
return RedirectToAction(nameof(Edit), new { id = ex.EntityId });
}
catch (InvalidOperationException ex)
{
ModelState.AddModelError(string.Empty, ex.Message);
var editModel = model.CharacterID == 0
? await characters.GetCreateCharacterAsync(model.ProjectID)
: await characters.GetEditCharacterAsync(model.CharacterID);
if (editModel is null)
{
return NotFound();
}
editModel.CharacterName = model.CharacterName;
editModel.ShortName = model.ShortName;
editModel.Sex = model.Sex;
editModel.BirthDate = model.BirthDate;
editModel.AgeAtSeriesStart = model.AgeAtSeriesStart;
editModel.Height = model.Height;
editModel.EyeColour = model.EyeColour;
editModel.CharacterImportance = model.CharacterImportance;
editModel.ShowInQuickAddBar = model.ShowInQuickAddBar;
editModel.DefaultDescription = model.DefaultDescription;
return View("Edit", editModel);
}
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 });
}
}