PlotDirector/PlotLine/Sql/349_StoryElementReferences.sql
2026-12-05 17:22:38 +00:00

158 lines
7.4 KiB
Transact-SQL

SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO
IF OBJECT_ID(N'dbo.StoryElementReferences', N'U') IS NULL
BEGIN
CREATE TABLE dbo.StoryElementReferences
(
ReferenceID int IDENTITY(1,1) NOT NULL PRIMARY KEY,
CharacterID int NULL REFERENCES dbo.Characters(CharacterID) ON DELETE CASCADE,
LocationID int NULL REFERENCES dbo.Locations(LocationID) ON DELETE CASCADE,
StoryAssetID int NULL REFERENCES dbo.StoryAssets(StoryAssetID) ON DELETE CASCADE,
Kind varchar(14) NOT NULL,
Note nvarchar(500) NOT NULL,
StoragePath nvarchar(1000) NULL,
OriginalFileName nvarchar(300) NULL,
Version rowversion NOT NULL,
CONSTRAINT CK_StoryElementReferences_Entity CHECK
((CASE WHEN CharacterID IS NULL THEN 0 ELSE 1 END +
CASE WHEN LocationID IS NULL THEN 0 ELSE 1 END +
CASE WHEN StoryAssetID IS NULL THEN 0 ELSE 1 END) = 1),
CONSTRAINT CK_StoryElementReferences_Content CHECK
((Kind = 'Characteristic' AND LEN(Note) > 0 AND StoragePath IS NULL AND OriginalFileName IS NULL) OR
(Kind IN ('Image', 'Attachment') AND StoragePath IS NOT NULL AND OriginalFileName IS NOT NULL))
);
CREATE INDEX IX_StoryElementReferences_Character ON dbo.StoryElementReferences(CharacterID, ReferenceID) WHERE CharacterID IS NOT NULL;
CREATE INDEX IX_StoryElementReferences_Location ON dbo.StoryElementReferences(LocationID, ReferenceID) WHERE LocationID IS NOT NULL;
CREATE INDEX IX_StoryElementReferences_Asset ON dbo.StoryElementReferences(StoryAssetID, ReferenceID) WHERE StoryAssetID IS NOT NULL;
END;
GO
CREATE OR ALTER TRIGGER dbo.StoryElementReferences_MarkFilesDeleted
ON dbo.StoryElementReferences AFTER DELETE
AS
BEGIN
SET NOCOUNT ON;
UPDATE f SET IsDeleted = 1, DeletedDateUTC = SYSUTCDATETIME()
FROM dbo.UserFile f
INNER JOIN deleted d ON d.ReferenceID = f.EntityID AND d.StoragePath = f.StoragePath
WHERE f.EntityType = N'StoryElementReference' AND f.IsDeleted = 0;
END;
GO
CREATE OR ALTER PROCEDURE dbo.StoryElementReference_Access
@UserID int, @EntityType varchar(12), @EntityID int, @Action varchar(12),
@ReferenceID int = NULL, @Version binary(8) = NULL, @Kind varchar(14) = NULL,
@Note nvarchar(500) = NULL, @StoragePath nvarchar(1000) = NULL,
@OriginalFileName nvarchar(300) = NULL, @FileSizeBytes bigint = NULL
AS
BEGIN
SET NOCOUNT ON;
SET XACT_ABORT ON;
DECLARE @ProjectID int, @EntityName nvarchar(300), @CanEdit bit, @OwnerID int,
@CharacterID int, @LocationID int, @AssetID int;
BEGIN TRY
BEGIN TRANSACTION;
IF @EntityType = 'Character'
BEGIN
SET @CharacterID = @EntityID;
SELECT @ProjectID = ProjectID, @EntityName = CharacterName
FROM dbo.Characters WITH (HOLDLOCK) WHERE CharacterID = @EntityID AND IsArchived = 0;
END
ELSE IF @EntityType = 'Location'
BEGIN
SET @LocationID = @EntityID;
SELECT @ProjectID = ProjectID, @EntityName = LocationName
FROM dbo.Locations WITH (HOLDLOCK) WHERE LocationID = @EntityID AND IsArchived = 0;
END
ELSE IF @EntityType = 'Asset'
BEGIN
SET @AssetID = @EntityID;
SELECT @ProjectID = ProjectID, @EntityName = AssetName
FROM dbo.StoryAssets WITH (HOLDLOCK) WHERE StoryAssetID = @EntityID AND IsArchived = 0;
END
SELECT @CanEdit = CONVERT(bit, MAX(CASE WHEN a.AccessRole IN (N'Owner', N'Collaborator') THEN 1 ELSE 0 END))
FROM dbo.ProjectUserAccess a WITH (HOLDLOCK)
INNER JOIN dbo.Projects p WITH (HOLDLOCK) ON p.ProjectID = a.ProjectID AND p.IsArchived = 0
WHERE a.ProjectID = @ProjectID AND a.UserID = @UserID AND a.IsActive = 1;
IF @CanEdit IS NULL OR (@Action NOT IN ('list', 'file') AND @CanEdit = 0)
BEGIN
IF @Action = 'list'
SELECT @ProjectID AS ProjectID, @EntityName AS EntityName, @CanEdit AS CanEdit WHERE 1 = 0;
COMMIT;
RETURN;
END
IF @Action = 'list'
BEGIN
SELECT @ProjectID AS ProjectID, @EntityName AS EntityName, @CanEdit AS CanEdit;
SELECT ReferenceID, Kind, Note, OriginalFileName, Version
FROM dbo.StoryElementReferences
WHERE CharacterID = @CharacterID OR LocationID = @LocationID OR StoryAssetID = @AssetID
ORDER BY ReferenceID;
END
ELSE IF @Action = 'file'
BEGIN
SELECT Kind, StoragePath, OriginalFileName
FROM dbo.StoryElementReferences
WHERE ReferenceID = @ReferenceID AND StoragePath IS NOT NULL
AND (CharacterID = @CharacterID OR LocationID = @LocationID OR StoryAssetID = @AssetID);
END
ELSE IF @Action = 'add'
BEGIN
IF @StoragePath IS NOT NULL
BEGIN
SELECT TOP (1) @OwnerID = UserID FROM dbo.ProjectUserAccess WITH (HOLDLOCK)
WHERE ProjectID = @ProjectID AND AccessRole = N'Owner' AND IsActive = 1
ORDER BY ProjectUserAccessID;
IF @OwnerID IS NULL THROW 51010, 'Project owner unavailable.', 1;
END
INSERT dbo.StoryElementReferences (CharacterID, LocationID, StoryAssetID, Kind, Note, StoragePath, OriginalFileName)
VALUES (@CharacterID, @LocationID, @AssetID, @Kind, @Note, @StoragePath, @OriginalFileName);
IF @StoragePath IS NOT NULL
INSERT dbo.UserFile (UserID, ProjectID, EntityType, EntityID, FileName, OriginalFileName, ContentType, FileSizeBytes, StoragePath)
VALUES (@OwnerID, @ProjectID, N'StoryElementReference', CONVERT(int, SCOPE_IDENTITY()),
RIGHT(@StoragePath, CHARINDEX('/', REVERSE(@StoragePath)) - 1), @OriginalFileName,
CASE @Kind WHEN 'Image' THEN N'image/webp' ELSE N'application/octet-stream' END, @FileSizeBytes, @StoragePath);
SELECT @ProjectID AS ProjectID, CONVERT(bit, 1) AS Changed;
END
ELSE IF @Action = 'caption'
BEGIN
UPDATE dbo.CharacterImages SET Caption = NULLIF(@Note, N'')
WHERE CharacterImageID = @ReferenceID AND CharacterID = @CharacterID AND ProjectID = @ProjectID AND IsDeleted = 0;
SELECT @ProjectID AS ProjectID, CONVERT(bit, @@ROWCOUNT) AS Changed;
END
ELSE IF @Action = 'edit'
BEGIN
UPDATE dbo.StoryElementReferences SET Note = @Note
WHERE ReferenceID = @ReferenceID AND Version = @Version
AND (Kind <> 'Characteristic' OR LEN(@Note) > 0)
AND (CharacterID = @CharacterID OR LocationID = @LocationID OR StoryAssetID = @AssetID);
SELECT @ProjectID AS ProjectID, CONVERT(bit, @@ROWCOUNT) AS Changed;
END
ELSE IF @Action = 'delete'
BEGIN
DECLARE @Removed TABLE (StoragePath nvarchar(1000));
DELETE dbo.StoryElementReferences
OUTPUT deleted.StoragePath INTO @Removed
WHERE ReferenceID = @ReferenceID AND Version = @Version
AND (CharacterID = @CharacterID OR LocationID = @LocationID OR StoryAssetID = @AssetID);
SELECT @ProjectID AS ProjectID, CONVERT(bit, COUNT(*)) AS Changed, MAX(StoragePath) AS StoragePath
FROM @Removed;
END
COMMIT;
END TRY
BEGIN CATCH
IF XACT_STATE() <> 0 ROLLBACK;
THROW;
END CATCH
END;
GO