added more generic data access classes for postgres.
This commit is contained in:
160
External/Implementations/Postgres/OdometerRecordDataAccess.cs
vendored
Normal file
160
External/Implementations/Postgres/OdometerRecordDataAccess.cs
vendored
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
using CarCareTracker.External.Interfaces;
|
||||||
|
using CarCareTracker.Models;
|
||||||
|
using Npgsql;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace CarCareTracker.External.Implementations
|
||||||
|
{
|
||||||
|
public class PGOdometerRecordDataAccess : IOdometerRecordDataAccess
|
||||||
|
{
|
||||||
|
private NpgsqlConnection pgDataSource;
|
||||||
|
private readonly ILogger<PGOdometerRecordDataAccess> _logger;
|
||||||
|
private static string tableName = "odometerrecords";
|
||||||
|
public PGOdometerRecordDataAccess(IConfiguration config, ILogger<PGOdometerRecordDataAccess> logger)
|
||||||
|
{
|
||||||
|
pgDataSource = new NpgsqlConnection(config["POSTGRES_CONNECTION"]);
|
||||||
|
_logger = logger;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
pgDataSource.Open();
|
||||||
|
//create table if not exist.
|
||||||
|
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED ALWAYS AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
|
||||||
|
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public List<OdometerRecord> GetOdometerRecordsByVehicleId(int vehicleId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string cmd = $"SELECT data FROM app.{tableName} WHERE vehicleId = @vehicleId";
|
||||||
|
var results = new List<OdometerRecord>();
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("vehicleId", vehicleId);
|
||||||
|
using (NpgsqlDataReader reader = ctext.ExecuteReader())
|
||||||
|
while (reader.Read())
|
||||||
|
{
|
||||||
|
OdometerRecord odometerRecord = JsonSerializer.Deserialize<OdometerRecord>(reader["data"] as string);
|
||||||
|
results.Add(odometerRecord);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
return new List<OdometerRecord>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public OdometerRecord GetOdometerRecordById(int odometerRecordId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string cmd = $"SELECT data FROM app.{tableName} WHERE id = @id";
|
||||||
|
var result = new OdometerRecord();
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", odometerRecordId);
|
||||||
|
using (NpgsqlDataReader reader = ctext.ExecuteReader())
|
||||||
|
while (reader.Read())
|
||||||
|
{
|
||||||
|
OdometerRecord odometerRecord = JsonSerializer.Deserialize<OdometerRecord>(reader["data"] as string);
|
||||||
|
result = odometerRecord;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
return new OdometerRecord();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public bool DeleteOdometerRecordById(int odometerRecordId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string cmd = $"DELETE FROM app.{tableName} WHERE id = @id";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", odometerRecordId);
|
||||||
|
return ctext.ExecuteNonQuery() > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public bool SaveOdometerRecordToVehicle(OdometerRecord odometerRecord)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (odometerRecord.Id == default)
|
||||||
|
{
|
||||||
|
string cmd = $"INSERT INTO app.{tableName} (vehicleId, data) VALUES(@vehicleId, CAST(@data AS jsonb)) RETURNING id";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("vehicleId", odometerRecord.VehicleId);
|
||||||
|
ctext.Parameters.AddWithValue("data", "{}");
|
||||||
|
odometerRecord.Id = Convert.ToInt32(ctext.ExecuteScalar());
|
||||||
|
//update json data
|
||||||
|
if (odometerRecord.Id != default)
|
||||||
|
{
|
||||||
|
string cmdU = $"UPDATE app.{tableName} SET data = CAST(@data AS jsonb) WHERE id = @id";
|
||||||
|
using (var ctextU = new NpgsqlCommand(cmdU, pgDataSource))
|
||||||
|
{
|
||||||
|
var serializedData = JsonSerializer.Serialize(odometerRecord);
|
||||||
|
ctextU.Parameters.AddWithValue("id", odometerRecord.Id);
|
||||||
|
ctextU.Parameters.AddWithValue("data", serializedData);
|
||||||
|
return ctextU.ExecuteNonQuery() > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return odometerRecord.Id != default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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(odometerRecord);
|
||||||
|
ctext.Parameters.AddWithValue("id", odometerRecord.Id);
|
||||||
|
ctext.Parameters.AddWithValue("data", serializedData);
|
||||||
|
return ctext.ExecuteNonQuery() > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public bool DeleteAllOdometerRecordsByVehicleId(int vehicleId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string cmd = $"DELETE FROM app.{tableName} WHERE vehicleId = @id";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", vehicleId);
|
||||||
|
return ctext.ExecuteNonQuery() > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
160
External/Implementations/Postgres/PlanRecordDataAccess.cs
vendored
Normal file
160
External/Implementations/Postgres/PlanRecordDataAccess.cs
vendored
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
using CarCareTracker.External.Interfaces;
|
||||||
|
using CarCareTracker.Models;
|
||||||
|
using Npgsql;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace CarCareTracker.External.Implementations
|
||||||
|
{
|
||||||
|
public class PGPlanRecordDataAccess : IPlanRecordDataAccess
|
||||||
|
{
|
||||||
|
private NpgsqlConnection pgDataSource;
|
||||||
|
private readonly ILogger<PGPlanRecordDataAccess> _logger;
|
||||||
|
private static string tableName = "planrecords";
|
||||||
|
public PGPlanRecordDataAccess(IConfiguration config, ILogger<PGPlanRecordDataAccess> logger)
|
||||||
|
{
|
||||||
|
pgDataSource = new NpgsqlConnection(config["POSTGRES_CONNECTION"]);
|
||||||
|
_logger = logger;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
pgDataSource.Open();
|
||||||
|
//create table if not exist.
|
||||||
|
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED ALWAYS AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
|
||||||
|
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public List<PlanRecord> GetPlanRecordsByVehicleId(int vehicleId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string cmd = $"SELECT data FROM app.{tableName} WHERE vehicleId = @vehicleId";
|
||||||
|
var results = new List<PlanRecord>();
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("vehicleId", vehicleId);
|
||||||
|
using (NpgsqlDataReader reader = ctext.ExecuteReader())
|
||||||
|
while (reader.Read())
|
||||||
|
{
|
||||||
|
PlanRecord planRecord = JsonSerializer.Deserialize<PlanRecord>(reader["data"] as string);
|
||||||
|
results.Add(planRecord);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
return new List<PlanRecord>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public PlanRecord GetPlanRecordById(int planRecordId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string cmd = $"SELECT data FROM app.{tableName} WHERE id = @id";
|
||||||
|
var result = new PlanRecord();
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", planRecordId);
|
||||||
|
using (NpgsqlDataReader reader = ctext.ExecuteReader())
|
||||||
|
while (reader.Read())
|
||||||
|
{
|
||||||
|
PlanRecord planRecord = JsonSerializer.Deserialize<PlanRecord>(reader["data"] as string);
|
||||||
|
result = planRecord;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
return new PlanRecord();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public bool DeletePlanRecordById(int planRecordId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string cmd = $"DELETE FROM app.{tableName} WHERE id = @id";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", planRecordId);
|
||||||
|
return ctext.ExecuteNonQuery() > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public bool SavePlanRecordToVehicle(PlanRecord planRecord)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (planRecord.Id == default)
|
||||||
|
{
|
||||||
|
string cmd = $"INSERT INTO app.{tableName} (vehicleId, data) VALUES(@vehicleId, CAST(@data AS jsonb)) RETURNING id";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("vehicleId", planRecord.VehicleId);
|
||||||
|
ctext.Parameters.AddWithValue("data", "{}");
|
||||||
|
planRecord.Id = Convert.ToInt32(ctext.ExecuteScalar());
|
||||||
|
//update json data
|
||||||
|
if (planRecord.Id != default)
|
||||||
|
{
|
||||||
|
string cmdU = $"UPDATE app.{tableName} SET data = CAST(@data AS jsonb) WHERE id = @id";
|
||||||
|
using (var ctextU = new NpgsqlCommand(cmdU, pgDataSource))
|
||||||
|
{
|
||||||
|
var serializedData = JsonSerializer.Serialize(planRecord);
|
||||||
|
ctextU.Parameters.AddWithValue("id", planRecord.Id);
|
||||||
|
ctextU.Parameters.AddWithValue("data", serializedData);
|
||||||
|
return ctextU.ExecuteNonQuery() > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return planRecord.Id != default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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(planRecord);
|
||||||
|
ctext.Parameters.AddWithValue("id", planRecord.Id);
|
||||||
|
ctext.Parameters.AddWithValue("data", serializedData);
|
||||||
|
return ctext.ExecuteNonQuery() > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public bool DeleteAllPlanRecordsByVehicleId(int vehicleId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string cmd = $"DELETE FROM app.{tableName} WHERE vehicleId = @id";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", vehicleId);
|
||||||
|
return ctext.ExecuteNonQuery() > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
160
External/Implementations/Postgres/PlanRecordTemplateDataAccess.cs
vendored
Normal file
160
External/Implementations/Postgres/PlanRecordTemplateDataAccess.cs
vendored
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
using CarCareTracker.External.Interfaces;
|
||||||
|
using CarCareTracker.Models;
|
||||||
|
using Npgsql;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace CarCareTracker.External.Implementations
|
||||||
|
{
|
||||||
|
public class PGPlanRecordTemplateDataAccess : IPlanRecordTemplateDataAccess
|
||||||
|
{
|
||||||
|
private NpgsqlConnection pgDataSource;
|
||||||
|
private readonly ILogger<PGPlanRecordTemplateDataAccess> _logger;
|
||||||
|
private static string tableName = "planrecordtemplates";
|
||||||
|
public PGPlanRecordTemplateDataAccess(IConfiguration config, ILogger<PGPlanRecordTemplateDataAccess> logger)
|
||||||
|
{
|
||||||
|
pgDataSource = new NpgsqlConnection(config["POSTGRES_CONNECTION"]);
|
||||||
|
_logger = logger;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
pgDataSource.Open();
|
||||||
|
//create table if not exist.
|
||||||
|
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED ALWAYS AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
|
||||||
|
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public List<PlanRecordInput> GetPlanRecordTemplatesByVehicleId(int vehicleId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string cmd = $"SELECT data FROM app.{tableName} WHERE vehicleId = @vehicleId";
|
||||||
|
var results = new List<PlanRecordInput>();
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("vehicleId", vehicleId);
|
||||||
|
using (NpgsqlDataReader reader = ctext.ExecuteReader())
|
||||||
|
while (reader.Read())
|
||||||
|
{
|
||||||
|
PlanRecordInput planRecord = JsonSerializer.Deserialize<PlanRecordInput>(reader["data"] as string);
|
||||||
|
results.Add(planRecord);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
return new List<PlanRecordInput>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public PlanRecordInput GetPlanRecordTemplateById(int planRecordId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string cmd = $"SELECT data FROM app.{tableName} WHERE id = @id";
|
||||||
|
var result = new PlanRecordInput();
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", planRecordId);
|
||||||
|
using (NpgsqlDataReader reader = ctext.ExecuteReader())
|
||||||
|
while (reader.Read())
|
||||||
|
{
|
||||||
|
PlanRecordInput planRecord = JsonSerializer.Deserialize<PlanRecordInput>(reader["data"] as string);
|
||||||
|
result = planRecord;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
return new PlanRecordInput();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public bool DeletePlanRecordTemplateById(int planRecordId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string cmd = $"DELETE FROM app.{tableName} WHERE id = @id";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", planRecordId);
|
||||||
|
return ctext.ExecuteNonQuery() > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public bool SavePlanRecordTemplateToVehicle(PlanRecordInput planRecord)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (planRecord.Id == default)
|
||||||
|
{
|
||||||
|
string cmd = $"INSERT INTO app.{tableName} (vehicleId, data) VALUES(@vehicleId, CAST(@data AS jsonb)) RETURNING id";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("vehicleId", planRecord.VehicleId);
|
||||||
|
ctext.Parameters.AddWithValue("data", "{}");
|
||||||
|
planRecord.Id = Convert.ToInt32(ctext.ExecuteScalar());
|
||||||
|
//update json data
|
||||||
|
if (planRecord.Id != default)
|
||||||
|
{
|
||||||
|
string cmdU = $"UPDATE app.{tableName} SET data = CAST(@data AS jsonb) WHERE id = @id";
|
||||||
|
using (var ctextU = new NpgsqlCommand(cmdU, pgDataSource))
|
||||||
|
{
|
||||||
|
var serializedData = JsonSerializer.Serialize(planRecord);
|
||||||
|
ctextU.Parameters.AddWithValue("id", planRecord.Id);
|
||||||
|
ctextU.Parameters.AddWithValue("data", serializedData);
|
||||||
|
return ctextU.ExecuteNonQuery() > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return planRecord.Id != default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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(planRecord);
|
||||||
|
ctext.Parameters.AddWithValue("id", planRecord.Id);
|
||||||
|
ctext.Parameters.AddWithValue("data", serializedData);
|
||||||
|
return ctext.ExecuteNonQuery() > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public bool DeleteAllPlanRecordTemplatesByVehicleId(int vehicleId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string cmd = $"DELETE FROM app.{tableName} WHERE vehicleId = @id";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", vehicleId);
|
||||||
|
return ctext.ExecuteNonQuery() > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
160
External/Implementations/Postgres/SupplyRecordDataAccess.cs
vendored
Normal file
160
External/Implementations/Postgres/SupplyRecordDataAccess.cs
vendored
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
using CarCareTracker.External.Interfaces;
|
||||||
|
using CarCareTracker.Models;
|
||||||
|
using Npgsql;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace CarCareTracker.External.Implementations
|
||||||
|
{
|
||||||
|
public class PGSupplyRecordDataAccess : ISupplyRecordDataAccess
|
||||||
|
{
|
||||||
|
private NpgsqlConnection pgDataSource;
|
||||||
|
private readonly ILogger<PGSupplyRecordDataAccess> _logger;
|
||||||
|
private static string tableName = "supplyrecords";
|
||||||
|
public PGSupplyRecordDataAccess(IConfiguration config, ILogger<PGSupplyRecordDataAccess> logger)
|
||||||
|
{
|
||||||
|
pgDataSource = new NpgsqlConnection(config["POSTGRES_CONNECTION"]);
|
||||||
|
_logger = logger;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
pgDataSource.Open();
|
||||||
|
//create table if not exist.
|
||||||
|
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED ALWAYS AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
|
||||||
|
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public List<SupplyRecord> GetSupplyRecordsByVehicleId(int vehicleId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string cmd = $"SELECT data FROM app.{tableName} WHERE vehicleId = @vehicleId";
|
||||||
|
var results = new List<SupplyRecord>();
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("vehicleId", vehicleId);
|
||||||
|
using (NpgsqlDataReader reader = ctext.ExecuteReader())
|
||||||
|
while (reader.Read())
|
||||||
|
{
|
||||||
|
SupplyRecord supplyRecord = JsonSerializer.Deserialize<SupplyRecord>(reader["data"] as string);
|
||||||
|
results.Add(supplyRecord);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
return new List<SupplyRecord>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public SupplyRecord GetSupplyRecordById(int supplyRecordId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string cmd = $"SELECT data FROM app.{tableName} WHERE id = @id";
|
||||||
|
var result = new SupplyRecord();
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", supplyRecordId);
|
||||||
|
using (NpgsqlDataReader reader = ctext.ExecuteReader())
|
||||||
|
while (reader.Read())
|
||||||
|
{
|
||||||
|
SupplyRecord supplyRecord = JsonSerializer.Deserialize<SupplyRecord>(reader["data"] as string);
|
||||||
|
result = supplyRecord;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
return new SupplyRecord();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public bool DeleteSupplyRecordById(int supplyRecordId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string cmd = $"DELETE FROM app.{tableName} WHERE id = @id";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", supplyRecordId);
|
||||||
|
return ctext.ExecuteNonQuery() > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public bool SaveSupplyRecordToVehicle(SupplyRecord supplyRecord)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (supplyRecord.Id == default)
|
||||||
|
{
|
||||||
|
string cmd = $"INSERT INTO app.{tableName} (vehicleId, data) VALUES(@vehicleId, CAST(@data AS jsonb)) RETURNING id";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("vehicleId", supplyRecord.VehicleId);
|
||||||
|
ctext.Parameters.AddWithValue("data", "{}");
|
||||||
|
supplyRecord.Id = Convert.ToInt32(ctext.ExecuteScalar());
|
||||||
|
//update json data
|
||||||
|
if (supplyRecord.Id != default)
|
||||||
|
{
|
||||||
|
string cmdU = $"UPDATE app.{tableName} SET data = CAST(@data AS jsonb) WHERE id = @id";
|
||||||
|
using (var ctextU = new NpgsqlCommand(cmdU, pgDataSource))
|
||||||
|
{
|
||||||
|
var serializedData = JsonSerializer.Serialize(supplyRecord);
|
||||||
|
ctextU.Parameters.AddWithValue("id", supplyRecord.Id);
|
||||||
|
ctextU.Parameters.AddWithValue("data", serializedData);
|
||||||
|
return ctextU.ExecuteNonQuery() > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return supplyRecord.Id != default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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(supplyRecord);
|
||||||
|
ctext.Parameters.AddWithValue("id", supplyRecord.Id);
|
||||||
|
ctext.Parameters.AddWithValue("data", serializedData);
|
||||||
|
return ctext.ExecuteNonQuery() > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public bool DeleteAllSupplyRecordsByVehicleId(int vehicleId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string cmd = $"DELETE FROM app.{tableName} WHERE vehicleId = @id";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", vehicleId);
|
||||||
|
return ctext.ExecuteNonQuery() > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
160
External/Implementations/Postgres/UpgradeRecordDataAccess.cs
vendored
Normal file
160
External/Implementations/Postgres/UpgradeRecordDataAccess.cs
vendored
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
using CarCareTracker.External.Interfaces;
|
||||||
|
using CarCareTracker.Models;
|
||||||
|
using Npgsql;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace CarCareTracker.External.Implementations
|
||||||
|
{
|
||||||
|
public class PGUpgradeRecordDataAccess : IUpgradeRecordDataAccess
|
||||||
|
{
|
||||||
|
private NpgsqlConnection pgDataSource;
|
||||||
|
private readonly ILogger<PGUpgradeRecordDataAccess> _logger;
|
||||||
|
private static string tableName = "upgraderecords";
|
||||||
|
public PGUpgradeRecordDataAccess(IConfiguration config, ILogger<PGUpgradeRecordDataAccess> logger)
|
||||||
|
{
|
||||||
|
pgDataSource = new NpgsqlConnection(config["POSTGRES_CONNECTION"]);
|
||||||
|
_logger = logger;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
pgDataSource.Open();
|
||||||
|
//create table if not exist.
|
||||||
|
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED ALWAYS AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
|
||||||
|
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public List<UpgradeRecord> GetUpgradeRecordsByVehicleId(int vehicleId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string cmd = $"SELECT data FROM app.{tableName} WHERE vehicleId = @vehicleId";
|
||||||
|
var results = new List<UpgradeRecord>();
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("vehicleId", vehicleId);
|
||||||
|
using (NpgsqlDataReader reader = ctext.ExecuteReader())
|
||||||
|
while (reader.Read())
|
||||||
|
{
|
||||||
|
UpgradeRecord upgradeRecord = JsonSerializer.Deserialize<UpgradeRecord>(reader["data"] as string);
|
||||||
|
results.Add(upgradeRecord);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
return new List<UpgradeRecord>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public UpgradeRecord GetUpgradeRecordById(int upgradeRecordId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string cmd = $"SELECT data FROM app.{tableName} WHERE id = @id";
|
||||||
|
var result = new UpgradeRecord();
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", upgradeRecordId);
|
||||||
|
using (NpgsqlDataReader reader = ctext.ExecuteReader())
|
||||||
|
while (reader.Read())
|
||||||
|
{
|
||||||
|
UpgradeRecord upgradeRecord = JsonSerializer.Deserialize<UpgradeRecord>(reader["data"] as string);
|
||||||
|
result = upgradeRecord;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
return new UpgradeRecord();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public bool DeleteUpgradeRecordById(int upgradeRecordId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string cmd = $"DELETE FROM app.{tableName} WHERE id = @id";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", upgradeRecordId);
|
||||||
|
return ctext.ExecuteNonQuery() > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public bool SaveUpgradeRecordToVehicle(UpgradeRecord upgradeRecord)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (upgradeRecord.Id == default)
|
||||||
|
{
|
||||||
|
string cmd = $"INSERT INTO app.{tableName} (vehicleId, data) VALUES(@vehicleId, CAST(@data AS jsonb)) RETURNING id";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("vehicleId", upgradeRecord.VehicleId);
|
||||||
|
ctext.Parameters.AddWithValue("data", "{}");
|
||||||
|
upgradeRecord.Id = Convert.ToInt32(ctext.ExecuteScalar());
|
||||||
|
//update json data
|
||||||
|
if (upgradeRecord.Id != default)
|
||||||
|
{
|
||||||
|
string cmdU = $"UPDATE app.{tableName} SET data = CAST(@data AS jsonb) WHERE id = @id";
|
||||||
|
using (var ctextU = new NpgsqlCommand(cmdU, pgDataSource))
|
||||||
|
{
|
||||||
|
var serializedData = JsonSerializer.Serialize(upgradeRecord);
|
||||||
|
ctextU.Parameters.AddWithValue("id", upgradeRecord.Id);
|
||||||
|
ctextU.Parameters.AddWithValue("data", serializedData);
|
||||||
|
return ctextU.ExecuteNonQuery() > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return upgradeRecord.Id != default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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(upgradeRecord);
|
||||||
|
ctext.Parameters.AddWithValue("id", upgradeRecord.Id);
|
||||||
|
ctext.Parameters.AddWithValue("data", serializedData);
|
||||||
|
return ctext.ExecuteNonQuery() > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public bool DeleteAllUpgradeRecordsByVehicleId(int vehicleId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string cmd = $"DELETE FROM app.{tableName} WHERE vehicleId = @id";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", vehicleId);
|
||||||
|
return ctext.ExecuteNonQuery() > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
15
Program.cs
15
Program.cs
@@ -22,6 +22,11 @@ if (!string.IsNullOrWhiteSpace(builder.Configuration["POSTGRES_CONNECTION"])){
|
|||||||
builder.Services.AddSingleton<ICollisionRecordDataAccess, PGCollisionRecordDataAccess>();
|
builder.Services.AddSingleton<ICollisionRecordDataAccess, PGCollisionRecordDataAccess>();
|
||||||
builder.Services.AddSingleton<ITaxRecordDataAccess, PGTaxRecordDataAccess>();
|
builder.Services.AddSingleton<ITaxRecordDataAccess, PGTaxRecordDataAccess>();
|
||||||
builder.Services.AddSingleton<IReminderRecordDataAccess, PGReminderRecordDataAccess>();
|
builder.Services.AddSingleton<IReminderRecordDataAccess, PGReminderRecordDataAccess>();
|
||||||
|
builder.Services.AddSingleton<IUpgradeRecordDataAccess, PGUpgradeRecordDataAccess>();
|
||||||
|
builder.Services.AddSingleton<IOdometerRecordDataAccess, PGOdometerRecordDataAccess>();
|
||||||
|
builder.Services.AddSingleton<ISupplyRecordDataAccess, PGSupplyRecordDataAccess>();
|
||||||
|
builder.Services.AddSingleton<IPlanRecordDataAccess, PGPlanRecordDataAccess>();
|
||||||
|
builder.Services.AddSingleton<IPlanRecordTemplateDataAccess, PGPlanRecordTemplateDataAccess>();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -32,17 +37,17 @@ else
|
|||||||
builder.Services.AddSingleton<ICollisionRecordDataAccess, CollisionRecordDataAccess>();
|
builder.Services.AddSingleton<ICollisionRecordDataAccess, CollisionRecordDataAccess>();
|
||||||
builder.Services.AddSingleton<ITaxRecordDataAccess, TaxRecordDataAccess>();
|
builder.Services.AddSingleton<ITaxRecordDataAccess, TaxRecordDataAccess>();
|
||||||
builder.Services.AddSingleton<IReminderRecordDataAccess, ReminderRecordDataAccess>();
|
builder.Services.AddSingleton<IReminderRecordDataAccess, ReminderRecordDataAccess>();
|
||||||
|
builder.Services.AddSingleton<IUpgradeRecordDataAccess, UpgradeRecordDataAccess>();
|
||||||
|
builder.Services.AddSingleton<IOdometerRecordDataAccess, OdometerRecordDataAccess>();
|
||||||
|
builder.Services.AddSingleton<ISupplyRecordDataAccess, SupplyRecordDataAccess>();
|
||||||
|
builder.Services.AddSingleton<IPlanRecordDataAccess, PlanRecordDataAccess>();
|
||||||
|
builder.Services.AddSingleton<IPlanRecordTemplateDataAccess, PlanRecordTemplateDataAccess>();
|
||||||
}
|
}
|
||||||
|
|
||||||
builder.Services.AddSingleton<IUpgradeRecordDataAccess, UpgradeRecordDataAccess>();
|
|
||||||
builder.Services.AddSingleton<IUserRecordDataAccess, UserRecordDataAccess>();
|
builder.Services.AddSingleton<IUserRecordDataAccess, UserRecordDataAccess>();
|
||||||
builder.Services.AddSingleton<ITokenRecordDataAccess, TokenRecordDataAccess>();
|
builder.Services.AddSingleton<ITokenRecordDataAccess, TokenRecordDataAccess>();
|
||||||
builder.Services.AddSingleton<IUserAccessDataAccess, UserAccessDataAccess>();
|
builder.Services.AddSingleton<IUserAccessDataAccess, UserAccessDataAccess>();
|
||||||
builder.Services.AddSingleton<IUserConfigDataAccess, UserConfigDataAccess>();
|
builder.Services.AddSingleton<IUserConfigDataAccess, UserConfigDataAccess>();
|
||||||
builder.Services.AddSingleton<ISupplyRecordDataAccess, SupplyRecordDataAccess>();
|
|
||||||
builder.Services.AddSingleton<IPlanRecordDataAccess, PlanRecordDataAccess>();
|
|
||||||
builder.Services.AddSingleton<IPlanRecordTemplateDataAccess, PlanRecordTemplateDataAccess>();
|
|
||||||
builder.Services.AddSingleton<IOdometerRecordDataAccess, OdometerRecordDataAccess>();
|
|
||||||
|
|
||||||
//configure helpers
|
//configure helpers
|
||||||
builder.Services.AddSingleton<IFileHelper, FileHelper>();
|
builder.Services.AddSingleton<IFileHelper, FileHelper>();
|
||||||
|
|||||||
Reference in New Issue
Block a user