mirror of
https://github.com/hargata/lubelog.git
synced 2025-12-10 00:46:08 -06:00
added token data access for pg
This commit is contained in:
parent
ad2b58697a
commit
9fe0396abe
@ -2,7 +2,6 @@
|
||||
using CarCareTracker.Helper;
|
||||
using CarCareTracker.Models;
|
||||
using LiteDB;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
|
||||
namespace CarCareTracker.External.Implementations
|
||||
{
|
||||
|
||||
145
External/Implementations/Postgres/TokenRecordDataAccess.cs
vendored
Normal file
145
External/Implementations/Postgres/TokenRecordDataAccess.cs
vendored
Normal file
@ -0,0 +1,145 @@
|
||||
using CarCareTracker.External.Interfaces;
|
||||
using CarCareTracker.Models;
|
||||
using Npgsql;
|
||||
using System.Net.Mail;
|
||||
|
||||
namespace CarCareTracker.External.Implementations
|
||||
{
|
||||
public class PGTokenRecordDataAccess : ITokenRecordDataAccess
|
||||
{
|
||||
private NpgsqlConnection pgDataSource;
|
||||
private readonly ILogger<PGTokenRecordDataAccess> _logger;
|
||||
private static string tableName = "tokenrecords";
|
||||
public PGTokenRecordDataAccess(IConfiguration config, ILogger<PGTokenRecordDataAccess> 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, body TEXT not null, emailaddress TEXT not null)";
|
||||
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
||||
{
|
||||
ctext.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex.Message);
|
||||
}
|
||||
}
|
||||
public List<Token> GetTokens()
|
||||
{
|
||||
try
|
||||
{
|
||||
string cmd = $"SELECT id, emailaddress, body FROM app.{tableName}";
|
||||
var results = new List<Token>();
|
||||
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||
{
|
||||
using (NpgsqlDataReader reader = ctext.ExecuteReader())
|
||||
while (reader.Read())
|
||||
{
|
||||
Token result = new Token();
|
||||
result.Id = int.Parse(reader["id"].ToString());
|
||||
result.EmailAddress = reader["emailaddress"].ToString();
|
||||
result.Body = reader["body"].ToString();
|
||||
results.Add(result);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex.Message);
|
||||
return new List<Token>();
|
||||
}
|
||||
}
|
||||
public Token GetTokenRecordByBody(string tokenBody)
|
||||
{
|
||||
try
|
||||
{
|
||||
string cmd = $"SELECT id, emailaddress, body FROM app.{tableName} WHERE body = @body";
|
||||
var result = new Token();
|
||||
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||
{
|
||||
ctext.Parameters.AddWithValue("body", tokenBody);
|
||||
using (NpgsqlDataReader reader = ctext.ExecuteReader())
|
||||
while (reader.Read())
|
||||
{
|
||||
result.Id = int.Parse(reader["id"].ToString());
|
||||
result.EmailAddress = reader["emailaddress"].ToString();
|
||||
result.Body = reader["body"].ToString();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex.Message);
|
||||
return new Token();
|
||||
}
|
||||
}
|
||||
public Token GetTokenRecordByEmailAddress(string emailAddress)
|
||||
{
|
||||
try
|
||||
{
|
||||
string cmd = $"SELECT id, emailaddress, body FROM app.{tableName} WHERE emailaddress = @emailaddress";
|
||||
var result = new Token();
|
||||
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||
{
|
||||
ctext.Parameters.AddWithValue("emailaddress", emailAddress);
|
||||
using (NpgsqlDataReader reader = ctext.ExecuteReader())
|
||||
while (reader.Read())
|
||||
{
|
||||
result.Id = int.Parse(reader["id"].ToString());
|
||||
result.EmailAddress = reader["emailaddress"].ToString();
|
||||
result.Body = reader["body"].ToString();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex.Message);
|
||||
return new Token();
|
||||
}
|
||||
}
|
||||
public bool CreateNewToken(Token token)
|
||||
{
|
||||
try
|
||||
{
|
||||
string cmd = $"INSERT INTO app.{tableName} (emailaddress, body) VALUES(@emailaddress, @body) RETURNING id";
|
||||
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||
{
|
||||
ctext.Parameters.AddWithValue("emailaddress", token.EmailAddress);
|
||||
ctext.Parameters.AddWithValue("body", token.Body);
|
||||
token.Id = Convert.ToInt32(ctext.ExecuteScalar());
|
||||
return token.Id != default;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex.Message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public bool DeleteToken(int tokenId)
|
||||
{
|
||||
try
|
||||
{
|
||||
string cmd = $"DELETE FROM app.{tableName} WHERE id = @id";
|
||||
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||
{
|
||||
ctext.Parameters.AddWithValue("id", tokenId);
|
||||
return ctext.ExecuteNonQuery() > 0;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex.Message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,8 +1,6 @@
|
||||
using CarCareTracker.External.Interfaces;
|
||||
using CarCareTracker.Models;
|
||||
using Npgsql;
|
||||
using System.Net.Mail;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace CarCareTracker.External.Implementations
|
||||
{
|
||||
|
||||
@ -29,6 +29,7 @@ if (!string.IsNullOrWhiteSpace(builder.Configuration["POSTGRES_CONNECTION"])){
|
||||
builder.Services.AddSingleton<IPlanRecordTemplateDataAccess, PGPlanRecordTemplateDataAccess>();
|
||||
builder.Services.AddSingleton<IUserConfigDataAccess, PGUserConfigDataAccess>();
|
||||
builder.Services.AddSingleton<IUserRecordDataAccess, PGUserRecordDataAccess>();
|
||||
builder.Services.AddSingleton<ITokenRecordDataAccess, PGTokenRecordDataAccess>();
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -46,10 +47,11 @@ else
|
||||
builder.Services.AddSingleton<IPlanRecordTemplateDataAccess, PlanRecordTemplateDataAccess>();
|
||||
builder.Services.AddSingleton<IUserConfigDataAccess, UserConfigDataAccess>();
|
||||
builder.Services.AddSingleton<IUserRecordDataAccess, UserRecordDataAccess>();
|
||||
builder.Services.AddSingleton<ITokenRecordDataAccess, TokenRecordDataAccess>();
|
||||
}
|
||||
|
||||
|
||||
builder.Services.AddSingleton<ITokenRecordDataAccess, TokenRecordDataAccess>();
|
||||
|
||||
builder.Services.AddSingleton<IUserAccessDataAccess, UserAccessDataAccess>();
|
||||
|
||||
//configure helpers
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user