The self-nesting outline tree was more structure than chapter outlining needs.
A chapter outline is now a paragraph plus a flat, ordered table of beats, and
tags do the cross-referencing that nesting was doing badly.
A beat is one row: a three-to-five word title, an optional character, what
happened, and what's next. Ordering is a SortOrder column within the chapter —
no parent pointers, no cycle guards, no recursive tree building. Reordering is
one call taking beat ids in the order wanted; ids left out keep their relative
position at the end rather than jumping to the front.
Beats plan, scenes carry prose. The two layers stay separate and a beat's
SceneId is the optional link between them, nullable in both directions —
deleting a scene ungroups its beats rather than deleting the plan, since that
is a decision about prose and not about the outline.
Tags are project-scoped, unique by name case-insensitively, and attach to
characters, chapters and beats through three join tables so cascade deletes are
the database's job rather than ours. Applying an unknown tag by name creates it,
which keeps tagging a single action; GET /api/tags/{id}/references returns
everything carrying a tag across all three kinds at once.
Removed: OutlineNode, OutlineService, its endpoints, agent and MCP tools, and
the Outline tab. Added: Beat and Tag with their services, endpoints, 5 agent
tools and 10 MCP tools, a beat table on the chapter page, a tag editor used in
three places, and a Tags tab for cross-referencing.
Migration drops OutlineNodes — the scaffolder's data-loss warning is the
intended removal, not an accident.
44 tests, up from 31.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01S56bfZMGe1hnhpWP4CjjNw
148 lines
5.7 KiB
C#
148 lines
5.7 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using NovelSoftware.Application.Dtos;
|
|
using NovelSoftware.Domain.Entities;
|
|
|
|
namespace NovelSoftware.Application.Services;
|
|
|
|
public class CharacterService(INovelDbContext db, TagService tags)
|
|
{
|
|
public async Task<IReadOnlyList<CharacterDto>> ListAsync(Guid projectId, CancellationToken ct = default)
|
|
{
|
|
var characters = await Query()
|
|
.Where(c => c.ProjectId == projectId)
|
|
.OrderBy(c => c.Role)
|
|
.ThenBy(c => c.Name)
|
|
.ToListAsync(ct);
|
|
|
|
return [.. characters.Select(c => c.ToDto())];
|
|
}
|
|
|
|
public async Task<CharacterDto> GetAsync(Guid id, CancellationToken ct = default) =>
|
|
(await FindAsync(id, ct)).ToDto();
|
|
|
|
public async Task<CharacterDto> CreateAsync(Guid projectId, CreateCharacterRequest request, CancellationToken ct = default)
|
|
{
|
|
await EnsureProjectExists(projectId, ct);
|
|
|
|
var character = new Character
|
|
{
|
|
ProjectId = projectId,
|
|
Name = request.Name,
|
|
Role = request.Role,
|
|
Age = request.Age,
|
|
Pronouns = request.Pronouns,
|
|
Occupation = request.Occupation,
|
|
Appearance = request.Appearance,
|
|
Personality = request.Personality,
|
|
Backstory = request.Backstory,
|
|
Want = request.Want,
|
|
Need = request.Need,
|
|
InternalConflict = request.InternalConflict,
|
|
ExternalConflict = request.ExternalConflict,
|
|
ArcSummary = request.ArcSummary,
|
|
Voice = request.Voice,
|
|
Notes = request.Notes
|
|
};
|
|
|
|
if (request.Tags is { } names)
|
|
{
|
|
character.Tags = await tags.ResolveAsync(projectId, names, ct);
|
|
}
|
|
|
|
db.Characters.Add(character);
|
|
await db.SaveChangesAsync(ct);
|
|
return (await FindAsync(character.Id, ct)).ToDto();
|
|
}
|
|
|
|
public async Task<CharacterDto> UpdateAsync(Guid id, UpdateCharacterRequest request, CancellationToken ct = default)
|
|
{
|
|
var character = await FindAsync(id, ct);
|
|
|
|
character.Name = Patch.Apply(character.Name, request.Name) ?? character.Name;
|
|
character.Role = request.Role ?? character.Role;
|
|
character.Age = Patch.Apply(character.Age, request.Age);
|
|
character.Pronouns = Patch.Apply(character.Pronouns, request.Pronouns);
|
|
character.Occupation = Patch.Apply(character.Occupation, request.Occupation);
|
|
character.Appearance = Patch.Apply(character.Appearance, request.Appearance);
|
|
character.Personality = Patch.Apply(character.Personality, request.Personality);
|
|
character.Backstory = Patch.Apply(character.Backstory, request.Backstory);
|
|
character.Want = Patch.Apply(character.Want, request.Want);
|
|
character.Need = Patch.Apply(character.Need, request.Need);
|
|
character.InternalConflict = Patch.Apply(character.InternalConflict, request.InternalConflict);
|
|
character.ExternalConflict = Patch.Apply(character.ExternalConflict, request.ExternalConflict);
|
|
character.ArcSummary = Patch.Apply(character.ArcSummary, request.ArcSummary);
|
|
character.Voice = Patch.Apply(character.Voice, request.Voice);
|
|
character.Notes = Patch.Apply(character.Notes, request.Notes);
|
|
character.UpdatedAt = DateTimeOffset.UtcNow;
|
|
|
|
if (request.Tags is { } names)
|
|
{
|
|
character.Tags = await tags.ResolveAsync(character.ProjectId, names, ct);
|
|
}
|
|
|
|
await db.SaveChangesAsync(ct);
|
|
return (await FindAsync(id, ct)).ToDto();
|
|
}
|
|
|
|
public async Task DeleteAsync(Guid id, CancellationToken ct = default)
|
|
{
|
|
var character = await FindAsync(id, ct);
|
|
db.Characters.Remove(character);
|
|
await db.SaveChangesAsync(ct);
|
|
}
|
|
|
|
public async Task<CharacterDto> AddRelationshipAsync(
|
|
Guid characterId, CreateRelationshipRequest request, CancellationToken ct = default)
|
|
{
|
|
var character = await FindAsync(characterId, ct);
|
|
|
|
var related = await db.Characters
|
|
.FirstOrDefaultAsync(c => c.Id == request.RelatedCharacterId, ct)
|
|
?? throw new NotFoundException(nameof(Character), request.RelatedCharacterId);
|
|
|
|
if (related.ProjectId != character.ProjectId)
|
|
{
|
|
throw new InvalidOperationException("Characters must belong to the same project to be related.");
|
|
}
|
|
|
|
db.CharacterRelationships.Add(new CharacterRelationship
|
|
{
|
|
CharacterId = characterId,
|
|
RelatedCharacterId = request.RelatedCharacterId,
|
|
RelationshipType = request.RelationshipType,
|
|
Description = request.Description
|
|
});
|
|
|
|
await db.SaveChangesAsync(ct);
|
|
return (await FindAsync(characterId, ct)).ToDto();
|
|
}
|
|
|
|
public async Task RemoveRelationshipAsync(Guid relationshipId, CancellationToken ct = default)
|
|
{
|
|
var relationship = await db.CharacterRelationships
|
|
.FirstOrDefaultAsync(r => r.Id == relationshipId, ct)
|
|
?? throw new NotFoundException(nameof(CharacterRelationship), relationshipId);
|
|
|
|
db.CharacterRelationships.Remove(relationship);
|
|
await db.SaveChangesAsync(ct);
|
|
}
|
|
|
|
private IQueryable<Character> Query() =>
|
|
db.Characters
|
|
.Include(c => c.Relationships)
|
|
.ThenInclude(r => r.RelatedCharacter)
|
|
.Include(c => c.Tags);
|
|
|
|
private async Task<Character> FindAsync(Guid id, CancellationToken ct) =>
|
|
await Query().FirstOrDefaultAsync(c => c.Id == id, ct)
|
|
?? throw new NotFoundException(nameof(Character), id);
|
|
|
|
private async Task EnsureProjectExists(Guid projectId, CancellationToken ct)
|
|
{
|
|
if (!await db.Projects.AnyAsync(p => p.Id == projectId, ct))
|
|
{
|
|
throw new NotFoundException(nameof(Project), projectId);
|
|
}
|
|
}
|
|
}
|