feat(FinalizeDocument): aus CommonJobs kopiert, mit einfachen Fehlerbehebungen unter Verwendung von Copilot
- Programmiersprache von VSC zu C# geändert - Framework von .NET Framework zu .NET geändert
This commit is contained in:
63
EnvelopeGenerator.ServiceHost/Jobs/Infrastructure/Base.cs
Normal file
63
EnvelopeGenerator.ServiceHost/Jobs/Infrastructure/Base.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using System.Data;
|
||||
using DigitalData.Modules.Logging;
|
||||
|
||||
namespace DigitalData.Modules.Base;
|
||||
|
||||
public abstract class BaseClass
|
||||
{
|
||||
protected BaseClass(LogConfig logConfig)
|
||||
{
|
||||
LogConfig = logConfig;
|
||||
Logger = logConfig.GetLogger();
|
||||
}
|
||||
|
||||
protected LogConfig LogConfig { get; }
|
||||
protected Logger Logger { get; }
|
||||
}
|
||||
|
||||
public static class ObjectEx
|
||||
{
|
||||
public static T ToEnum<T>(object value) where T : struct, Enum
|
||||
{
|
||||
if (value is T enumValue)
|
||||
{
|
||||
return enumValue;
|
||||
}
|
||||
|
||||
if (value is string stringValue && Enum.TryParse<T>(stringValue, true, out var parsed))
|
||||
{
|
||||
return parsed;
|
||||
}
|
||||
|
||||
if (int.TryParse(Convert.ToString(value), out var intValue))
|
||||
{
|
||||
return (T)Enum.ToObject(typeof(T), intValue);
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
public static class DataRowExtensions
|
||||
{
|
||||
public static T ItemEx<T>(this DataRow row, string columnName, T defaultValue)
|
||||
{
|
||||
if (!row.Table.Columns.Contains(columnName))
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
var value = row[columnName];
|
||||
if (value is DBNull or null)
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
return (T)Convert.ChangeType(value, typeof(T));
|
||||
}
|
||||
|
||||
public static string ItemEx(this DataRow row, string columnName, string defaultValue)
|
||||
{
|
||||
return ItemEx<string>(row, columnName, defaultValue);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
using System.Data;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using DigitalData.Modules.Logging;
|
||||
|
||||
namespace DigitalData.Modules.Database;
|
||||
|
||||
public class MSSQLServer
|
||||
{
|
||||
private readonly LogConfig _logConfig;
|
||||
private readonly string _connectionString;
|
||||
|
||||
public MSSQLServer(LogConfig logConfig, string connectionString)
|
||||
{
|
||||
_logConfig = logConfig;
|
||||
_connectionString = connectionString;
|
||||
}
|
||||
|
||||
public static string DecryptConnectionString(string connectionString) => connectionString;
|
||||
|
||||
public SqlConnection GetConnection
|
||||
{
|
||||
get
|
||||
{
|
||||
var connection = new SqlConnection(_connectionString);
|
||||
if (connection.State != ConnectionState.Open)
|
||||
{
|
||||
connection.Open();
|
||||
}
|
||||
|
||||
return connection;
|
||||
}
|
||||
}
|
||||
|
||||
public DataTable GetDatatable(string sql)
|
||||
{
|
||||
using var connection = new SqlConnection(_connectionString);
|
||||
using var command = new SqlCommand(sql, connection);
|
||||
using var adapter = new SqlDataAdapter(command);
|
||||
var table = new DataTable();
|
||||
adapter.Fill(table);
|
||||
return table;
|
||||
}
|
||||
|
||||
public object? GetScalarValue(string sql)
|
||||
{
|
||||
using var connection = new SqlConnection(_connectionString);
|
||||
using var command = new SqlCommand(sql, connection);
|
||||
connection.Open();
|
||||
return command.ExecuteScalar();
|
||||
}
|
||||
|
||||
public bool ExecuteNonQuery(string sql)
|
||||
{
|
||||
using var connection = new SqlConnection(_connectionString);
|
||||
using var command = new SqlCommand(sql, connection);
|
||||
connection.Open();
|
||||
return command.ExecuteNonQuery() > 0;
|
||||
}
|
||||
|
||||
public bool ExecuteNonQuery(SqlCommand command)
|
||||
{
|
||||
using var connection = new SqlConnection(_connectionString);
|
||||
command.Connection = connection;
|
||||
connection.Open();
|
||||
return command.ExecuteNonQuery() > 0;
|
||||
}
|
||||
}
|
||||
26
EnvelopeGenerator.ServiceHost/Jobs/Infrastructure/Logging.cs
Normal file
26
EnvelopeGenerator.ServiceHost/Jobs/Infrastructure/Logging.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System.Globalization;
|
||||
|
||||
namespace DigitalData.Modules.Logging;
|
||||
|
||||
public class LogConfig
|
||||
{
|
||||
public bool Debug { get; set; }
|
||||
|
||||
public Logger GetLogger() => new();
|
||||
}
|
||||
|
||||
public class Logger
|
||||
{
|
||||
public void Debug(string message, params object?[] args) => Write("DEBUG", message, args);
|
||||
public void Info(string message, params object?[] args) => Write("INFO", message, args);
|
||||
public void Warn(string message, params object?[] args) => Write("WARN", message, args);
|
||||
public void Warn(Exception exception, string message, params object?[] args) => Write("WARN", message + " " + exception.Message, args);
|
||||
public void Error(Exception exception) => Write("ERROR", exception.Message, Array.Empty<object?>());
|
||||
public void Error(Exception exception, string message, params object?[] args) => Write("ERROR", message + " " + exception.Message, args);
|
||||
|
||||
private static void Write(string level, string message, params object?[] args)
|
||||
{
|
||||
var formatted = args.Length > 0 ? string.Format(CultureInfo.InvariantCulture, message, args) : message;
|
||||
Console.WriteLine($"[{level}] {formatted}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user