The layered split into Domain/Application/Infrastructure/Api was forcing organisation by layer: adding one capability meant touching four projects and four folders that each held a slice of it. Those four projects are now one feature-organised Novelly.Api, where each folder — Projects, Characters, Chapters, Beats, Scenes, Tags, Agent — holds its entity, DTOs, service and endpoints together. Common/ holds what genuinely crosses features (the patch semantics, the two exception types, DraftStatus) and Data/ holds the DbContext and migrations. Six .NET projects become five: the three layer projects are gone, and Novelly.AppHost and Novelly.ServiceDefaults are new. - Namespaces move from NovelSoftware.* to Novelly.*, including the entity type names recorded in the EF model snapshots. The migration ids are untouched, so an existing novel.db still migrates cleanly — verified against a fresh file. - Aspire orchestration mirrors the mic-check setup: the AppHost starts the API on :5080 and the Vite dev server on :5173, and the API picks up OpenTelemetry, health checks and service discovery from ServiceDefaults. /health and /alive now answer in development. - A Husky pre-push hook runs scripts/ci/prepush.sh: build, test, then a web build. The scripts are plain bash so CI can run the same steps. - The MCP server's env var is now NOVELLY_API_URL. Verified beyond the build: 44 tests pass, the web client builds, the API was exercised over curl (project/chapter/beat/tag round trip, tag cross-reference, 503 on the agent without a key while conversation listing still returns 200), the MCP server was driven over stdio JSON-RPC (26 tools, errors still surface the API's own message rather than being flattened), and the AppHost was run to confirm both resources come up and Vite proxies /api through to the API. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01S56bfZMGe1hnhpWP4CjjNw
166 lines
5.8 KiB
C#
166 lines
5.8 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Novelly.Api.Chapters;
|
|
using Novelly.Api.Common;
|
|
using Novelly.Api.Data;
|
|
using Novelly.Api.Tags;
|
|
|
|
namespace Novelly.Api.Beats;
|
|
|
|
/// <summary>
|
|
/// Beats are a chapter's outline: a flat, ordered table rather than a tree. Everything
|
|
/// here is scoped to one chapter.
|
|
/// </summary>
|
|
public class BeatService(INovelDbContext db, TagService tags)
|
|
{
|
|
public async Task<IReadOnlyList<BeatDto>> ListAsync(Guid chapterId, CancellationToken ct = default)
|
|
{
|
|
var beats = await Query()
|
|
.Where(b => b.ChapterId == chapterId)
|
|
.OrderBy(b => b.SortOrder)
|
|
.ToListAsync(ct);
|
|
|
|
return [.. beats.Select(b => b.ToDto())];
|
|
}
|
|
|
|
public async Task<BeatDto> GetAsync(Guid id, CancellationToken ct = default) =>
|
|
(await FindAsync(id, ct)).ToDto();
|
|
|
|
public async Task<BeatDto> CreateAsync(Guid chapterId, CreateBeatRequest request, CancellationToken ct = default)
|
|
{
|
|
var chapter = await db.Chapters.FirstOrDefaultAsync(c => c.Id == chapterId, ct)
|
|
?? throw new NotFoundException(nameof(Chapter), chapterId);
|
|
|
|
await ValidateReferencesAsync(chapter, request.CharacterId, request.SceneId, ct);
|
|
|
|
var beat = new Beat
|
|
{
|
|
ChapterId = chapterId,
|
|
Title = request.Title,
|
|
SortOrder = request.SortOrder ?? await NextSortOrderAsync(chapterId, ct),
|
|
CharacterId = request.CharacterId,
|
|
WhatHappened = request.WhatHappened,
|
|
WhatsNext = request.WhatsNext,
|
|
SceneId = request.SceneId
|
|
};
|
|
|
|
if (request.Tags is { } names)
|
|
{
|
|
beat.Tags = await tags.ResolveAsync(chapter.ProjectId, names, ct);
|
|
}
|
|
|
|
db.Beats.Add(beat);
|
|
await db.SaveChangesAsync(ct);
|
|
return (await FindAsync(beat.Id, ct)).ToDto();
|
|
}
|
|
|
|
public async Task<BeatDto> UpdateAsync(Guid id, UpdateBeatRequest request, CancellationToken ct = default)
|
|
{
|
|
var beat = await FindAsync(id, ct);
|
|
var chapter = await db.Chapters.FirstOrDefaultAsync(c => c.Id == beat.ChapterId, ct)
|
|
?? throw new NotFoundException(nameof(Chapter), beat.ChapterId);
|
|
|
|
await ValidateReferencesAsync(chapter, request.CharacterId, request.SceneId, ct);
|
|
|
|
beat.Title = Patch.Apply(beat.Title, request.Title) ?? beat.Title;
|
|
beat.SortOrder = request.SortOrder ?? beat.SortOrder;
|
|
beat.CharacterId = request.CharacterId ?? beat.CharacterId;
|
|
beat.WhatHappened = Patch.Apply(beat.WhatHappened, request.WhatHappened);
|
|
beat.WhatsNext = Patch.Apply(beat.WhatsNext, request.WhatsNext);
|
|
beat.SceneId = request.SceneId ?? beat.SceneId;
|
|
beat.UpdatedAt = DateTimeOffset.UtcNow;
|
|
|
|
if (request.Tags is { } names)
|
|
{
|
|
beat.Tags = await tags.ResolveAsync(chapter.ProjectId, names, ct);
|
|
}
|
|
|
|
await db.SaveChangesAsync(ct);
|
|
return (await FindAsync(id, ct)).ToDto();
|
|
}
|
|
|
|
public async Task DeleteAsync(Guid id, CancellationToken ct = default)
|
|
{
|
|
var beat = await FindAsync(id, ct);
|
|
db.Beats.Remove(beat);
|
|
await db.SaveChangesAsync(ct);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Renumbers a chapter's beats to match the order given. Sending the whole list beats
|
|
/// patching sort orders one at a time, which is fiddly to get right from a drag handle.
|
|
/// </summary>
|
|
public async Task<IReadOnlyList<BeatDto>> ReorderAsync(
|
|
Guid chapterId, ReorderBeatsRequest request, CancellationToken ct = default)
|
|
{
|
|
var beats = await db.Beats.Where(b => b.ChapterId == chapterId).ToListAsync(ct);
|
|
|
|
var missing = request.BeatIds.Where(id => beats.All(b => b.Id != id)).ToList();
|
|
if (missing.Count > 0)
|
|
{
|
|
throw new NotFoundException(nameof(Beat), missing[0]);
|
|
}
|
|
|
|
// Listed beats take the order given; anything omitted keeps its relative position
|
|
// after them rather than silently jumping to the front.
|
|
var order = 1;
|
|
foreach (var id in request.BeatIds)
|
|
{
|
|
beats.Single(b => b.Id == id).SortOrder = order++;
|
|
}
|
|
|
|
foreach (var beat in beats.Where(b => !request.BeatIds.Contains(b.Id)).OrderBy(b => b.SortOrder))
|
|
{
|
|
beat.SortOrder = order++;
|
|
}
|
|
|
|
await db.SaveChangesAsync(ct);
|
|
return await ListAsync(chapterId, ct);
|
|
}
|
|
|
|
private async Task ValidateReferencesAsync(
|
|
Chapter chapter, Guid? characterId, Guid? sceneId, CancellationToken ct)
|
|
{
|
|
if (characterId is { } cid)
|
|
{
|
|
var belongs = await db.Characters
|
|
.AnyAsync(c => c.Id == cid && c.ProjectId == chapter.ProjectId, ct);
|
|
|
|
if (!belongs)
|
|
{
|
|
throw new InvalidOperationException(
|
|
"A beat's character must belong to the same project as its chapter.");
|
|
}
|
|
}
|
|
|
|
if (sceneId is { } sid)
|
|
{
|
|
var belongs = await db.Scenes.AnyAsync(s => s.Id == sid && s.ChapterId == chapter.Id, ct);
|
|
|
|
if (!belongs)
|
|
{
|
|
throw new InvalidOperationException(
|
|
"A beat can only be grouped under a scene in the same chapter.");
|
|
}
|
|
}
|
|
}
|
|
|
|
private async Task<int> NextSortOrderAsync(Guid chapterId, CancellationToken ct)
|
|
{
|
|
var max = await db.Beats
|
|
.Where(b => b.ChapterId == chapterId)
|
|
.MaxAsync(b => (int?)b.SortOrder, ct);
|
|
|
|
return (max ?? 0) + 1;
|
|
}
|
|
|
|
private IQueryable<Beat> Query() =>
|
|
db.Beats
|
|
.Include(b => b.Character)
|
|
.Include(b => b.Scene)
|
|
.Include(b => b.Tags);
|
|
|
|
private async Task<Beat> FindAsync(Guid id, CancellationToken ct) =>
|
|
await Query().FirstOrDefaultAsync(b => b.Id == id, ct)
|
|
?? throw new NotFoundException(nameof(Beat), id);
|
|
}
|