97 lines
5.0 KiB
C#
97 lines
5.0 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using PlotLine.Models;
|
|
using PlotLine.Services;
|
|
using PlotLine.Data;
|
|
using PlotLine.ViewModels;
|
|
|
|
namespace PlotLine.Controllers;
|
|
|
|
[Authorize]
|
|
public sealed class RelationshipMapController(IRelationshipMapService relationshipMap, ICharacterRepository repository, ICharacterService characters) : Controller
|
|
{
|
|
public async Task<IActionResult> Index(int projectId, int? bookId, int? chapterId, int? sceneId, int? focusCharacterId, bool showFullNetwork = false)
|
|
{
|
|
var position = StoryPosition.FromTimelineSelection(projectId, bookId, chapterId, sceneId);
|
|
var model = position is null
|
|
? null
|
|
: await relationshipMap.GetAsync(position, null, true);
|
|
if (model is null) return NotFound();
|
|
model.FocusCharacterID = focusCharacterId;
|
|
model.ShowFullNetwork = showFullNetwork;
|
|
foreach (var node in model.Nodes) node.IsFocus = node.CharacterID == focusCharacterId;
|
|
return View(model);
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<IActionResult> State(int projectId, int? bookId, int? sceneId)
|
|
{
|
|
var model = await relationshipMap.GetAsync(projectId, bookId, null, sceneId, null, true);
|
|
return model is null ? NotFound() : Json(model);
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Save(WorkspaceRelationshipEdit model)
|
|
{
|
|
if (!ModelState.IsValid) return BadRequest("Check the relationship fields.");
|
|
var data = await repository.GetRelationshipMapAsync(model.ProjectID);
|
|
var lookups = await repository.GetLookupsAsync(model.ProjectID);
|
|
if (model.SourceID == model.TargetID
|
|
|| !data.Characters.Any(x => x.CharacterID == model.SourceID)
|
|
|| !data.Characters.Any(x => x.CharacterID == model.TargetID)
|
|
|| !lookups.RelationshipTypes.Any(x => x.RelationshipTypeID == model.TypeID)
|
|
|| !lookups.RelationshipStates.Any(x => x.RelationshipStateID == model.StateID)
|
|
|| (model.SceneID.HasValue && !data.Scenes.Any(x => x.SceneID == model.SceneID)))
|
|
return BadRequest("Choose valid characters, type, state and scene from this project.");
|
|
var matches = data.Relationships.Where(x => !x.IsArchived && x.RelationshipTypeID == model.TypeID
|
|
&& ((x.CharacterAID == model.SourceID && x.CharacterBID == model.TargetID)
|
|
|| (x.IsReciprocal && x.CharacterAID == model.TargetID && x.CharacterBID == model.SourceID))).ToList();
|
|
if (matches.Count > 1) return Conflict("Multiple matching relationships exist. Review them in the character details before editing.");
|
|
var existing = matches.SingleOrDefault();
|
|
if (model.SceneID.HasValue)
|
|
{
|
|
var previous = data.RelationshipEvents.Where(x => x.CharacterRelationshipID == existing?.CharacterRelationshipID && x.SceneID == model.SceneID)
|
|
.OrderByDescending(x => x.RelationshipEventID).FirstOrDefault();
|
|
var change = new RelationshipEventCreateViewModel
|
|
{
|
|
RelationshipEventID = previous?.RelationshipEventID ?? 0,
|
|
CharacterRelationshipID = existing?.CharacterRelationshipID,
|
|
SceneID = model.SceneID.Value, CharacterAID = model.SourceID, CharacterBID = model.TargetID,
|
|
RelationshipTypeID = model.TypeID, RelationshipStateID = model.StateID, Intensity = model.Intensity,
|
|
IsReciprocal = model.Mutual, Description = model.Notes, IsKnownToOtherCharacter = model.Known, ReturnProjectID = model.ProjectID
|
|
};
|
|
if (previous is null) await characters.AddRelationshipEventAsync(change);
|
|
else await characters.UpdateRelationshipEventAsync(change);
|
|
}
|
|
else
|
|
{
|
|
await characters.SaveInitialRelationshipAsync(new InitialRelationshipEditViewModel
|
|
{
|
|
CharacterRelationshipID = existing?.CharacterRelationshipID ?? 0,
|
|
ProjectID = model.ProjectID, CharacterID = existing?.CharacterAID ?? model.SourceID,
|
|
RelatedCharacterID = existing?.CharacterBID ?? model.TargetID, RelationshipTypeID = model.TypeID,
|
|
InitialBookID = existing?.InitialBookID, InitialRelationshipStateID = model.StateID,
|
|
InitialIntensity = model.Intensity, IsReciprocal = model.Mutual, ReaderInitiallyKnows = model.Known, Notes = model.Notes
|
|
});
|
|
}
|
|
return Ok();
|
|
}
|
|
}
|
|
|
|
public sealed class WorkspaceRelationshipEdit
|
|
{
|
|
public int ProjectID { get; set; }
|
|
public int SourceID { get; set; }
|
|
public int TargetID { get; set; }
|
|
public int TypeID { get; set; }
|
|
public int StateID { get; set; }
|
|
public int? SceneID { get; set; }
|
|
[System.ComponentModel.DataAnnotations.Range(1, 10)]
|
|
public int? Intensity { get; set; }
|
|
public bool Mutual { get; set; }
|
|
public bool Known { get; set; }
|
|
[System.ComponentModel.DataAnnotations.StringLength(4000)]
|
|
public string? Notes { get; set; }
|
|
}
|