-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapi_Custom_CreateSelection.sql
More file actions
88 lines (77 loc) · 2.78 KB
/
api_Custom_CreateSelection.sql
File metadata and controls
88 lines (77 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
-- =============================================
-- Ministry Platform Stored Procedure Install
-- Generated: 2026-03-03
-- =============================================
-- NOTE: Run this script against your Ministry Platform database
-- =============================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET NOCOUNT ON
GO
/****** Object: StoredProcedure [dbo].[api_Custom_CreateSelection] Script Date: 3/3/2026 ******/
DROP PROCEDURE IF EXISTS [dbo].[api_Custom_CreateSelection]
GO
-- =============================================
-- api_Custom_CreateSelection
-- =============================================
-- Description: Creates a new MP Selection with the given records for a user/page.
-- Last Modified: 3/3/2026
-- =============================================
CREATE PROCEDURE [dbo].[api_Custom_CreateSelection]
@DomainID INT,
@PageID INT,
@UserID INT,
@SelectionName NVARCHAR(255),
@RecordIDs NVARCHAR(MAX) -- comma-separated, e.g. '1001,1002,1003'
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO dp_Selections (Selection_Name, Page_ID, User_ID)
VALUES (@SelectionName, @PageID, @UserID);
DECLARE @SelectionID INT = SCOPE_IDENTITY();
INSERT INTO dp_Selected_Records (Selection_ID, Record_ID)
SELECT @SelectionID, CAST(LTRIM(RTRIM(x.value('.', 'VARCHAR(MAX)'))) AS INT)
FROM (
SELECT CAST('<r>' + REPLACE(@RecordIDs, ',', '</r><r>') + '</r>' AS XML)
) t(xml)
CROSS APPLY t.xml.nodes('/r') AS n(x)
WHERE LTRIM(RTRIM(x.value('.', 'VARCHAR(MAX)'))) != '';
SELECT
@SelectionID AS Selection_ID,
@SelectionName AS Selection_Name,
(SELECT COUNT(*) FROM dp_Selected_Records WHERE Selection_ID = @SelectionID) AS Record_Count;
END
GO
-- =============================================
-- SP MetaData Install
-- =============================================
DECLARE @spName NVARCHAR(128) = 'api_Custom_CreateSelection';
DECLARE @spDescription NVARCHAR(500) = 'Creates a new MP Selection with the given records for a user/page.';
IF NOT EXISTS (
SELECT API_Procedure_ID FROM dp_API_Procedures WHERE Procedure_Name = @spName
)
BEGIN
INSERT INTO dp_API_Procedures (Procedure_Name, Description)
VALUES (@spName, @spDescription);
END
-- Grant to Administrators Role
DECLARE @AdminRoleID INT = (
SELECT Role_ID FROM dp_Roles WHERE Role_Name = 'Administrators'
);
IF NOT EXISTS (
SELECT 1
FROM dp_Role_API_Procedures RP
INNER JOIN dp_API_Procedures AP ON AP.API_Procedure_ID = RP.API_Procedure_ID
WHERE AP.Procedure_Name = @spName AND RP.Role_ID = @AdminRoleID
)
BEGIN
INSERT INTO dp_Role_API_Procedures (Domain_ID, API_Procedure_ID, Role_ID)
VALUES (
1,
(SELECT API_Procedure_ID FROM dp_API_Procedures WHERE Procedure_Name = @spName),
@AdminRoleID
);
END
GO