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] =?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; + } } }