56 lines
2.0 KiB
C#
56 lines
2.0 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 RefillRepository : IRefillRepository
|
|
{
|
|
private readonly IConnectionString _connectionString;
|
|
private readonly ILogger<RefillRepository> _logger;
|
|
public RefillRepository(IConnectionString connectionString, ILogger<RefillRepository> logger)
|
|
{
|
|
_connectionString = connectionString;
|
|
_logger = logger;
|
|
}
|
|
public void CreateRefill(Refill refill)
|
|
{
|
|
_logger.LogInformation("Добавление объекта");
|
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(refill));
|
|
|
|
try
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
var queryInsert = @"INSERT INTO Refill (Refill_Date, Refill_Amount, Fuel_ID, Car_ID)
|
|
VALUES (@Refill_Date, @Refill_Amount, @Fuel_ID, @Car_ID)";
|
|
connection.Execute(queryInsert, refill);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public IEnumerable<Refill> ReadRefills(DateTime? dateFrom = null, DateTime? dateTo = null, int? fuelId = null, int? carId = null)
|
|
{
|
|
_logger.LogInformation("Получение всех объектов");
|
|
try
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
var querySelect = "SELECT * FROM Refill";
|
|
var refills = connection.Query<Refill>(querySelect);
|
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(refills));
|
|
return refills;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
|
throw;
|
|
}
|
|
}
|
|
}
|