189 lines
6.8 KiB
C#
189 lines
6.8 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using PlotLine.Services;
|
|
using PlotLine.ViewModels;
|
|
|
|
namespace PlotLine.Controllers;
|
|
|
|
[Authorize]
|
|
public sealed class LocationsController(ILocationService locations) : Controller
|
|
{
|
|
public async Task<IActionResult> Index(int projectId)
|
|
{
|
|
var model = await locations.GetLocationsAsync(projectId);
|
|
return model is null ? NotFound() : View(model);
|
|
}
|
|
|
|
public async Task<IActionResult> Create(int projectId)
|
|
{
|
|
var model = await locations.GetCreateAsync(projectId);
|
|
return model is null ? NotFound() : View("Edit", model);
|
|
}
|
|
|
|
public async Task<IActionResult> Edit(int id)
|
|
{
|
|
var model = await locations.GetEditAsync(id);
|
|
return model is null ? NotFound() : View(model);
|
|
}
|
|
|
|
public async Task<IActionResult> Details(int id)
|
|
{
|
|
var model = await locations.GetDetailAsync(id);
|
|
return model is null ? NotFound() : View(model);
|
|
}
|
|
|
|
public async Task<IActionResult> Merge(int id)
|
|
{
|
|
var model = await locations.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 locations.MergeAsync(form);
|
|
if (result is null) return NotFound();
|
|
TempData["LocationMessage"] = $"{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 locations.GetMergeAsync(form.SourceID);
|
|
if (model is null) return NotFound();
|
|
model.Form = form;
|
|
return View(model);
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Reparent(int projectId, int locationId, int parentLocationId, int? expectedParentLocationId)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
if (IsAjaxRequest()) return BadRequest(new { error = "Invalid location selection. Please try again." });
|
|
TempData["LocationError"] = "Invalid location selection. Please try again.";
|
|
return RedirectToAction(nameof(Index), new { projectId });
|
|
}
|
|
try
|
|
{
|
|
await locations.ReparentAsync(projectId, locationId, parentLocationId, expectedParentLocationId);
|
|
if (IsAjaxRequest())
|
|
{
|
|
var model = await locations.GetLocationsAsync(projectId);
|
|
return model is null ? NotFound() : PartialView("_LocationTree", model);
|
|
}
|
|
TempData["LocationMessage"] = "Location moved to its new parent.";
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
if (IsAjaxRequest()) return BadRequest(new { error = ex.Message });
|
|
TempData["LocationError"] = ex.Message;
|
|
}
|
|
return RedirectToAction(nameof(Index), new { projectId });
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Save(LocationEditViewModel model)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
var editModel = model.LocationID == 0
|
|
? await locations.GetCreateAsync(model.ProjectID)
|
|
: await locations.GetEditAsync(model.LocationID);
|
|
if (editModel is null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
editModel.ParentLocationID = model.ParentLocationID;
|
|
editModel.LocationName = model.LocationName;
|
|
editModel.Aliases = model.Aliases;
|
|
editModel.LocationTypeID = model.LocationTypeID;
|
|
editModel.Description = model.Description;
|
|
editModel.ShowInQuickAddBar = model.ShowInQuickAddBar;
|
|
editModel.ExcludeFromCompanionDetection = model.ExcludeFromCompanionDetection;
|
|
editModel.DetectionPriority = model.DetectionPriority;
|
|
return View("Edit", editModel);
|
|
}
|
|
|
|
var locationId = await locations.SaveAsync(model);
|
|
return RedirectToAction(nameof(Details), new { id = locationId });
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> SetPreferredName(int id, string preferredName)
|
|
{
|
|
try
|
|
{
|
|
await locations.SetPreferredNameAsync(id, preferredName);
|
|
TempData["LocationMessage"] = $"Preferred name set to {preferredName}.";
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
TempData["LocationMessage"] = ex.Message;
|
|
}
|
|
|
|
return RedirectToAction(nameof(Details), new { id });
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Archive(int id, int projectId)
|
|
{
|
|
await locations.ArchiveAsync(id);
|
|
TempData["ArchiveMessage"] = "Location archived. You can restore it from Archived Items.";
|
|
return RedirectToAction(nameof(Index), new { projectId });
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> AddRelationship(LocationRelationshipEditViewModel model)
|
|
{
|
|
await locations.SaveRelationshipAsync(model);
|
|
return RedirectToAction(nameof(Details), new { id = model.FromLocationID });
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> DropRelationship(LocationRelationshipEditViewModel model)
|
|
{
|
|
if (!ModelState.IsValid || model.FromLocationID == model.ToLocationID)
|
|
{
|
|
if (IsAjaxRequest()) return BadRequest(new { error = "Choose two different locations and a relationship type." });
|
|
return RedirectToAction(nameof(Index), new { projectId = model.ProjectID });
|
|
}
|
|
try
|
|
{
|
|
await locations.SaveRelationshipAsync(model);
|
|
if (IsAjaxRequest()) return Json(new { message = "Location relationship saved." });
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
if (IsAjaxRequest()) return BadRequest(new { error = ex.Message });
|
|
TempData["LocationError"] = ex.Message;
|
|
}
|
|
return RedirectToAction(nameof(Index), new { projectId = model.ProjectID });
|
|
}
|
|
|
|
private bool IsAjaxRequest() => Request.Headers["X-Requested-With"] == "XMLHttpRequest";
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> ArchiveRelationship(int id, int returnLocationId)
|
|
{
|
|
await locations.ArchiveRelationshipAsync(id);
|
|
TempData["ArchiveMessage"] = "Location relationship archived.";
|
|
return RedirectToAction(nameof(Details), new { id = returnLocationId });
|
|
}
|
|
}
|