using Dapper; using GasStation.Entities; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Npgsql; namespace GasStation.Repositories.Implementations; public class SupplyRepository : ISupplyRepository { private readonly IConnectionString _connectionString; private readonly ILogger _logger; public SupplyRepository(IConnectionString connectionString, ILogger logger) { _connectionString = connectionString; _logger = logger; } public void CreateSupply(Supply supply) { _logger.LogInformation("Добавление объекта"); _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(supply)); try { using var connection = new NpgsqlConnection(_connectionString.ConnectionString); var queryInsert = @" INSERT INTO supply (productID, supplierID, count, supplyDate) VALUES (@ProductID, @SupplierID, @Count, @SupplyDate)"; connection.Execute(queryInsert, supply); } catch (Exception ex) { _logger.LogError(ex, "Ошибка при добавлении объекта"); throw; } } public void DeleteSupply(int id) { _logger.LogInformation("Удаление объекта"); _logger.LogDebug("Объект: {id}", id); try { using var connection = new NpgsqlConnection(_connectionString.ConnectionString); var queryDelete = @" DELETE FROM supply WHERE id=@Id"; connection.Execute(queryDelete, new { id }); } catch (Exception ex) { _logger.LogError(ex, "Ошибка при удалении объекта"); throw; } } public IEnumerable ReadSupply(DateTime? supplyDate = null, int? supplierID = null, int? productID = null, int? count = null) { _logger.LogInformation("Получение всех объектов"); try { using var connection = new NpgsqlConnection(_connectionString.ConnectionString); var querySelect = @"SELECT * FROM supply"; var supply = connection.Query(querySelect); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(supply)); return supply; } catch (Exception ex) { _logger.LogError(ex, "Ошибка при чтении объектов"); throw; } } }