-- Additive native chapter suggestions only. No canonical chapter/scene/prose writes. SET ANSI_NULLS ON; SET QUOTED_IDENTIFIER ON; GO IF OBJECT_ID(N'dbo.NativeChapterReviews',N'U') IS NULL BEGIN CREATE TABLE dbo.NativeChapterReviews( ReviewID uniqueidentifier NOT NULL PRIMARY KEY, UserID int NOT NULL,BookID int NOT NULL,ManuscriptID uniqueidentifier NOT NULL, ChapterStart int NOT NULL,ChapterEnd int NOT NULL,ContentHash varchar(64) NOT NULL, ResultJson nvarchar(max) NOT NULL,DecisionsJson nvarchar(max) NULL, Version int NOT NULL DEFAULT 1,CreatedUtc datetime2 NOT NULL DEFAULT SYSUTCDATETIME()); CREATE UNIQUE INDEX UX_NativeChapterReviews_Content ON dbo.NativeChapterReviews(UserID,BookID,ManuscriptID,ContentHash); END; -- No foreign keys: suggestions must not block existing project/book deletion or restore. -- Controllers revalidate live book access for every read/write. Expired rows are purged on save. GO CREATE OR ALTER PROCEDURE dbo.NativeChapterReview_Access @Action varchar(16),@UserID int,@BookID int=0,@ReviewID uniqueidentifier=NULL, @ManuscriptID uniqueidentifier=NULL,@ChapterStart int=0,@ChapterEnd int=0, @ContentHash varchar(64)=NULL,@ResultJson nvarchar(max)=NULL,@DecisionsJson nvarchar(max)=NULL,@Version int=0 AS BEGIN SET NOCOUNT ON;SET XACT_ABORT ON; IF @Action='Find' SELECT TOP(1) * FROM dbo.NativeChapterReviews WHERE UserID=@UserID AND BookID=@BookID AND ManuscriptID=@ManuscriptID AND ContentHash=@ContentHash AND CreatedUtc>DATEADD(day,-30,SYSUTCDATETIME()); ELSE IF @Action='Get' SELECT * FROM dbo.NativeChapterReviews WHERE UserID=@UserID AND ReviewID=@ReviewID AND CreatedUtc>DATEADD(day,-30,SYSUTCDATETIME()); ELSE IF @Action='List' SELECT TOP(500) * FROM dbo.NativeChapterReviews WHERE UserID=@UserID AND BookID=@BookID AND CreatedUtc>DATEADD(day,-30,SYSUTCDATETIME()) ORDER BY CreatedUtc DESC; ELSE IF @Action='Save' BEGIN IF ISJSON(@ResultJson)<>1 OR DATALENGTH(@ResultJson)>2097152 THROW 51000,'Invalid review payload',1; BEGIN TRANSACTION; DELETE FROM dbo.NativeChapterReviews WHERE UserID=@UserID AND CreatedUtc<=DATEADD(day,-30,SYSUTCDATETIME()); IF NOT EXISTS(SELECT 1 FROM dbo.NativeChapterReviews WITH(UPDLOCK,HOLDLOCK) WHERE UserID=@UserID AND BookID=@BookID AND ManuscriptID=@ManuscriptID AND ContentHash=@ContentHash) BEGIN IF (SELECT COUNT(*) FROM dbo.NativeChapterReviews WHERE UserID=@UserID)>=1000 THROW 51000,'Review capacity reached',1; INSERT dbo.NativeChapterReviews(ReviewID,UserID,BookID,ManuscriptID,ChapterStart,ChapterEnd,ContentHash,ResultJson) VALUES(@ReviewID,@UserID,@BookID,@ManuscriptID,@ChapterStart,@ChapterEnd,@ContentHash,@ResultJson); END; COMMIT; SELECT TOP(1) * FROM dbo.NativeChapterReviews WHERE UserID=@UserID AND BookID=@BookID AND ManuscriptID=@ManuscriptID AND ContentHash=@ContentHash; END ELSE IF @Action='Decide' BEGIN IF ISJSON(@DecisionsJson)<>1 OR DATALENGTH(@DecisionsJson)>2097152 THROW 51000,'Invalid decision payload',1; UPDATE dbo.NativeChapterReviews SET DecisionsJson=@DecisionsJson,Version=Version+1 WHERE UserID=@UserID AND ReviewID=@ReviewID AND Version=@Version AND CreatedUtc>DATEADD(day,-30,SYSUTCDATETIME()); SELECT @@ROWCOUNT; END; END; GO