PlotDirector/PlotLine/Services/ApplicationNavigation.cs

61 lines
3.4 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
{
"Projects" or "Books" or "Chapters" or "Scenes" => "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
} };
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 projectId = project?.ProjectID ?? PositiveId(Property(model, "ProjectID"))
?? book?.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 ?? 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);
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;
}