added note data access for postgres.
This commit is contained in:
112
External/Implementations/Postgres/NoteDataAccess.cs
vendored
Normal file
112
External/Implementations/Postgres/NoteDataAccess.cs
vendored
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
using CarCareTracker.External.Interfaces;
|
||||||
|
using CarCareTracker.Helper;
|
||||||
|
using CarCareTracker.Models;
|
||||||
|
using Npgsql;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace CarCareTracker.External.Implementations
|
||||||
|
{
|
||||||
|
public class PGNoteDataAccess: INoteDataAccess
|
||||||
|
{
|
||||||
|
private NpgsqlConnection pgDataSource;
|
||||||
|
public PGNoteDataAccess(IConfiguration config)
|
||||||
|
{
|
||||||
|
pgDataSource = new NpgsqlConnection(config["POSTGRES_CONNECTION"]);
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private static string tableName = "notes";
|
||||||
|
public List<Note> GetNotesByVehicleId(int vehicleId)
|
||||||
|
{
|
||||||
|
string cmd = $"SELECT data FROM app.{tableName} WHERE vehicleId = @vehicleId";
|
||||||
|
var results = new List<Note>();
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("vehicleId", vehicleId);
|
||||||
|
using (NpgsqlDataReader reader = ctext.ExecuteReader())
|
||||||
|
while (reader.Read())
|
||||||
|
{
|
||||||
|
Note note = JsonSerializer.Deserialize<Note>(reader["data"] as string);
|
||||||
|
results.Add(note);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
public Note GetNoteById(int noteId)
|
||||||
|
{
|
||||||
|
string cmd = $"SELECT data FROM app.{tableName} WHERE id = @id";
|
||||||
|
var result = new Note();
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", noteId);
|
||||||
|
using (NpgsqlDataReader reader = ctext.ExecuteReader())
|
||||||
|
while (reader.Read())
|
||||||
|
{
|
||||||
|
Note note = JsonSerializer.Deserialize<Note>(reader["data"] as string);
|
||||||
|
result = note;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
public bool SaveNoteToVehicle(Note note)
|
||||||
|
{
|
||||||
|
if (note.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", note.VehicleId);
|
||||||
|
ctext.Parameters.AddWithValue("data", "{}");
|
||||||
|
note.Id = Convert.ToInt32(ctext.ExecuteScalar());
|
||||||
|
//update json data
|
||||||
|
if (note.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(note);
|
||||||
|
ctextU.Parameters.AddWithValue("id", note.Id);
|
||||||
|
ctextU.Parameters.AddWithValue("data", serializedData);
|
||||||
|
return ctextU.ExecuteNonQuery() > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return note.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(note);
|
||||||
|
ctext.Parameters.AddWithValue("id", note.Id);
|
||||||
|
ctext.Parameters.AddWithValue("data", serializedData);
|
||||||
|
return ctext.ExecuteNonQuery() > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public bool DeleteNoteById(int noteId)
|
||||||
|
{
|
||||||
|
string cmd = $"DELETE FROM app.{tableName} WHERE id = @id";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", noteId);
|
||||||
|
return ctext.ExecuteNonQuery() > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public bool DeleteAllNotesByVehicleId(int vehicleId)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,11 +15,13 @@ builder.Services.AddControllersWithViews();
|
|||||||
//data access method
|
//data access method
|
||||||
if (!string.IsNullOrWhiteSpace(builder.Configuration["POSTGRES_CONNECTION"])){
|
if (!string.IsNullOrWhiteSpace(builder.Configuration["POSTGRES_CONNECTION"])){
|
||||||
builder.Services.AddSingleton<IVehicleDataAccess, PGVehicleDataAccess>();
|
builder.Services.AddSingleton<IVehicleDataAccess, PGVehicleDataAccess>();
|
||||||
|
builder.Services.AddSingleton<INoteDataAccess, PGNoteDataAccess>();
|
||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
builder.Services.AddSingleton<IVehicleDataAccess, VehicleDataAccess>();
|
builder.Services.AddSingleton<IVehicleDataAccess, VehicleDataAccess>();
|
||||||
|
builder.Services.AddSingleton<INoteDataAccess, NoteDataAccess>();
|
||||||
}
|
}
|
||||||
builder.Services.AddSingleton<INoteDataAccess, NoteDataAccess>();
|
|
||||||
builder.Services.AddSingleton<IServiceRecordDataAccess, ServiceRecordDataAccess>();
|
builder.Services.AddSingleton<IServiceRecordDataAccess, ServiceRecordDataAccess>();
|
||||||
builder.Services.AddSingleton<IGasRecordDataAccess, GasRecordDataAccess>();
|
builder.Services.AddSingleton<IGasRecordDataAccess, GasRecordDataAccess>();
|
||||||
builder.Services.AddSingleton<ICollisionRecordDataAccess, CollisionRecordDataAccess>();
|
builder.Services.AddSingleton<ICollisionRecordDataAccess, CollisionRecordDataAccess>();
|
||||||
|
|||||||
Reference in New Issue
Block a user