mirror of
https://github.com/stokebob/bnhtrade.git
synced 2026-05-18 19:48:23 +00:00
136 lines
4.8 KiB
C#
136 lines
4.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Microsoft.Data.SqlClient;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace bnhtrade.Core.Data.Database.Log
|
|
{
|
|
public class DateTimeLog : Connection
|
|
{
|
|
public DateTimeLog()
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the Date and Time by a unique string.
|
|
/// </summary>
|
|
/// <param name="logDateTimeId">Unique string</param>
|
|
/// <returns>UTC DateTime</returns>
|
|
public DateTime GetDateTimeUtc(string logDateTimeId)
|
|
{
|
|
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
|
|
{
|
|
conn.Open();
|
|
|
|
using (SqlCommand cmd = new SqlCommand(@"
|
|
SELECT
|
|
DateTimeUtc
|
|
FROM
|
|
tblLogDateTime
|
|
WHERE
|
|
LogDateTimeID = @logDateTimeID
|
|
", conn))
|
|
{
|
|
cmd.Parameters.AddWithValue("@logDateTimeID", logDateTimeId);
|
|
|
|
object obj = cmd.ExecuteScalar();
|
|
|
|
if (obj == null || obj == DBNull.Value)
|
|
{
|
|
throw new Exception("Id not found.");
|
|
}
|
|
else
|
|
{
|
|
DateTime returnDt = (DateTime)obj;
|
|
return DateTime.SpecifyKind(returnDt, DateTimeKind.Utc);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SetDateTimeUtc(string logDateTimeID, DateTime utcDateTime)
|
|
{
|
|
if (utcDateTime.Kind != DateTimeKind.Utc)
|
|
{
|
|
throw new Exception("Datetime kind is not set to UTC");
|
|
}
|
|
|
|
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
|
|
{
|
|
conn.Open();
|
|
|
|
using (SqlCommand cmd = new SqlCommand(@"
|
|
UPDATE tblLogDateTime
|
|
SET
|
|
DateTimeUtc = @utcDateTime
|
|
,RecordModified = @recordModified
|
|
WHERE
|
|
LogDateTimeID = @logDateTimeID
|
|
", conn))
|
|
{
|
|
cmd.Parameters.AddWithValue("@utcDateTime", utcDateTime.ToUniversalTime());
|
|
cmd.Parameters.AddWithValue("@recordModified", DateTime.UtcNow.ToUniversalTime());
|
|
cmd.Parameters.AddWithValue("@logDateTimeID", logDateTimeID);
|
|
|
|
int count = cmd.ExecuteNonQuery();
|
|
|
|
if (count == 0)
|
|
{
|
|
throw new Exception("Error, failed to update record.");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void NewDateTimeUtc(string LogDateTimeId, DateTime utcDateTime, string comments = null)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(LogDateTimeId))
|
|
{ throw new Exception("Invalid LogDateTimeID"); }
|
|
|
|
if (utcDateTime.Kind != DateTimeKind.Utc)
|
|
{
|
|
throw new Exception("Datetime kind is not set to UTC");
|
|
}
|
|
|
|
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
|
|
{
|
|
conn.Open();
|
|
|
|
using (SqlCommand cmd = new SqlCommand(@"
|
|
INSERT INTO tblLogDateTime (
|
|
LogDateTimeID
|
|
,DateTimeUtc
|
|
,Comments
|
|
,RecordModified
|
|
,RecordCreated
|
|
)
|
|
VALUES (
|
|
@logDateTimeId
|
|
,@utcDateTime
|
|
,@comments
|
|
,@recordModified
|
|
,@recordCreated
|
|
)
|
|
", conn))
|
|
{
|
|
var nowTime = DateTime.UtcNow;
|
|
cmd.Parameters.AddWithValue("@logDateTimeId", LogDateTimeId);
|
|
cmd.Parameters.AddWithValue("@utcDateTime", utcDateTime.ToUniversalTime());
|
|
if (string.IsNullOrWhiteSpace(comments)) { cmd.Parameters.AddWithValue("@comments", DBNull.Value); }
|
|
else { cmd.Parameters.AddWithValue("@comments", comments); }
|
|
cmd.Parameters.AddWithValue("@recordModified", nowTime.ToUniversalTime());
|
|
cmd.Parameters.AddWithValue("@recordCreated", nowTime.ToUniversalTime());
|
|
|
|
int count = cmd.ExecuteNonQuery();
|
|
|
|
if (count == 0)
|
|
{
|
|
throw new Exception("Error updating, no records where effected.");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |