109 lines
3.8 KiB
C#
Raw Normal View History

using Microsoft.Extensions.Logging;
using LDBproject.Entities;
using Newtonsoft.Json;
using Npgsql;
using Dapper;
2024-12-20 20:09:17 +04:00
namespace LDBproject.Repositories.Implementations;
public class CustomerCardR : ICustomerCardsRep
{
private readonly IConnectionString _connectionString;
private readonly ILogger<CustomerCardR> _logger;
public CustomerCardR(IConnectionString connectionString, ILogger<CustomerCardR> logger)
{
_connectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
2024-12-20 20:09:17 +04:00
public CustomerCard GetCardByID(int id)
{
_logger.LogInformation("< Getting CARD by id >");
_logger.LogDebug("Object ID: {id}", id);
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @"SELECT * FROM CustomerCards WHERE CardID = @CardID"; // Assumes you have a CardID column
return connection.QueryFirstOrDefault<CustomerCard>(querySelect, new { CardID = id });
}
catch (Exception ex)
{
_logger.LogError(ex, "< Error while reading CARD by ID >");
throw;
}
2024-12-20 20:09:17 +04:00
}
public void AddCard(CustomerCard card)
{
_logger.LogInformation("< New (reader)CARD Added >");
_logger.LogDebug("Object: {json}", JsonConvert.SerializeObject(card));
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
var queryInsert = @"INSERT INTO CustomerCards (FIO, AgeBirthday) VALUES (@FIO, @AgeBirthday)";
connection.Execute(queryInsert, card);
}
catch (Exception ex)
{
_logger.LogError(ex, "< Error while adding CARD >");
throw;
}
2024-12-20 20:09:17 +04:00
}
public void UpdateCard(CustomerCard card)
{
_logger.LogInformation("< CARD Info Updated >");
_logger.LogDebug("Object: {json}", JsonConvert.SerializeObject(card));
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
var queryUpdate = @"UPDATE CustomerCards SET FIO = @FIO, AgeBirthday = @AgeBirthday WHERE CardID = @CardID"; // Assumes you have a CardID column
connection.Execute(queryUpdate, card);
}
catch (Exception ex)
{
_logger.LogError(ex, "< Error while updating CARD >");
throw;
}
2024-12-20 20:09:17 +04:00
}
public void DeleteCard(int id)
{
_logger.LogInformation("< Removing CARD >");
_logger.LogDebug("Object ID: {id}", id);
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var queryDelete = @"DELETE FROM CustomerCards WHERE CardID = @CardID"; // Assumes you have a CardID column
connection.Execute(queryDelete, new { CardID = id });
}
catch (Exception ex)
{
_logger.LogError(ex, "< Error while deleting CARD >");
throw;
}
2024-12-20 20:09:17 +04:00
}
public IEnumerable<CustomerCard> GetCards()
{
_logger.LogInformation("< Getting all CARDS >");
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
var querySelectAll = "SELECT * FROM CustomerCards";
var cards = connection.Query<CustomerCard>(querySelectAll);
_logger.LogDebug("Aimed objects: {json}", JsonConvert.SerializeObject(cards));
return cards;
}
catch (Exception ex)
{
_logger.LogError(ex, "< Error while getting CARDS >");
throw;
}
2024-12-20 20:09:17 +04:00
}
}