68 lines
4.5 KiB
C#
68 lines
4.5 KiB
C#
using PlotLine.Data;
|
|
using PlotLine.Models;
|
|
using PlotLine.ViewModels;
|
|
|
|
namespace PlotLine.Services;
|
|
|
|
// Request-scoped: existing project partials register their already-authorised context
|
|
// while the body renders, before the shared layout renders the application menu.
|
|
public sealed class ApplicationNavigation(IProjectRepository projects, IBookRepository books, ICurrentUserService currentUser)
|
|
{
|
|
public Project? Project { get; set; }
|
|
|
|
public async Task<ApplicationNavigationViewModel> BuildAsync(object? model, string? section, HttpRequest request)
|
|
{
|
|
var controller = request.RouteValues["controller"]?.ToString() ?? string.Empty;
|
|
var result = new ApplicationNavigationViewModel { ActiveSection = section ?? controller switch
|
|
{
|
|
"WritingActivity" => "Writer",
|
|
"Projects" or "Books" or "Chapters" or "Scenes" or "ManuscriptReader" => "Overview",
|
|
"StoryCalendar" => "Calendar", "StoryPoint" => "At This Point",
|
|
"RelationshipMap" => "Relationships", "StoryAssets" => "Assets",
|
|
"FloorPlans" => "Floor Plans", "PlotThreads" => "Threads", "PlotLineWorkspace" => "Plot Lines",
|
|
"ProjectMetrics" => "Metrics", "StoryBible" => "Story Bible",
|
|
"ContinuityExplorer" => "Continuity Explorer", "Analytics" or "Dynamics" => "Health",
|
|
"Archives" => "Archive", _ => controller
|
|
} };
|
|
var writerDestination = Property(model, "Destination") as string;
|
|
result.WriterDestination = controller == "WritingActivity" ? "Reflect" : controller == "WriterPlanning" ? "Plan"
|
|
: writerDestination is null or "Legacy" ? WriterNavigation.DestinationForAction(request.RouteValues["action"]?.ToString()) : writerDestination;
|
|
if (currentUser.UserId is not int userId) return result;
|
|
|
|
var project = Project ?? Property(model, "Project") as Project ?? model as Project;
|
|
var book = Property(model, "Book") as Book ?? (model as StoryPointViewModel)?.Snapshot?.Book;
|
|
var writingPlan = (model as WritingSchedulePreviewViewModel)?.SelectedPlan;
|
|
var projectId = project?.ProjectID ?? PositiveId(Property(model, "ProjectID"))
|
|
?? book?.ProjectID ?? writingPlan?.ProjectID ?? PositiveId(request.Query["projectId"].FirstOrDefault());
|
|
if (projectId is not > 0) return result;
|
|
|
|
// Resolve through the user-scoped repository even on models that only carry IDs.
|
|
// Never use a remembered project or an unverified URL value as navigation context.
|
|
project = await projects.GetForUserAsync(projectId.Value, userId);
|
|
if (project is null) return result;
|
|
var projectBooks = await books.ListByProjectAsync(project.ProjectID);
|
|
var selectedBookId = model switch
|
|
{
|
|
StoryPointViewModel point => point.Snapshot?.Book.BookID,
|
|
TimelineViewModel timeline => timeline.SelectedBookID,
|
|
StoryCalendarViewModel calendar => calendar.SelectedBookID,
|
|
NamesLibraryViewModel names => names.Filter.BookID,
|
|
_ => book?.BookID ?? writingPlan?.BookID ?? PositiveId(Property(model, "SelectedBookID"))
|
|
?? PositiveId(Property(model, "BookID"))
|
|
?? PositiveId(Property(Property(model, "Filter"), "BookID"))
|
|
?? PositiveId(request.Query["bookId"].FirstOrDefault())
|
|
};
|
|
result.Project = project;
|
|
result.Books = projectBooks.Where(x => !x.IsArchived).ToList();
|
|
result.Book = result.Books.FirstOrDefault(x => x.BookID == selectedBookId);
|
|
result.WordChapterID = (model as WriterDashboardViewModel)?.ReviewReader?.Chapter?.ChapterID ?? ((model as WriterDashboardViewModel)?.Destination=="Write" ? (model as WriterDashboardViewModel)?.Objective?.ChapterID : null) ?? PositiveId(Property(model,"ChapterID")) ?? PositiveId(Property(Property(model,"Scene"),"ChapterID"));
|
|
result.WordSceneID = ((model as WriterDashboardViewModel)?.Destination=="Write" ? (model as WriterDashboardViewModel)?.Objective?.SceneID : null) ?? PositiveId(Property(model,"SceneID")) ?? PositiveId(Property(Property(model,"Scene"),"SceneID"));
|
|
return result;
|
|
}
|
|
|
|
// The existing view models share these named properties but no common base class.
|
|
// Keep that compatibility boundary here rather than modifying every page/model.
|
|
private static object? Property(object? model, string name) => model?.GetType().GetProperty(name)?.GetValue(model);
|
|
private static int? PositiveId(object? value) => int.TryParse(value?.ToString(), out var id) && id > 0 ? id : null;
|
|
}
|