From d18fe8d6597b84e6b94e2e8e08f67cc6964d26e6 Mon Sep 17 00:00:00 2001
From: Petek1234 <149153720+Petek1234@users.noreply.github.com>
Date: Fri, 22 Nov 2024 08:59:01 +0400
Subject: [PATCH 1/2] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=B0=202=20=D0=BD=D0=B5?=
=?UTF-8?q?=20=D0=B4=D0=BE=20=D0=BA=D0=BE=D0=BD=D1=86=D0=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
GasStation/GasStation/GasStation.csproj | 20 +++-
GasStation/GasStation/Program.cs | 21 ++++
.../Repositories/IConnectionString.cs | 12 ++
.../Repositories/IFuelRepository.cs | 7 +-
.../Implementations/CashierRepository.cs | 107 +++++++++++++++++-
.../Implementations/ConnectionString.cs | 6 +
.../Implementations/FuelRepository.cs | 105 +++++++++++++++--
GasStation/GasStation/appsettings.json | 15 +++
8 files changed, 273 insertions(+), 20 deletions(-)
create mode 100644 GasStation/GasStation/Repositories/IConnectionString.cs
create mode 100644 GasStation/GasStation/Repositories/Implementations/ConnectionString.cs
create mode 100644 GasStation/GasStation/appsettings.json
diff --git a/GasStation/GasStation/GasStation.csproj b/GasStation/GasStation/GasStation.csproj
index f2b439c..92f6c9a 100644
--- a/GasStation/GasStation/GasStation.csproj
+++ b/GasStation/GasStation/GasStation.csproj
@@ -2,7 +2,7 @@
WinExe
- net7.0-windows
+ net8.0-windows7.0
enable
true
enable
@@ -19,7 +19,19 @@
+
+
+
+
+
+
+
+
+
+
+
+
@@ -37,4 +49,10 @@
+
+
+ PreserveNewest
+
+
+
\ No newline at end of file
diff --git a/GasStation/GasStation/Program.cs b/GasStation/GasStation/Program.cs
index e6e7fb9..692bb81 100644
--- a/GasStation/GasStation/Program.cs
+++ b/GasStation/GasStation/Program.cs
@@ -2,6 +2,10 @@ using GasStation.Repositories.Implementations;
using GasStation.Repositories;
using Unity.Lifetime;
using Unity;
+using Unity.Microsoft.Logging;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.Logging;
+using Serilog;
namespace GasStation
{
@@ -23,13 +27,30 @@ namespace GasStation
{
var container = new UnityContainer();
+ container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
+
container.RegisterType ();
container.RegisterType ();
container.RegisterType ();
container.RegisterType ();
container.RegisterType ();
+ container.RegisterType();
+
return container;
}
+
+ private static LoggerFactory CreateLoggerFactory()
+ {
+ var loggerFactory = new LoggerFactory();
+ loggerFactory.AddSerilog(new LoggerConfiguration()
+ .ReadFrom.Configuration(new ConfigurationBuilder()
+ .SetBasePath(Directory.GetCurrentDirectory())
+ .AddJsonFile("appsettings.json")
+ .Build())
+ .CreateLogger());
+ return loggerFactory;
+ }
+
}
}
\ No newline at end of file
diff --git a/GasStation/GasStation/Repositories/IConnectionString.cs b/GasStation/GasStation/Repositories/IConnectionString.cs
new file mode 100644
index 0000000..d12e9b5
--- /dev/null
+++ b/GasStation/GasStation/Repositories/IConnectionString.cs
@@ -0,0 +1,12 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace GasStation.Repositories;
+
+public interface IConnectionString
+{
+ public string ConnectionString { get; }
+}
diff --git a/GasStation/GasStation/Repositories/IFuelRepository.cs b/GasStation/GasStation/Repositories/IFuelRepository.cs
index 5d80d21..74cbf61 100644
--- a/GasStation/GasStation/Repositories/IFuelRepository.cs
+++ b/GasStation/GasStation/Repositories/IFuelRepository.cs
@@ -1,9 +1,4 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using GasStation.Entities;
+using GasStation.Entities;
namespace GasStation.Repositories;
diff --git a/GasStation/GasStation/Repositories/Implementations/CashierRepository.cs b/GasStation/GasStation/Repositories/Implementations/CashierRepository.cs
index d264b5e..ed897e4 100644
--- a/GasStation/GasStation/Repositories/Implementations/CashierRepository.cs
+++ b/GasStation/GasStation/Repositories/Implementations/CashierRepository.cs
@@ -1,34 +1,133 @@
using System;
using System.Collections.Generic;
+using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
+using Dapper;
using GasStation.Entities;
using GasStation.Entities.Enums;
+using Microsoft.Extensions.Logging;
+using Newtonsoft.Json;
+using Npgsql;
namespace GasStation.Repositories.Implementations;
public class CashierRepository : ICashierRepository
{
+ private readonly IConnectionString _connectionString;
+ private readonly ILogger _logger;
+
+ public CashierRepository(IConnectionString connectionString, ILogger logger)
+ {
+ _connectionString = connectionString;
+ _logger = logger;
+ }
+
public void CreateCashier(Cashier cashier)
- {
+ {
+ _logger.LogInformation("Добавление объекта");
+ _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(cashier));
+
+ try
+ {
+ using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
+ var queryInsert = @"
+ INSERT INTO Cashier (FirstName, LastName, CashierPost)
+ VALUES (@FirstName, @LastName, @CashierPost)";
+ connection.Execute(queryInsert, cashier);
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при добавлении объекта");
+ throw;
+ }
}
public void DeleteCashier(int id)
{
+ _logger.LogInformation("Удаление объекта");
+ _logger.LogDebug("Объект: {id}", id);
+
+ try
+ {
+ using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
+ var queryDelete = @"
+ DELETE FROM Cashier
+ WHERE Id=@id";
+ connection.Execute(queryDelete, new { id });
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при удалении объекта");
+ throw;
+ }
}
public Cashier ReadCashierById(int id)
{
- return Cashier.CreateEntity(0, string.Empty, string.Empty, CashierPost.None);
+ _logger.LogInformation("Получение объекта по идентификатору");
+ _logger.LogDebug("Объект: {id}", id);
+
+ try
+ {
+ using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
+ var querySelect = @"
+ SELECT * FROM Cashier
+ WHERE Id=@id";
+ var cashier = connection.QueryFirst(querySelect, new { id });
+ _logger.LogDebug("Найденный объект: {json}",
+ JsonConvert.SerializeObject(cashier));
+ return cashier;
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при поиске объекта");
+ throw;
+ }
}
public IEnumerable ReadCashiers()
{
- return [];
+ _logger.LogInformation("Получение всех объектов");
+
+ try
+ {
+ using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
+ var querySelect = "SELECT * FROM Cashier";
+ var cashier = connection.Query(querySelect);
+ _logger.LogDebug("Полученные объекты: {json}",
+ JsonConvert.SerializeObject(cashier));
+ return cashier;
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при чтении объектов");
+ throw;
+ }
}
public void UpdateCashier(Cashier cashier)
- {
+ {
+ _logger.LogInformation("Редактирование объекта");
+ _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(cashier));
+
+ try
+ {
+ using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
+ var queryUpdate = @"
+ UPDATE Cashier
+ SET
+ FirstName=@FirstName,
+ LastName=@LastName,
+ CashierPost=@CashierPost
+ WHERE Id=@Id";
+ connection.Execute(queryUpdate, cashier);
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при редактировании объекта");
+ throw;
+ }
}
}
diff --git a/GasStation/GasStation/Repositories/Implementations/ConnectionString.cs b/GasStation/GasStation/Repositories/Implementations/ConnectionString.cs
new file mode 100644
index 0000000..569b67e
--- /dev/null
+++ b/GasStation/GasStation/Repositories/Implementations/ConnectionString.cs
@@ -0,0 +1,6 @@
+namespace GasStation.Repositories.Implementations;
+
+public class ConnectionString : IConnectionString
+{
+ string IConnectionString.ConnectionString => "Server=localhost,5432;Database=egov;Uid=postgres;Pwd=postgres;";
+}
diff --git a/GasStation/GasStation/Repositories/Implementations/FuelRepository.cs b/GasStation/GasStation/Repositories/Implementations/FuelRepository.cs
index ac9ff6e..42485ac 100644
--- a/GasStation/GasStation/Repositories/Implementations/FuelRepository.cs
+++ b/GasStation/GasStation/Repositories/Implementations/FuelRepository.cs
@@ -1,34 +1,121 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using GasStation.Entities;
-using GasStation.Entities.Enums;
+using GasStation.Entities;
+using Microsoft.Extensions.Logging;
+using Newtonsoft.Json;
+using Npgsql;
+using Dapper;
+using System.Data.SqlClient;
+
namespace GasStation.Repositories.Implementations;
public class FuelRepository : IFuelRepository
{
+ private readonly IConnectionString _connectionString;
+ private readonly ILogger _logger;
+
+ public FuelRepository(IConnectionString connectionString, ILogger 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 (Cost, Type)
+ VALUES (@Cost, @Type)";
+ 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
+ WHERE Id=@id";
+ connection.Execute(queryDelete, new { id });
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при удалении объекта");
+ throw;
+ }
}
public Fuel ReadFuelById(int id)
{
- return Fuel.CreateEntity(0, 0, FuelType.Fuel_diesel);
+ _logger.LogInformation("Получение объекта по идентификатору");
+ _logger.LogDebug("Объект: {id}", id);
+ try
+ {
+ using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
+ var querySelect = @"
+ SELECT * FROM Fuel
+ WHERE Id=@id";
+ var fuel = connection.QueryFirst(querySelect, new { id });
+ _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(fuel));
+ return fuel;
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при поиске объекта");
+ throw;
+ }
}
public IEnumerable ReadFuels()
{
- return [];
+ _logger.LogInformation("Получение всех объектов");
+ try
+ {
+ using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
+ var querySelect = "SELECT * FROM Fuel";
+ var fuels = connection.Query(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
+ SET
+ Cost=@Cost,
+ Type=@Type
+ WHERE Id=@Id";
+ connection.Execute(queryUpdate, fuel);
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при редактировании объекта");
+ throw;
+ }
}
}
diff --git a/GasStation/GasStation/appsettings.json b/GasStation/GasStation/appsettings.json
new file mode 100644
index 0000000..0f5bd9b
--- /dev/null
+++ b/GasStation/GasStation/appsettings.json
@@ -0,0 +1,15 @@
+{
+ "Serilog": {
+ "Using": [ "Serilog.Sinks.File" ],
+ "MinimumLevel": "Debug",
+ "WriteTo": [
+ {
+ "Name": "File",
+ "Args": {
+ "path": "Logs/gas_log.txt",
+ "rollingInterval": "Day"
+ }
+ }
+ ]
+ }
+}
\ No newline at end of file
--
2.25.1
From 89827510029678c66f2bab2fa68f70b60bb630a2 Mon Sep 17 00:00:00 2001
From: Petek1234 <149153720+Petek1234@users.noreply.github.com>
Date: Sun, 1 Dec 2024 15:50:35 +0400
Subject: [PATCH 2/2] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?=
=?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=202=20=D0=B3=D0=BE=D1=82=D0=BE?=
=?UTF-8?q?=D0=B2=D0=B0=D1=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../GasStation/Forms/FormFuelSale.Designer.cs | 1 -
GasStation/GasStation/Forms/FormFuelSale.cs | 3 +-
.../Implementations/FuelSalesRepository.cs | 90 ++++++++++++++++-
.../Implementations/SupplierRepository.cs | 98 ++++++++++++++++++-
.../SuppliersFuelRepository.cs | 87 +++++++++++++++-
5 files changed, 273 insertions(+), 6 deletions(-)
diff --git a/GasStation/GasStation/Forms/FormFuelSale.Designer.cs b/GasStation/GasStation/Forms/FormFuelSale.Designer.cs
index 69ef3de..58e73f2 100644
--- a/GasStation/GasStation/Forms/FormFuelSale.Designer.cs
+++ b/GasStation/GasStation/Forms/FormFuelSale.Designer.cs
@@ -80,7 +80,6 @@
dataGridView.MultiSelect = false;
dataGridView.Name = "dataGridView";
dataGridView.RowHeadersVisible = false;
- dataGridView.RowTemplate.Height = 25;
dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView.Size = new Size(264, 223);
dataGridView.TabIndex = 0;
diff --git a/GasStation/GasStation/Forms/FormFuelSale.cs b/GasStation/GasStation/Forms/FormFuelSale.cs
index de69446..3e2440f 100644
--- a/GasStation/GasStation/Forms/FormFuelSale.cs
+++ b/GasStation/GasStation/Forms/FormFuelSale.cs
@@ -41,8 +41,9 @@ public partial class FormFuelSale : Form
{
throw new Exception("Имеются незаполненные поля");
}
+ int countQuantity = int.Parse(dataGridView.Rows[0].Cells["ColumnQuantity"].Value?.ToString() ?? "0");
_fuelSalesRepository.CreateFuelSale(FuelSale.CreateElement(0,
- (int)comboBoxCashier.SelectedValue!, dateTimePickerDate.Value, 0, CreateListFuelFuelSaleFromDataGrid()));
+ (int)comboBoxCashier.SelectedValue!, dateTimePickerDate.Value, countQuantity, CreateListFuelFuelSaleFromDataGrid()));
Close();
}
catch (Exception ex)
diff --git a/GasStation/GasStation/Repositories/Implementations/FuelSalesRepository.cs b/GasStation/GasStation/Repositories/Implementations/FuelSalesRepository.cs
index c031429..bb081cf 100644
--- a/GasStation/GasStation/Repositories/Implementations/FuelSalesRepository.cs
+++ b/GasStation/GasStation/Repositories/Implementations/FuelSalesRepository.cs
@@ -1,24 +1,112 @@
using System;
using System.Collections.Generic;
+using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
+using Dapper;
using GasStation.Entities;
+using Microsoft.Extensions.Logging;
+using Newtonsoft.Json;
+using Npgsql;
namespace GasStation.Repositories.Implementations;
public class FuelSalesRepository : IFuelSalesRepository
{
+ private readonly IConnectionString _connectionString;
+ private readonly ILogger _logger;
+
+ public FuelSalesRepository(IConnectionString connectionString, ILogger logger)
+ {
+ _connectionString = connectionString;
+ _logger = logger;
+ }
+
public void CreateFuelSale(FuelSale fuelSale)
{
+ _logger.LogInformation("Добавление объекта");
+ _logger.LogDebug("Объект: {json}",
+ JsonConvert.SerializeObject(fuelSale));
+ try
+ {
+ using var connection = new
+ NpgsqlConnection(_connectionString.ConnectionString);
+ connection.Open();
+ using var transaction = connection.BeginTransaction();
+ var queryInsert = @"
+ INSERT INTO FuelSale (CashierId, SaleDate, TotalPrice)
+ VALUES (@CashierId, @SaleDate, @TotalPrice);
+ SELECT MAX(Id) FROM FuelSale";
+ var fuelSaleId =
+ connection.QueryFirst(queryInsert, fuelSale, transaction);
+ var querySubInsert = @"
+ INSERT INTO FuelFuelSale (FuelSaleId, FuelId, Quantity, Price)
+ VALUES (@FuelSaleId, @FuelId, @Quantity, @Price)";
+ foreach (var elem in fuelSale.FuelFuelSale)
+ {
+ connection.Execute(querySubInsert, new
+ {
+ fuelSaleId,
+ elem.FuelId,
+ elem.Quantity,
+ elem.Price
+ }, transaction);
+ }
+ transaction.Commit();
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при добавлении объекта");
+ throw;
+ }
}
public void DeleteFuelSale(int id)
{
+ _logger.LogInformation("Удаление объекта");
+ _logger.LogDebug("Объект: {id}", id);
+ try
+ {
+ using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
+ connection.Open();
+ using var transaction = connection.BeginTransaction();
+
+ var queryDeleteSub = @"
+ DELETE FROM FuelFuelSale
+ WHERE FuelSaleId = @id";
+ connection.Execute(queryDeleteSub, new { id }, transaction);
+
+ var queryDelete = @"
+ DELETE FROM FuelSale
+ WHERE Id = @id";
+ connection.Execute(queryDelete, new { id }, transaction);
+
+ transaction.Commit();
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при удалении объекта");
+ throw;
+ }
}
public IEnumerable ReadFuelSale(DateTime? dateFrom = null, DateTime? dateTo = null, int? gasStationId = null, int? fuelId = null, int? cashierId = null)
{
- return [];
+ _logger.LogInformation("Получение всех объектов");
+ try
+ {
+ using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
+ var querySelect = "SELECT * FROM FuelSale";
+ var fuelSales = connection.Query(querySelect);
+ _logger.LogDebug("Полученные объекты: {json}",
+ JsonConvert.SerializeObject(fuelSales));
+ return fuelSales;
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при чтении объектов");
+ throw;
+ }
}
}
diff --git a/GasStation/GasStation/Repositories/Implementations/SupplierRepository.cs b/GasStation/GasStation/Repositories/Implementations/SupplierRepository.cs
index 739b15d..a953788 100644
--- a/GasStation/GasStation/Repositories/Implementations/SupplierRepository.cs
+++ b/GasStation/GasStation/Repositories/Implementations/SupplierRepository.cs
@@ -4,32 +4,126 @@ using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
+using Dapper;
using GasStation.Entities;
using GasStation.Entities.Enums;
+using Microsoft.Extensions.Logging;
+using Newtonsoft.Json;
+using Npgsql;
namespace GasStation.Repositories.Implementations;
public class SupplierRepository : ISupplierRepository
{
+ private readonly IConnectionString _connectionString;
+ private readonly ILogger _logger;
+
+ public SupplierRepository(IConnectionString connectionString, ILogger 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 (Brand, Types)
+ VALUES (@Brand, @Types)";
+ 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)
{
- return Supplier.CreateEntity(0, string.Empty, SupppliersFuelType.Fuel_diesel);
+ _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(querySelect, new { id });
+ _logger.LogDebug("Найденный объект: {json}",
+ JsonConvert.SerializeObject(supplier));
+ return supplier;
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при поиске объекта");
+ throw;
+ }
}
public IEnumerable ReadSuppliers()
{
- return [];
+ _logger.LogInformation("Получение всех объектов");
+ try
+ {
+ using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
+ var querySelect = "SELECT * FROM Supplier";
+ var suppliers = connection.Query(querySelect);
+ _logger.LogDebug("Полученные объекты: {json}",
+ JsonConvert.SerializeObject(suppliers));
+ return suppliers;
+ }
+ 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
+ Brand=@Brand,
+ Types=@Types
+ WHERE Id=@Id";
+ connection.Execute(queryUpdate, supplier);
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при редактировании объекта");
+ throw;
+ }
}
}
diff --git a/GasStation/GasStation/Repositories/Implementations/SuppliersFuelRepository.cs b/GasStation/GasStation/Repositories/Implementations/SuppliersFuelRepository.cs
index a198f70..20ad028 100644
--- a/GasStation/GasStation/Repositories/Implementations/SuppliersFuelRepository.cs
+++ b/GasStation/GasStation/Repositories/Implementations/SuppliersFuelRepository.cs
@@ -3,22 +3,107 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
+using Dapper;
using GasStation.Entities;
+using Microsoft.Extensions.Logging;
+using Newtonsoft.Json;
+using Npgsql;
namespace GasStation.Repositories.Implementations;
public class SuppliersFuelRepository : ISuppliersFuelRepository
{
+ private readonly IConnectionString _connectionString;
+ private readonly ILogger _logger;
+
+ public SuppliersFuelRepository(IConnectionString connectionString, ILogger logger)
+ {
+ _connectionString = connectionString;
+ _logger = logger;
+ }
+
public void CreateSuppliersFuel(SuppliersFuel suppliersFuel)
{
+ _logger.LogInformation("Добавление объекта");
+ _logger.LogDebug("Объект: {json}",
+ JsonConvert.SerializeObject(suppliersFuel));
+ try
+ {
+ using var connection = new
+ NpgsqlConnection(_connectionString.ConnectionString);
+ connection.Open();
+ using var transaction = connection.BeginTransaction();
+ var queryInsert = @"
+ INSERT INTO SuppliersFuel (SuppliersId, Date)
+ VALUES (@SuppliersId, @Date);
+ SELECT MAX(Id) FROM SuppliersFuel";
+ var suppliersFuelId =
+ connection.QueryFirst(queryInsert, suppliersFuel, transaction);
+ var querySubInsert = @"
+ INSERT INTO SuppliersFuelFuel (SuppliersFuelId, FuelId, Quantity)
+ VALUES (@SuppliersFuelId, @FuelId, @Quantity)";
+ foreach (var elem in suppliersFuel.SuppliersFuelFuel)
+ {
+ connection.Execute(querySubInsert, new
+ {
+ suppliersFuelId,
+ elem.FuelId,
+ elem.Quantity
+ }, transaction);
+ }
+ transaction.Commit();
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при добавлении объекта");
+ throw;
+ }
}
public void DeleteSuppliersFuel(int id)
{
+ _logger.LogInformation("Удаление объекта");
+ _logger.LogDebug("Объект: {id}", id);
+ try
+ {
+ using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
+ connection.Open();
+ using var transaction = connection.BeginTransaction();
+ var queryDeleteSub = @"
+ DELETE FROM SuppliersFuelFuel
+ WHERE SuppliersFuelId = @id";
+ connection.Execute(queryDeleteSub, new { id }, transaction);
+
+ var queryDelete = @"
+ DELETE FROM SuppliersFuel
+ WHERE Id = @id";
+ connection.Execute(queryDelete, new { id }, transaction);
+
+ transaction.Commit();
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при удалении объекта");
+ throw;
+ }
}
public IEnumerable ReadSuppliersFuels(DateTime? dateFrom = null, DateTime? dateTo = null, int? suppliersId = null, int? fuelId = null)
{
- return [];
+ _logger.LogInformation("Получение всех объектов");
+ try
+ {
+ using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
+ var querySelect = "SELECT * FROM SuppliersFuel";
+ var suppliersFuels = connection.Query(querySelect);
+ _logger.LogDebug("Полученные объекты: {json}",
+ JsonConvert.SerializeObject(suppliersFuels));
+ return suppliersFuels;
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при чтении объектов");
+ throw;
+ }
}
}
--
2.25.1