121 lines
4.1 KiB
C#
121 lines
4.1 KiB
C#
using Dapper;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using Npgsql;
|
|
using ProjectFuel.Entities;
|
|
using ProjectFuel.Repositories;
|
|
|
|
namespace ProjectFuel.Repositories.Implementations;
|
|
|
|
public class FuelRepository : IFuelRepository
|
|
{
|
|
private readonly IConnectionString _connectionString;
|
|
|
|
private readonly ILogger<FuelRepository> _logger;
|
|
|
|
public FuelRepository(IConnectionString connectionString, ILogger<FuelRepository> logger)
|
|
{
|
|
_connectionString = connectionString;
|
|
_logger = logger;
|
|
}
|
|
public void CreateFuel(Fuel fuel)
|
|
{
|
|
_logger.LogInformation("Добавление объекта");
|
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(fuel));
|
|
|
|
try
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
var queryInsert = @"INSERT INTO Fuel_And_Lubricants (Fuel_Type, Price_Per_Liter, Amount)
|
|
VALUES (@Fuel_Type, @Price_Per_Liter, @Amount)";
|
|
connection.Execute(queryInsert, fuel);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public void DeleteFuel(int id)
|
|
{
|
|
_logger.LogInformation("Удаление объекта");
|
|
_logger.LogDebug("Объект: {id}", id);
|
|
try
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
var queryDelete = @"
|
|
DELETE FROM Fuel_And_Lubricants
|
|
WHERE Fuel_ID=@id";
|
|
connection.Execute(queryDelete, new { id });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public Fuel ReadFuelByID(int id)
|
|
{
|
|
_logger.LogInformation("Получение объекта по идентификатору");
|
|
_logger.LogDebug("Объект: {id}", id);
|
|
try
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
var querySelect = @"
|
|
SELECT * FROM Fuel_And_Lubricants
|
|
WHERE Fuel_ID=@id";
|
|
var fuel = connection.QueryFirst<Fuel>(querySelect, new { id });
|
|
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(fuel));
|
|
return fuel;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public IEnumerable<Fuel> ReadFuels()
|
|
{
|
|
_logger.LogInformation("Получение всех объектов");
|
|
try
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
var querySelect = "SELECT * FROM Fuel_And_Lubricants";
|
|
var fuels = connection.Query<Fuel>(querySelect);
|
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(fuels));
|
|
return fuels;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public void UpdateFuel(Fuel fuel)
|
|
{
|
|
_logger.LogInformation("Редактирование объекта");
|
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(fuel));
|
|
try
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
var queryUpdate = @"
|
|
UPDATE Fuel_And_Lubricants
|
|
SET
|
|
Fuel_Type=@Fuel_Type,
|
|
Price_Per_Liter=@Price_Per_Liter,
|
|
Amount=@Amount
|
|
WHERE Fuel_ID=@Fuel_ID";
|
|
connection.Execute(queryUpdate, fuel);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
|
throw;
|
|
}
|
|
}
|
|
}
|