SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE OR ALTER VIEW [dbo].[VWREC_ACTION]
AS
SELECT TOP (100) PERCENT
			[CFG_ACTION].[GUID]						as 'ACTION_GUID',
			[CFG_ACTION].[PROFILE_ID]				as 'PROFILE_ID',
			[CFG_PROFILE].[PROFILE_NAME]			as 'PROFILE_NAME',
			[CFG_PROFILE].[TYPE_ID]					as 'PROFILE_TYPE_ID',
			CASE
				WHEN [CFG_PROFILE].[TYPE_ID] = 1 THEN 'HTTP'
				WHEN [CFG_PROFILE].[TYPE_ID] = 2 THEN 'HTTPS'
				ELSE 'INVALID'
			END										as 'PROFILE_TYPE',
			[CFG_ACTION].[SEQUENCE]					as 'SEQUENCE',
			[CFG_ACTION].[ENDPOINT_ID]				as 'ENDPOINT_ID',
			[CFG_ENDPOINT].[URI]					as 'ENDPOINT_URI',
			[CFG_ACTION].[ENDPOINT_AUTH_ID]			as 'ENDPOINT_AUTH_ID',
			[CFG_ENDPOINT_AUTH].[TYPE_ID]			as 'ENDPOINT_AUTH_TYPE_ID',
			CASE
				WHEN [CFG_ENDPOINT_AUTH].[TYPE_ID] = 0 THEN 'No Auth'
				WHEN [CFG_ENDPOINT_AUTH].[TYPE_ID] = 1 THEN 'API Key'
				WHEN [CFG_ENDPOINT_AUTH].[TYPE_ID] = 2 THEN 'Bearer Token'
				WHEN [CFG_ENDPOINT_AUTH].[TYPE_ID] = 3 THEN 'JWT Bearer'
				WHEN [CFG_ENDPOINT_AUTH].[TYPE_ID] = 4 THEN 'Basic Auth'
				WHEN [CFG_ENDPOINT_AUTH].[TYPE_ID] = 5 THEN 'Digest Auth'
				WHEN [CFG_ENDPOINT_AUTH].[TYPE_ID] = 6 THEN 'OAuth 1.0'
				WHEN [CFG_ENDPOINT_AUTH].[TYPE_ID] = 7 THEN 'OAuth 2.0'
				WHEN [CFG_ENDPOINT_AUTH].[TYPE_ID] = 8 THEN 'AWS Signature'
				WHEN [CFG_ENDPOINT_AUTH].[TYPE_ID] = 9 THEN 'NTLM Auth'
				ELSE 'INVALID'
			END										as 'ENDPOINT_AUTH_TYPE',
			[CFG_ENDPOINT_AUTH].[API_KEY]			as 'ENDPOINT_AUTH_API_KEY',
			[CFG_ENDPOINT_AUTH].[API_VALUE]			as 'ENDPOINT_AUTH_API_VALUE',
			[CFG_ENDPOINT_AUTH].[API_KEY_ADD_TO_ID]	as 'ENDPOINT_AUTH_API_KEY_ADD_TO_ID',
			CASE
				WHEN [CFG_ENDPOINT_AUTH].[API_KEY_ADD_TO_ID] = 0 THEN 'HEADER'
				WHEN [CFG_ENDPOINT_AUTH].[API_KEY_ADD_TO_ID] = 1 THEN 'QUERY'
				ELSE NULL
			END										as 'ENDPOINT_AUTH_API_KEY_ADD_TO',
			[CFG_ENDPOINT_AUTH].[TOKEN]				as 'ENDPOINT_AUTH_TOKEN',
			[CFG_ENDPOINT_AUTH].[USERNAME]			as 'ENDPOINT_AUTH_USERNAME',
			[CFG_ENDPOINT_AUTH].[PASSWORD]			as 'ENDPOINT_AUTH_PASSWORD',
			[CFG_ENDPOINT_AUTH].[DOMAIN]			as 'ENDPOINT_AUTH_DOMAIN',
			[CFG_ENDPOINT_AUTH].[WORKSTATION]		as 'ENDPOINT_AUTH_WORKSTATION',
			[CFG_ACTION].[ENDPOINT_PARAMS_ID]		as 'ENDPOINT_PARAMS_ID',
			[CFG_ACTION].[SQL_CONNECTION_ID]		as 'SQL_CONNECTION_ID',
			[CFG_SQL_CONNECTION].[SERVER]			as 'SQL_CONNECTION_SERVER',
			[CFG_SQL_CONNECTION].[DATENBANK]		as 'SQL_CONNECTION_DB',
			[CFG_SQL_CONNECTION].[USERNAME]			as 'SQL_CONNECTION_USERNAME',
			[CFG_SQL_CONNECTION].[PASSWORD]			as 'SQL_CONNECTION_PASSWORD',
			[CFG_ACTION].[TYPE_ID]					as 'REST_TYPE_ID',
			CASE
				WHEN [CFG_ACTION].[TYPE_ID] = 0 THEN 'NONE'
				WHEN [CFG_ACTION].[TYPE_ID] = 1 THEN 'GET'
				WHEN [CFG_ACTION].[TYPE_ID] = 2 THEN 'POST'
				WHEN [CFG_ACTION].[TYPE_ID] = 3 THEN 'PUT'
				WHEN [CFG_ACTION].[TYPE_ID] = 4 THEN 'PATCH'
				WHEN [CFG_ACTION].[TYPE_ID] = 5 THEN 'DELETE'
				WHEN [CFG_ACTION].[TYPE_ID] = 6 THEN 'HEAD'
				WHEN [CFG_ACTION].[TYPE_ID] = 7 THEN 'OPTIONS'
				WHEN [CFG_ACTION].[TYPE_ID] = 8 THEN 'CONNECT'
				WHEN [CFG_ACTION].[TYPE_ID] = 9 THEN 'TRACE'
				ELSE 'INVALID'
			END										as 'REST_TYPE',
			[CFG_ACTION].[PREPROCESSING_QUERY]		as 'PREPROCESSING_QUERY',
			[CFG_ACTION].[HEADER_QUERY]				as 'HEADER_QUERY',
			[CFG_ACTION].[BODY_QUERY]				as 'BODY_QUERY',
			[CFG_ACTION].[POSTPROCESSING_QUERY]		as 'POSTPROCESSING_QUERY',
			[CFG_ACTION].[ERROR_ACTION_ID]			as 'ERROR_ACTION_ID',
			CASE 
				WHEN [CFG_ACTION].[ERROR_ACTION_ID] = 0 THEN 'STOP'
				WHEN [CFG_ACTION].[ERROR_ACTION_ID] = 1 THEN 'CONTINUE'
				ELSE 'INVALID'
			END										as 'ERROR_ACTION'

 FROM       [TBREC_CFG_ACTION]        as [CFG_ACTION]         WITH (NOLOCK)
 INNER JOIN [TBREC_CFG_PROFILE]       as [CFG_PROFILE]        WITH (NOLOCK) ON [CFG_ACTION].[PROFILE_ID]        = [CFG_PROFILE].[GUID]
 INNER JOIN [TBREC_CFG_ENDPOINT]      as [CFG_ENDPOINT]       WITH (NOLOCK) ON [CFG_ACTION].[ENDPOINT_ID]       = [CFG_ENDPOINT].[GUID]
 LEFT  JOIN [TBREC_CFG_ENDPOINT_AUTH] as [CFG_ENDPOINT_AUTH]  WITH (NOLOCK) ON [CFG_ACTION].[ENDPOINT_AUTH_ID]  = [CFG_ENDPOINT_AUTH].[GUID]
 LEFT  JOIN [TBDD_CONNECTION]         as [CFG_SQL_CONNECTION] WITH (NOLOCK) ON [CFG_ACTION].[SQL_CONNECTION_ID] = [CFG_SQL_CONNECTION].[GUID]

 WHERE		[CFG_ACTION].[ACTIVE]        = 1
 AND		[CFG_PROFILE].[ACTIVE]       = 1
 AND		[CFG_ENDPOINT].[ACTIVE]      = 1
 AND		([CFG_ACTION].[ENDPOINT_AUTH_ID] IS NULL OR [CFG_ENDPOINT_AUTH].[ACTIVE] = 1)

