mirror of
https://github.com/hargata/lubelog.git
synced 2025-12-11 04:37:03 -06:00
112 lines
4.2 KiB
C#
112 lines
4.2 KiB
C#
using CarCareTracker.External.Interfaces;
|
|
using CarCareTracker.Models;
|
|
using Npgsql;
|
|
using System.Text.Json;
|
|
|
|
namespace CarCareTracker.External.Implementations
|
|
{
|
|
public class PGUserConfigDataAccess: IUserConfigDataAccess
|
|
{
|
|
private NpgsqlConnection pgDataSource;
|
|
private readonly ILogger<PGUserConfigDataAccess> _logger;
|
|
private static string tableName = "userconfigrecords";
|
|
public PGUserConfigDataAccess(IConfiguration config, ILogger<PGUserConfigDataAccess> logger)
|
|
{
|
|
pgDataSource = new NpgsqlConnection(config["POSTGRES_CONNECTION"]);
|
|
_logger = logger;
|
|
try
|
|
{
|
|
if (pgDataSource.State == System.Data.ConnectionState.Closed)
|
|
{
|
|
pgDataSource.Open();
|
|
}
|
|
//create table if not exist.
|
|
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT primary key, data jsonb not null)";
|
|
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
|
{
|
|
ctext.ExecuteNonQuery();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex.Message);
|
|
}
|
|
}
|
|
public UserConfigData GetUserConfig(int userId)
|
|
{
|
|
try
|
|
{
|
|
string cmd = $"SELECT data FROM app.{tableName} WHERE id = @id";
|
|
UserConfigData result = null;
|
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
|
{
|
|
ctext.Parameters.AddWithValue("id", userId);
|
|
using (NpgsqlDataReader reader = ctext.ExecuteReader())
|
|
while (reader.Read())
|
|
{
|
|
UserConfigData userConfig = JsonSerializer.Deserialize<UserConfigData>(reader["data"] as string);
|
|
result = userConfig;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex.Message);
|
|
return new UserConfigData();
|
|
}
|
|
}
|
|
public bool SaveUserConfig(UserConfigData userConfigData)
|
|
{
|
|
var existingRecord = GetUserConfig(userConfigData.Id);
|
|
try
|
|
{
|
|
if (existingRecord == null)
|
|
{
|
|
string cmd = $"INSERT INTO app.{tableName} (id, data) VALUES(@id, CAST(@data AS jsonb))";
|
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
|
{
|
|
var serializedData = JsonSerializer.Serialize(userConfigData);
|
|
ctext.Parameters.AddWithValue("id", userConfigData.Id);
|
|
ctext.Parameters.AddWithValue("data", serializedData);
|
|
return ctext.ExecuteNonQuery() > 0;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
string cmd = $"UPDATE app.{tableName} SET data = CAST(@data AS jsonb) WHERE id = @id";
|
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
|
{
|
|
var serializedData = JsonSerializer.Serialize(userConfigData);
|
|
ctext.Parameters.AddWithValue("id", userConfigData.Id);
|
|
ctext.Parameters.AddWithValue("data", serializedData);
|
|
return ctext.ExecuteNonQuery() > 0;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
public bool DeleteUserConfig(int userId)
|
|
{
|
|
try
|
|
{
|
|
string cmd = $"DELETE FROM app.{tableName} WHERE id = @id";
|
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
|
{
|
|
ctext.Parameters.AddWithValue("id", userId);
|
|
return ctext.ExecuteNonQuery() > 0;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|