CREATE OR ALTER PROCEDURE dbo.AssetDependency_Save @AssetDependencyID int = NULL, @SourceAssetID int, @TargetAssetID int, @AssetDependencyTypeID int, @SourceRequiredStateID int = NULL, @TargetBlockedStateID int = NULL, @Description nvarchar(max) = NULL AS BEGIN SET NOCOUNT ON; IF @SourceAssetID = @TargetAssetID THROW 51000, 'An asset cannot depend on itself.', 1; IF NOT EXISTS ( SELECT 1 FROM dbo.StoryAssets source INNER JOIN dbo.StoryAssets target ON target.ProjectID = source.ProjectID WHERE source.StoryAssetID = @SourceAssetID AND target.StoryAssetID = @TargetAssetID ) THROW 51000, 'Dependency endpoints must belong to the same project.', 1; IF @AssetDependencyID IS NULL OR @AssetDependencyID = 0 BEGIN INSERT dbo.AssetDependencies (SourceAssetID, TargetAssetID, AssetDependencyTypeID, SourceRequiredStateID, TargetBlockedStateID, Description) VALUES (@SourceAssetID, @TargetAssetID, @AssetDependencyTypeID, @SourceRequiredStateID, @TargetBlockedStateID, @Description); SET @AssetDependencyID = CAST(SCOPE_IDENTITY() AS int); END ELSE BEGIN UPDATE dbo.AssetDependencies SET AssetDependencyTypeID = @AssetDependencyTypeID, SourceRequiredStateID = @SourceRequiredStateID, TargetBlockedStateID = @TargetBlockedStateID, Description = @Description, UpdatedDate = SYSUTCDATETIME() WHERE AssetDependencyID = @AssetDependencyID AND IsArchived = 0 AND SourceAssetID = @SourceAssetID AND TargetAssetID = @TargetAssetID; IF @@ROWCOUNT = 0 SET @AssetDependencyID = 0; END SELECT @AssetDependencyID; END; GO CREATE OR ALTER PROCEDURE dbo.AssetEvent_ListByAsset @StoryAssetID int AS BEGIN SET NOCOUNT ON; SELECT ae.AssetEventID, ae.StoryAssetID, sa.ProjectID, sa.AssetName, sa.AssetKindID, ak.KindName, ae.SceneID, ae.AssetEventTypeID, aet.TypeName AS AssetEventTypeName, aet.MarkerText, ae.FromStateID, fs.StateName AS FromStateName, ae.ToStateID, ts.StateName AS ToStateName, ae.EventTitle, ae.EventDescription, ae.CreatedDate, ae.UpdatedDate, b.BookID, b.BookNumber, b.BookTitle, b.Subtitle AS BookSubtitle, c.ChapterID, c.ChapterNumber, s.SceneNumber, s.SceneTitle, b.SortOrder AS BookSortOrder, c.SortOrder AS ChapterSortOrder, s.SortOrder AS SceneSortOrder FROM dbo.AssetEvents ae INNER JOIN dbo.StoryAssets sa ON sa.StoryAssetID = ae.StoryAssetID INNER JOIN dbo.AssetKinds ak ON ak.AssetKindID = sa.AssetKindID INNER JOIN dbo.AssetEventTypes aet ON aet.AssetEventTypeID = ae.AssetEventTypeID INNER JOIN dbo.Scenes s ON s.SceneID = ae.SceneID INNER JOIN dbo.Chapters c ON c.ChapterID = s.ChapterID INNER JOIN dbo.Books b ON b.BookID = c.BookID LEFT JOIN dbo.AssetStates fs ON fs.AssetStateID = ae.FromStateID LEFT JOIN dbo.AssetStates ts ON ts.AssetStateID = ae.ToStateID WHERE ae.StoryAssetID = @StoryAssetID ORDER BY b.SortOrder, c.SortOrder, s.SortOrder, ae.AssetEventID; END; GO CREATE OR ALTER PROCEDURE dbo.AssetCustodyEvent_ListByAsset @StoryAssetID int AS BEGIN SET NOCOUNT ON; SELECT ace.AssetCustodyEventID, ace.StoryAssetID, sa.AssetName, ace.SceneID, ace.AssetCustodyEventTypeID, acet.TypeName AS CustodyEventTypeName, ace.Description, STRING_AGG(CONCAT(aec.CharacterNameText, N' (', cr.RoleName, N')'), N', ') AS CustodianSummary, ace.CreatedDate, ace.UpdatedDate, b.BookID, b.BookNumber, b.BookTitle, b.Subtitle AS BookSubtitle, c.ChapterID, c.ChapterNumber, s.SceneNumber, s.SceneTitle, b.SortOrder AS BookSortOrder, c.SortOrder AS ChapterSortOrder, s.SortOrder AS SceneSortOrder FROM dbo.AssetCustodyEvents ace INNER JOIN dbo.StoryAssets sa ON sa.StoryAssetID = ace.StoryAssetID INNER JOIN dbo.AssetCustodyEventTypes acet ON acet.AssetCustodyEventTypeID = ace.AssetCustodyEventTypeID INNER JOIN dbo.Scenes s ON s.SceneID = ace.SceneID INNER JOIN dbo.Chapters c ON c.ChapterID = s.ChapterID INNER JOIN dbo.Books b ON b.BookID = c.BookID LEFT JOIN dbo.AssetCustodyEventCharacters aec ON aec.AssetCustodyEventID = ace.AssetCustodyEventID LEFT JOIN dbo.CustodyRoles cr ON cr.CustodyRoleID = aec.CustodyRoleID WHERE ace.StoryAssetID = @StoryAssetID GROUP BY ace.AssetCustodyEventID, ace.StoryAssetID, sa.AssetName, ace.SceneID, ace.AssetCustodyEventTypeID, acet.TypeName, ace.Description, ace.CreatedDate, ace.UpdatedDate, b.BookID, b.BookNumber, b.BookTitle, b.Subtitle, c.ChapterID, c.ChapterNumber, s.SceneNumber, s.SceneTitle, b.SortOrder, c.SortOrder, s.SortOrder ORDER BY b.SortOrder, c.SortOrder, s.SortOrder, ace.AssetCustodyEventID; END; GO CREATE OR ALTER PROCEDURE dbo.SceneAssetLocation_ListByAsset @StoryAssetID int AS BEGIN SET NOCOUNT ON; SELECT sal.SceneID, b.BookID, b.BookNumber, b.BookTitle, b.Subtitle AS BookSubtitle, c.ChapterID, c.ChapterNumber, s.SceneNumber, s.SceneTitle, b.SortOrder AS BookSortOrder, c.SortOrder AS ChapterSortOrder, s.SortOrder AS SceneSortOrder, N'Location' AS Title, location.LocationPath AS Detail, sal.Description FROM dbo.SceneAssetLocations sal INNER JOIN dbo.Scenes s ON s.SceneID = sal.SceneID INNER JOIN dbo.Chapters c ON c.ChapterID = s.ChapterID INNER JOIN dbo.Books b ON b.BookID = c.BookID LEFT JOIN dbo.LocationPaths location ON location.LocationID = sal.LocationID WHERE sal.StoryAssetID = @StoryAssetID ORDER BY b.SortOrder, c.SortOrder, s.SortOrder, sal.SceneAssetLocationID; END; GO