ORDER BY 	[CFG_ACTION].[PROFILE_ID] ASC, [CFG_ACTION].[SEQUENCE] ASC, [CFG_ACTION].[GUID] ASC;

GO

--------------------------------------------------------------------------------------

CREATE OR ALTER TRIGGER [dbo].[VWREC_ACTION_IO_INS]
ON [dbo].[VWREC_ACTION]
INSTEAD OF INSERT
AS
BEGIN
	SET NOCOUNT ON;
	THROW 51000, 'DML is not allowed on view [dbo].[VWREC_ACTION]. Use these Procedure instead: [PRREC_INSERT_OBJECT]', 1;
END;
GO

--------------------------------------------------------------------------------------

CREATE OR ALTER TRIGGER [dbo].[VWREC_ACTION_IO_UPD]
ON [dbo].[VWREC_ACTION]
INSTEAD OF UPDATE
AS
BEGIN
	SET NOCOUNT ON;
	THROW 52000, 'DML is not allowed on view [dbo].[VWREC_ACTION]. Use these Procedure instead: [PRREC_UPDATE_OBJECT]', 1;
END;
GO

--------------------------------------------------------------------------------------

CREATE OR ALTER TRIGGER [dbo].[VWREC_ACTION_IO_DEL]
ON [dbo].[VWREC_ACTION]
INSTEAD OF DELETE
AS
BEGIN
	SET NOCOUNT ON;
	THROW 53000, 'DML is not allowed on view [dbo].[VWREC_ACTION]. Use these Procedure instead: [PRREC_DELETE_OBJECT]', 1;
END;
GO

--------------------------------------------------------------------------------------