124 lines
3.8 KiB
C#
124 lines
3.8 KiB
C#
using Dapper;
|
|
using GasStation.Entities;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using Npgsql;
|
|
|
|
namespace GasStation.Repositories.Implementations;
|
|
|
|
public class SupplierRepository : ISupplierRepository
|
|
{
|
|
private readonly IConnectionString _connectionString;
|
|
|
|
private readonly ILogger<SupplierRepository> _logger;
|
|
|
|
public SupplierRepository(IConnectionString connectionString, ILogger<SupplierRepository> logger)
|
|
{
|
|
_connectionString = connectionString;
|
|
_logger = logger;
|
|
}
|
|
|
|
public void CreateSupplier(Supplier supplier)
|
|
{
|
|
_logger.LogInformation("Добавление объекта");
|
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(supplier));
|
|
|
|
try
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
var queryInsert = @"
|
|
INSERT INTO supplier (supplierName)
|
|
VALUES (@SupplierName)";
|
|
connection.Execute(queryInsert, supplier);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public void DeleteSupplier(int id)
|
|
{
|
|
_logger.LogInformation("Удаление объекта");
|
|
_logger.LogDebug("Объект: {id}", id);
|
|
|
|
try
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
var queryDelete = @"
|
|
DELETE FROM supplier
|
|
WHERE id=@Id";
|
|
connection.Execute(queryDelete, new { id });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public Supplier ReadSupplierByID(int id)
|
|
{
|
|
_logger.LogInformation("Получение объекта по идентификатору");
|
|
_logger.LogDebug("Объект: {id}", id);
|
|
|
|
try
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
var querySelect = @"
|
|
SELECT * FROM supplier
|
|
WHERE id=@Id";
|
|
var supplier = connection.QueryFirst<Supplier>(querySelect, new { id });
|
|
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(supplier));
|
|
|
|
return supplier;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public IEnumerable<Supplier> ReadSupplier()
|
|
{
|
|
_logger.LogInformation("Получение всех объектов");
|
|
try
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
var querySelect = @"SELECT * FROM supplier";
|
|
var supplier = connection.Query<Supplier>(querySelect);
|
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(supplier));
|
|
return supplier;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public void UpdateSupplier(Supplier supplier)
|
|
{
|
|
_logger.LogInformation("Редактирование объекта");
|
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(supplier));
|
|
|
|
try
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
var queryUpdate = @"
|
|
UPDATE supplier
|
|
SET
|
|
supplierName=@SupplierName
|
|
WHERE id=@Id";
|
|
connection.Execute(queryUpdate, supplier);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
|
throw;
|
|
}
|
|
}
|
|
}
|