211 lines
7.1 KiB
C#
211 lines
7.1 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using PlotLine.Services;
|
|
using PlotLine.ViewModels;
|
|
|
|
namespace PlotLine.Controllers;
|
|
|
|
[Authorize]
|
|
public sealed class StoryAssetsController(IAssetService assets) : Controller
|
|
{
|
|
public async Task<IActionResult> Index(int projectId)
|
|
{
|
|
var model = await assets.GetAssetsAsync(projectId);
|
|
return model is null ? NotFound() : View(model);
|
|
}
|
|
|
|
public async Task<IActionResult> Create(int projectId)
|
|
{
|
|
var model = await assets.GetCreateAssetAsync(projectId);
|
|
return model is null ? NotFound() : View("Edit", model);
|
|
}
|
|
|
|
public async Task<IActionResult> Edit(int id)
|
|
{
|
|
var model = await assets.GetEditAssetAsync(id);
|
|
return model is null ? NotFound() : View(model);
|
|
}
|
|
|
|
public async Task<IActionResult> Details(int id)
|
|
{
|
|
var model = await assets.GetAssetDetailAsync(id);
|
|
return model is null ? NotFound() : View(model);
|
|
}
|
|
|
|
public async Task<IActionResult> Merge(int id)
|
|
{
|
|
var model = await assets.GetMergeAsync(id);
|
|
return model is null ? NotFound() : View(model);
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Merge([Bind(Prefix = "Form")] EntityMergeForm form)
|
|
{
|
|
if (ModelState.IsValid)
|
|
{
|
|
try
|
|
{
|
|
var result = await assets.MergeAsync(form);
|
|
if (result is null) return NotFound();
|
|
TempData["AssetMessage"] = $"{result.SourceName} merged into {result.TargetName}.";
|
|
return RedirectToAction(nameof(Details), new { id = result.TargetID });
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
ModelState.AddModelError(string.Empty, ex.Message);
|
|
}
|
|
}
|
|
var model = await assets.GetMergeAsync(form.SourceID);
|
|
if (model is null) return NotFound();
|
|
model.Form = form;
|
|
return View(model);
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Save(StoryAssetEditViewModel model)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
var invalidModel = await RehydrateEditModel(model);
|
|
return invalidModel is null ? NotFound() : View("Edit", invalidModel);
|
|
}
|
|
|
|
int assetId;
|
|
try
|
|
{
|
|
assetId = await assets.SaveAssetAsync(model);
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
ModelState.AddModelError(string.Empty, ex.Message);
|
|
var editModel = await RehydrateEditModel(model);
|
|
return editModel is null ? NotFound() : View("Edit", editModel);
|
|
}
|
|
|
|
return RedirectToAction(nameof(Details), new { id = assetId });
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
[RequestSizeLimit(11L * 1024 * 1024)]
|
|
public async Task<IActionResult> SaveImage(int id, string operation, IFormFile? upload)
|
|
{
|
|
if (!ModelState.IsValid) return BadRequest();
|
|
var result = await assets.SaveAssetImageAsync(id, operation, upload);
|
|
if (result is null) return NotFound();
|
|
|
|
if (result.Succeeded)
|
|
{
|
|
TempData["AssetImageSaved"] = operation == "remove" ? "Asset image removed." : "Asset image saved.";
|
|
}
|
|
else
|
|
{
|
|
TempData["ImageError"] = result.ErrorMessage;
|
|
}
|
|
|
|
return RedirectToAction(nameof(Details), new { id, tab = "reference" });
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Archive(int id, int projectId)
|
|
{
|
|
await assets.ArchiveAssetAsync(id);
|
|
TempData["ArchiveMessage"] = "Asset archived. You can restore it from Archived Items.";
|
|
return RedirectToAction(nameof(Index), new { projectId });
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> AddDependency(AssetDependencyEditViewModel model, int returnAssetId)
|
|
{
|
|
if (!ModelState.IsValid) return BadRequest();
|
|
|
|
if (model.SourceAssetID == model.TargetAssetID)
|
|
{
|
|
TempData["AssetMessage"] = "Choose two different assets.";
|
|
}
|
|
else
|
|
{
|
|
model.AssetDependencyID = 0;
|
|
await assets.SaveDependencyAsync(model);
|
|
}
|
|
|
|
return RedirectToAction(nameof(Details), new { id = returnAssetId, tab = "dependencies" });
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> EditDependency(int id, int returnAssetId,
|
|
[Bind(nameof(AssetDependencyEditViewModel.AssetDependencyTypeID),
|
|
nameof(AssetDependencyEditViewModel.SourceRequiredStateID),
|
|
nameof(AssetDependencyEditViewModel.TargetBlockedStateID),
|
|
nameof(AssetDependencyEditViewModel.Description))] AssetDependencyEditViewModel model)
|
|
{
|
|
if (!ModelState.IsValid) return BadRequest();
|
|
|
|
var detail = await assets.GetAssetDetailAsync(returnAssetId);
|
|
var dependency = detail?.Dependencies.SingleOrDefault(item => item.AssetDependencyID == id);
|
|
if (dependency is null) return NotFound();
|
|
|
|
if (dependency.SourceAssetID == dependency.TargetAssetID)
|
|
{
|
|
TempData["AssetMessage"] = "An asset cannot depend on itself. Remove this dependency to replace it.";
|
|
}
|
|
else
|
|
{
|
|
model.AssetDependencyID = id;
|
|
model.SourceAssetID = dependency.SourceAssetID;
|
|
model.TargetAssetID = dependency.TargetAssetID;
|
|
if (await assets.SaveDependencyAsync(model) == 0) return NotFound();
|
|
}
|
|
|
|
return RedirectToAction(nameof(Details), new { id = returnAssetId, tab = "dependencies" });
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> DropDependency(AssetDependencyEditViewModel model)
|
|
{
|
|
if (model.SourceAssetID != model.TargetAssetID)
|
|
{
|
|
model.AssetDependencyID = 0;
|
|
await assets.SaveDependencyAsync(model);
|
|
}
|
|
|
|
return RedirectToAction(nameof(Index), new { projectId = model.ProjectID });
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> DeleteDependency(int id, int returnAssetId)
|
|
{
|
|
await assets.DeleteDependencyAsync(id);
|
|
return RedirectToAction(nameof(Details), new { id = returnAssetId, tab = "dependencies" });
|
|
}
|
|
|
|
private async Task<StoryAssetEditViewModel?> RehydrateEditModel(StoryAssetEditViewModel posted)
|
|
{
|
|
var model = posted.StoryAssetID == 0
|
|
? await assets.GetCreateAssetAsync(posted.ProjectID)
|
|
: await assets.GetEditAssetAsync(posted.StoryAssetID);
|
|
if (model is null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
model.AssetName = posted.AssetName;
|
|
model.Aliases = posted.Aliases;
|
|
model.AssetKindID = posted.AssetKindID;
|
|
model.Description = posted.Description;
|
|
model.Importance = posted.Importance;
|
|
model.CurrentStateID = posted.CurrentStateID;
|
|
model.CurrentLocationID = posted.CurrentLocationID;
|
|
model.IsResolved = posted.IsResolved;
|
|
model.ShowInQuickAddBar = posted.ShowInQuickAddBar;
|
|
return model;
|
|
}
|
|
}
|