Add InsertObjectProcedure MediatR command and handler

Introduces InsertObjectProcedure and its handler to support generic, parameterized insertion of various object types (ACTION, ENDPOINT, etc.) via a single stored procedure. The handler maps request properties to SQL parameters, executes the procedure, and returns the output GUID. This enables flexible and unified object creation through MediatR.
This commit is contained in:
2026-01-12 15:19:54 +01:00
parent 1a6eced316
commit 7bfb56b664

View File

@@ -1,4 +1,6 @@
using MediatR;
using DigitalData.Core.Abstraction.Application.Repository;
using MediatR;
using Microsoft.Data.SqlClient;
namespace ReC.Application.Common.Procedures;
@@ -8,4 +10,155 @@ public record InsertObjectProcedure : IRequest<long>
/// Target entity: ACTION, ENDPOINT, ENDPOINT_AUTH, ENDPOINT_PARAMS, PROFILE, RESULT
/// </summary>
public string Entity { get; set; } = "ACTION";
public string? AddedWho { get; set; }
public DateTime? AddedWhen { get; set; }
// ACTION
public long? ActionProfileId { get; set; }
public bool? ActionActive { get; set; }
public byte? ActionSequence { get; set; }
public long? ActionEndpointId { get; set; }
public long? ActionEndpointAuthId { get; set; }
public short? ActionEndpointParamsId { get; set; }
public short? ActionSqlConnectionId { get; set; }
public byte? ActionTypeId { get; set; }
public string? ActionPreSql { get; set; }
public string? ActionHeaderSql { get; set; }
public string? ActionBodySql { get; set; }
public string? ActionPostSql { get; set; }
public byte? ActionErrorActionId { get; set; }
// ENDPOINT
public bool? EndpointActive { get; set; }
public string? EndpointDescription { get; set; }
public string? EndpointUri { get; set; }
// ENDPOINT_AUTH
public bool? EndpointAuthActive { get; set; }
public string? EndpointAuthDescription { get; set; }
public byte? EndpointAuthTypeId { get; set; }
public string? EndpointAuthApiKey { get; set; }
public string? EndpointAuthApiValue { get; set; }
public bool? EndpointAuthApiKeyAddToId { get; set; }
public string? EndpointAuthToken { get; set; }
public string? EndpointAuthUsername { get; set; }
public string? EndpointAuthPassword { get; set; }
public string? EndpointAuthDomain { get; set; }
public string? EndpointAuthWorkstation { get; set; }
// PROFILE
public bool? ProfileActive { get; set; }
public byte? ProfileTypeId { get; set; }
public string? ProfileMandantor { get; set; }
public string? ProfileName { get; set; }
public string? ProfileDescription { get; set; }
public byte? ProfileLogLevelId { get; set; }
public short? ProfileLanguageId { get; set; }
// RESULT
public long? ResultActionId { get; set; }
public short? ResultStatusId { get; set; }
public string? ResultHeader { get; set; }
public string? ResultBody { get; set; }
// ENDPOINT_PARAMS
public bool? EndpointParamsActive { get; set; }
public string? EndpointParamsDescription { get; set; }
public short? EndpointParamsGroupId { get; set; }
public byte? EndpointParamsSequence { get; set; }
public string? EndpointParamsKey { get; set; }
public string? EndpointParamsValue { get; set; }
}
public class InsertObjectProcedureHandler(IRepository repo) : IRequestHandler<InsertObjectProcedure, long>
{
public async Task<long> Handle(InsertObjectProcedure request, CancellationToken cancel)
{
var parameters = new[]
{
new SqlParameter("@pENTITY", request.Entity ?? (object)DBNull.Value),
new SqlParameter("@pADDED_WHO", (object?)request.AddedWho ?? DBNull.Value),
new SqlParameter("@pADDED_WHEN", (object?)request.AddedWhen ?? DBNull.Value),
new SqlParameter("@pACTION_PROFILE_ID", (object?)request.ActionProfileId ?? DBNull.Value),
new SqlParameter("@pACTION_ACTIVE", (object?)request.ActionActive ?? DBNull.Value),
new SqlParameter("@pACTION_SEQUENCE", (object?)request.ActionSequence ?? DBNull.Value),
new SqlParameter("@pACTION_ENDPOINT_ID", (object?)request.ActionEndpointId ?? DBNull.Value),
new SqlParameter("@pACTION_ENDPOINT_AUTH_ID", (object?)request.ActionEndpointAuthId ?? DBNull.Value),
new SqlParameter("@pACTION_ENDPOINT_PARAMS_ID", (object?)request.ActionEndpointParamsId ?? DBNull.Value),
new SqlParameter("@pACTION_SQL_CONNECTION_ID", (object?)request.ActionSqlConnectionId ?? DBNull.Value),
new SqlParameter("@pACTION_TYPE_ID", (object?)request.ActionTypeId ?? DBNull.Value),
new SqlParameter("@pACTION_PRE_SQL", (object?)request.ActionPreSql ?? DBNull.Value),
new SqlParameter("@pACTION_HEADER_SQL", (object?)request.ActionHeaderSql ?? DBNull.Value),
new SqlParameter("@pACTION_BODY_SQL", (object?)request.ActionBodySql ?? DBNull.Value),
new SqlParameter("@pACTION_POST_SQL", (object?)request.ActionPostSql ?? DBNull.Value),
new SqlParameter("@pACTION_ERROR_ACTION_ID", (object?)request.ActionErrorActionId ?? DBNull.Value),
new SqlParameter("@pENDPOINT_ACTIVE", (object?)request.EndpointActive ?? DBNull.Value),
new SqlParameter("@pENDPOINT_DESCRIPTION", (object?)request.EndpointDescription ?? DBNull.Value),
new SqlParameter("@pENDPOINT_URI", (object?)request.EndpointUri ?? DBNull.Value),
new SqlParameter("@pENDPOINT_AUTH_ACTIVE", (object?)request.EndpointAuthActive ?? DBNull.Value),
new SqlParameter("@pENDPOINT_AUTH_DESCRIPTION", (object?)request.EndpointAuthDescription ?? DBNull.Value),
new SqlParameter("@pENDPOINT_AUTH_TYPE_ID", (object?)request.EndpointAuthTypeId ?? DBNull.Value),
new SqlParameter("@pENDPOINT_AUTH_API_KEY", (object?)request.EndpointAuthApiKey ?? DBNull.Value),
new SqlParameter("@pENDPOINT_AUTH_API_VALUE", (object?)request.EndpointAuthApiValue ?? DBNull.Value),
new SqlParameter("@pENDPOINT_AUTH_API_KEY_ADD_TO_ID", (object?)request.EndpointAuthApiKeyAddToId ?? DBNull.Value),
new SqlParameter("@pENDPOINT_AUTH_TOKEN", (object?)request.EndpointAuthToken ?? DBNull.Value),
new SqlParameter("@pENDPOINT_AUTH_USERNAME", (object?)request.EndpointAuthUsername ?? DBNull.Value),
new SqlParameter("@pENDPOINT_AUTH_PASSWORD", (object?)request.EndpointAuthPassword ?? DBNull.Value),
new SqlParameter("@pENDPOINT_AUTH_DOMAIN", (object?)request.EndpointAuthDomain ?? DBNull.Value),
new SqlParameter("@pENDPOINT_AUTH_WORKSTATION", (object?)request.EndpointAuthWorkstation ?? DBNull.Value),
new SqlParameter("@pPROFILE_ACTIVE", (object?)request.ProfileActive ?? DBNull.Value),
new SqlParameter("@pPROFILE_TYPE_ID", (object?)request.ProfileTypeId ?? DBNull.Value),
new SqlParameter("@pPROFILE_MANDANTOR", (object?)request.ProfileMandantor ?? DBNull.Value),
new SqlParameter("@pPROFILE_NAME", (object?)request.ProfileName ?? DBNull.Value),
new SqlParameter("@pPROFILE_DESCRIPTION", (object?)request.ProfileDescription ?? DBNull.Value),
new SqlParameter("@pPROFILE_LOG_LEVEL_ID", (object?)request.ProfileLogLevelId ?? DBNull.Value),
new SqlParameter("@pPROFILE_LANGUAGE_ID", (object?)request.ProfileLanguageId ?? DBNull.Value),
new SqlParameter("@pRESULT_ACTION_ID", (object?)request.ResultActionId ?? DBNull.Value),
new SqlParameter("@pRESULT_STATUS_ID", (object?)request.ResultStatusId ?? DBNull.Value),
new SqlParameter("@pRESULT_HEADER", (object?)request.ResultHeader ?? DBNull.Value),
new SqlParameter("@pRESULT_BODY", (object?)request.ResultBody ?? DBNull.Value),
new SqlParameter("@pENDPOINT_PARAMS_ACTIVE", (object?)request.EndpointParamsActive ?? DBNull.Value),
new SqlParameter("@pENDPOINT_PARAMS_DESCRIPTION", (object?)request.EndpointParamsDescription ?? DBNull.Value),
new SqlParameter("@pENDPOINT_PARAMS_GROUP_ID", (object?)request.EndpointParamsGroupId ?? DBNull.Value),
new SqlParameter("@pENDPOINT_PARAMS_SEQUENCE", (object?)request.EndpointParamsSequence ?? DBNull.Value),
new SqlParameter("@pENDPOINT_PARAMS_KEY", (object?)request.EndpointParamsKey ?? DBNull.Value),
new SqlParameter("@pENDPOINT_PARAMS_VALUE", (object?)request.EndpointParamsValue ?? DBNull.Value),
new SqlParameter
{
ParameterName = "@oGUID",
SqlDbType = System.Data.SqlDbType.BigInt,
Direction = System.Data.ParameterDirection.Output
}
};
await repo
.ExecuteQueryRawAsync(
"EXEC [dbo].[PRREC_INSERT_OBJECT] " +
"@pENTITY, @pADDED_WHO, @pADDED_WHEN, " +
"@pACTION_PROFILE_ID, @pACTION_ACTIVE, @pACTION_SEQUENCE, @pACTION_ENDPOINT_ID, @pACTION_ENDPOINT_AUTH_ID, @pACTION_ENDPOINT_PARAMS_ID, @pACTION_SQL_CONNECTION_ID, @pACTION_TYPE_ID, @pACTION_PRE_SQL, @pACTION_HEADER_SQL, @pACTION_BODY_SQL, @pACTION_POST_SQL, @pACTION_ERROR_ACTION_ID, " +
"@pENDPOINT_ACTIVE, @pENDPOINT_DESCRIPTION, @pENDPOINT_URI, " +
"@pENDPOINT_AUTH_ACTIVE, @pENDPOINT_AUTH_DESCRIPTION, @pENDPOINT_AUTH_TYPE_ID, @pENDPOINT_AUTH_API_KEY, @pENDPOINT_AUTH_API_VALUE, @pENDPOINT_AUTH_API_KEY_ADD_TO_ID, @pENDPOINT_AUTH_TOKEN, @pENDPOINT_AUTH_USERNAME, @pENDPOINT_AUTH_PASSWORD, @pENDPOINT_AUTH_DOMAIN, @pENDPOINT_AUTH_WORKSTATION, " +
"@pPROFILE_ACTIVE, @pPROFILE_TYPE_ID, @pPROFILE_MANDANTOR, @pPROFILE_NAME, @pPROFILE_DESCRIPTION, @pPROFILE_LOG_LEVEL_ID, @pPROFILE_LANGUAGE_ID, " +
"@pRESULT_ACTION_ID, @pRESULT_STATUS_ID, @pRESULT_HEADER, @pRESULT_BODY, " +
"@pENDPOINT_PARAMS_ACTIVE, @pENDPOINT_PARAMS_DESCRIPTION, @pENDPOINT_PARAMS_GROUP_ID, @pENDPOINT_PARAMS_SEQUENCE, @pENDPOINT_PARAMS_KEY, @pENDPOINT_PARAMS_VALUE, " +
"@oGUID OUTPUT",
parameters, cancel);
var guidParam = parameters.Last();
if (guidParam.Value != DBNull.Value)
if (long.TryParse(guidParam.Value.ToString(), out var guid))
return guid;
return 0;
}
}