105 lines
3.4 KiB
C#
105 lines
3.4 KiB
C#
using Dapper;
|
|
using GasStation.Entities;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using Npgsql;
|
|
using static System.Runtime.InteropServices.JavaScript.JSType;
|
|
|
|
namespace GasStation.Repositories.Implementations;
|
|
|
|
public class SupplyRepository : ISupplyRepository
|
|
{
|
|
private readonly IConnectionString _connectionString;
|
|
|
|
private readonly ILogger<SupplyRepository> _logger;
|
|
|
|
public SupplyRepository(IConnectionString connectionString, ILogger<SupplyRepository> 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<Supply> ReadSupply(DateTime? supplyDate = null, int? supplierID = null, int? productID = null, int? count = null)
|
|
{
|
|
_logger.LogInformation("Получение всех объектов");
|
|
try
|
|
{
|
|
var builder = new QueryBuilder();
|
|
if (supplyDate.HasValue)
|
|
{
|
|
builder.AddCondition("s.SupplyDate = @supplyDate");
|
|
}
|
|
if (supplierID.HasValue)
|
|
{
|
|
builder.AddCondition("s.SupplierID = @supplierID");
|
|
}
|
|
if (productID.HasValue)
|
|
{
|
|
builder.AddCondition("s.ProductID = @productID");
|
|
}
|
|
if (count.HasValue)
|
|
{
|
|
builder.AddCondition("s.Count = @count");
|
|
}
|
|
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
var querySelect = @$"SELECT
|
|
s.*,
|
|
CONCAT(p.ProductType, ' ', p.ProductName) as ProductName,
|
|
sup.SupplierName as SupplierName
|
|
FROM Supply s
|
|
LEFT JOIN Product p on p.Id = s.ProductId
|
|
LEFT JOIN Supplier sup on sup.Id = s.SupplierId
|
|
{builder.Build()}";
|
|
var supply = connection.Query<Supply>(querySelect, new { supplyDate, supplierID, productID, count });
|
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(supply));
|
|
return supply;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
|
throw;
|
|
}
|
|
}
|
|
}
|