Files
DP44/DataPRO/DbAPI/Groups/GroupHardware.cs
2026-04-17 14:55:32 -04:00

177 lines
7.1 KiB
C#

using DbAPI.Connections;
using DbAPI.Errors;
using DTS.Common.Interface.Database;
using System;
using System.Data;
using System.Data.SqlClient;
using DTS.Common.Interface.Groups.GroupList;
using System.Collections.Generic;
using DTS.Common.Interface.Groups;
using DTS.Common.Classes.Groups;
using DbAPI.Logging;
using System.Diagnostics;
namespace DbAPI.GroupHardware
{
/// <summary>
/// Handles GroupHardware functions
/// </summary>
internal class GroupHardware : IGroupHardware
{
public ulong GroupHardwareInsert(IUserDbRecord user,
IConnectionDetails connection,
GroupHardwareDbRecord groupHardwareDbRecord,
out int newId,
out string errorMessageString)
{
newId = -1;
errorMessageString = string.Empty;
if (!DbAPI.Connections.IsUserLoggedIn(user, connection))
{
return ErrorCodes.ERROR_ACCESS_DENIED;
}
var ret = ConnectionManager.GetSqlCommand(connection, out var cmd, "sp_GroupHardwareInsert");
if (ret != ErrorCodes.ERROR_SUCCESS)
{
return ret;
}
try
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@GroupId", SqlDbType.Int) { Value = groupHardwareDbRecord.GroupId });
cmd.Parameters.Add(new SqlParameter("@DASId", SqlDbType.Int) { Value = groupHardwareDbRecord.DASId });
var newGroupHardwareIdParam = new SqlParameter("@new_id", SqlDbType.Int) { Direction = ParameterDirection.Output };
cmd.Parameters.Add(newGroupHardwareIdParam);
var errorNumber = new SqlParameter("@errorNumber", SqlDbType.Int) { Direction = ParameterDirection.Output };
cmd.Parameters.Add(errorNumber);
var errorMessage = new SqlParameter("@errorMessage", SqlDbType.NVarChar, 255) { Direction = ParameterDirection.Output };
cmd.Parameters.Add(errorMessage);
cmd.ExecuteNonQuery();
if (null != errorNumber.Value)
{
if (Convert.ToInt32(errorNumber.Value) != 0)
{
LogManager.Log(TraceEventType.Error, LogManager.LogEvents.TestSetups, $"GroupHardwareInsert failed: {errorNumber.Value} {errorMessage.Value}");
errorMessageString = errorMessage.Value.ToString();
return Convert.ToUInt32(errorNumber.Value);
}
}
newId = Convert.ToInt32(newGroupHardwareIdParam.Value);
return ErrorCodes.ERROR_SUCCESS;
}
catch (Exception ex)
{
LogManager.Log(TraceEventType.Error, LogManager.LogEvents.TestSetups, $"GroupHardwareInsert failed: {ex.Message}");
return ErrorCodes.ERROR_UNKNOWN;
}
finally
{
cmd.Connection.Dispose();
cmd.Dispose();
}
}
public ulong GroupHardwareGet(IUserDbRecord user,
IConnectionDetails connection,
int? groupId,
string serialNumber,
bool? embedded,
out GroupHardwareDbRecord[] groupHardwareRecords)
{
groupHardwareRecords = null;
var list = new List<GroupHardwareDbRecord>();
if (!DbAPI.Connections.IsUserLoggedIn(user, connection))
{
return ErrorCodes.ERROR_ACCESS_DENIED;
}
var ret = ConnectionManager.GetSqlCommand(connection, out var cmd, "sp_GroupHardwareGet");
if (ret != ErrorCodes.ERROR_SUCCESS)
{
return ret;
}
try
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@GroupId", SqlDbType.Int) { Value = groupId });
cmd.Parameters.Add(new SqlParameter("@SerialNumber", SqlDbType.NVarChar) { Value = serialNumber });
cmd.Parameters.Add(new SqlParameter("@Embedded", SqlDbType.Bit) { Value = embedded });
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
list.Add(new GroupHardwareDbRecord(reader));
}
groupHardwareRecords = list.ToArray();
}
return ErrorCodes.ERROR_SUCCESS;
}
catch (Exception ex)
{
LogManager.Log(TraceEventType.Error, LogManager.LogEvents.TestSetups, $"GroupsGet failed - {ex.Message}");
return ErrorCodes.ERROR_UNKNOWN;
}
finally
{
cmd.Connection.Dispose();
}
}
public ulong GroupHardwareDelete(IUserDbRecord user,
IConnectionDetails connection,
int? groupId,
int? dasId,
out string errorString)
{
errorString = string.Empty;
if (!DbAPI.Connections.IsUserLoggedIn(user, connection))
{
return ErrorCodes.ERROR_ACCESS_DENIED;
}
var ret = ConnectionManager.GetSqlCommand(connection, out var cmd, "sp_GroupHardwareDelete");
if (ret != ErrorCodes.ERROR_SUCCESS)
{
return ret;
}
var errorNumberULong = ErrorCodes.ERROR_SUCCESS;
try
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@GroupId", SqlDbType.Int) { Value = groupId });
cmd.Parameters.Add(new SqlParameter("@DASId", SqlDbType.Int) { Value = dasId });
var errorNumber = new SqlParameter("@errorNumber", SqlDbType.Int) { Direction = ParameterDirection.Output };
cmd.Parameters.Add(errorNumber);
var errorMessage = new SqlParameter("@errorMessage", SqlDbType.NVarChar, 255) { Direction = ParameterDirection.Output };
cmd.Parameters.Add(errorMessage);
cmd.ExecuteNonQuery();
if (null != errorNumber.Value)
{
if (Convert.ToInt32(errorNumber.Value) != 0)
{
errorNumberULong = Convert.ToUInt64(errorNumber.Value);
throw new Exception((string)errorMessage.Value);
}
}
}
catch (Exception ex)
{
//Concatenate any error string returned from the stored procedure call
errorString = $"{ex.Message}; {errorString}";
return ErrorCodes.ERROR_UNKNOWN;
}
finally
{
cmd.Connection.Dispose();
}
return errorNumberULong;
}
}
}