37 lines
3.1 KiB
C#

using PlotLine.Data;
using PlotLine.Models;
namespace PlotLine.Services.Context;
public sealed record ContextNameEntry(Character Character,IReadOnlyList<CharacterAlias> Aliases,IReadOnlyList<string> SharedSpellings);
public sealed record ContextNamePanel(int ProjectID,int BookID,bool CanEdit,IReadOnlyList<ContextNameEntry> Entries,PlotLine.ViewModels.NamesLibraryViewModel? Library);
public sealed class ContextNames(ICharacterRepository characters,IProjectRepository projects,ICurrentUserService user,INameIntelligenceService names,INameIntelligenceRepository reference)
{
public async Task<ContextNamePanel> Build(int projectId,int bookId,NameSearchFilter? filter=null,CancellationToken cancellationToken=default)
{
var author=user.UserId??throw new UnauthorizedAccessException();
var project=await projects.GetForUserAsync(projectId,author)??throw new UnauthorizedAccessException();
filter??=new(){BookID=bookId,UsedScope="Book",PageSize=25};filter.ProjectID=projectId;
var library=await names.BuildLibraryAsync(filter,cancellationToken);
return new(projectId,bookId,ContextEditPolicy.CanEdit(project),[],library);
}
public async Task<IReadOnlyList<NameUsageDecoration>> Usage(int projectId,int? bookId,string search,string scope,CancellationToken cancellationToken=default)
{
var author=user.UserId??throw new UnauthorizedAccessException();if(await projects.GetForUserAsync(projectId,author)==null)throw new UnauthorizedAccessException();
return await reference.ListUsedDecorationsAsync(author,projectId,bookId,NameIntelligenceService.NormaliseName(search),scope,cancellationToken);
}
public async Task<ContextNamePanel> Identity(int projectId,int bookId,int characterId)
{
var author=user.UserId??throw new UnauthorizedAccessException();var project=await projects.GetForUserAsync(projectId,author)??throw new UnauthorizedAccessException();
var person=await characters.GetCharacterAsync(characterId)??throw new UnauthorizedAccessException();if(person.ProjectID!=projectId)throw new UnauthorizedAccessException();
var aliases=await characters.ListAliasesAsync(characterId);
return new(projectId,bookId,ContextEditPolicy.CanEdit(project),Compose([person],aliases.Take(50).ToList()),null);
}
public static IReadOnlyList<ContextNameEntry> Compose(IReadOnlyList<Character> people,IReadOnlyList<CharacterAlias> aliases)
{
var active=people.Where(c=>!c.IsArchived).ToList();var ids=active.Select(c=>c.CharacterID).ToHashSet();
var all=active.Select(c=>(c.CharacterID,Name:c.CharacterName)).Concat(aliases.Where(a=>ids.Contains(a.CharacterID)).Select(a=>(a.CharacterID,Name:a.Alias))).ToList();
var shared=all.GroupBy(a=>NameIntelligenceService.NormaliseName(a.Name)).Where(g=>g.Select(a=>a.CharacterID).Distinct().Count()>1).Select(g=>g.Key).ToHashSet();
return active.OrderBy(c=>c.CharacterName).Select(c=>new ContextNameEntry(c,aliases.Where(a=>a.CharacterID==c.CharacterID).ToList(),all.Where(a=>a.CharacterID==c.CharacterID&&shared.Contains(NameIntelligenceService.NormaliseName(a.Name))).Select(a=>a.Name).Distinct().ToList())).ToList();
}
}