Лабораторная 2 готовая
This commit is contained in:
parent
e0fa1ea187
commit
8982751002
@ -80,7 +80,6 @@
|
|||||||
dataGridView.MultiSelect = false;
|
dataGridView.MultiSelect = false;
|
||||||
dataGridView.Name = "dataGridView";
|
dataGridView.Name = "dataGridView";
|
||||||
dataGridView.RowHeadersVisible = false;
|
dataGridView.RowHeadersVisible = false;
|
||||||
dataGridView.RowTemplate.Height = 25;
|
|
||||||
dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
|
dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
|
||||||
dataGridView.Size = new Size(264, 223);
|
dataGridView.Size = new Size(264, 223);
|
||||||
dataGridView.TabIndex = 0;
|
dataGridView.TabIndex = 0;
|
||||||
|
@ -41,8 +41,9 @@ public partial class FormFuelSale : Form
|
|||||||
{
|
{
|
||||||
throw new Exception("Имеются незаполненные поля");
|
throw new Exception("Имеются незаполненные поля");
|
||||||
}
|
}
|
||||||
|
int countQuantity = int.Parse(dataGridView.Rows[0].Cells["ColumnQuantity"].Value?.ToString() ?? "0");
|
||||||
_fuelSalesRepository.CreateFuelSale(FuelSale.CreateElement(0,
|
_fuelSalesRepository.CreateFuelSale(FuelSale.CreateElement(0,
|
||||||
(int)comboBoxCashier.SelectedValue!, dateTimePickerDate.Value, 0, CreateListFuelFuelSaleFromDataGrid()));
|
(int)comboBoxCashier.SelectedValue!, dateTimePickerDate.Value, countQuantity, CreateListFuelFuelSaleFromDataGrid()));
|
||||||
Close();
|
Close();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
@ -1,24 +1,112 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Data.SqlClient;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Dapper;
|
||||||
using GasStation.Entities;
|
using GasStation.Entities;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
|
||||||
namespace GasStation.Repositories.Implementations;
|
namespace GasStation.Repositories.Implementations;
|
||||||
|
|
||||||
public class FuelSalesRepository : IFuelSalesRepository
|
public class FuelSalesRepository : IFuelSalesRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<FuelSalesRepository> _logger;
|
||||||
|
|
||||||
|
public FuelSalesRepository(IConnectionString connectionString, ILogger<FuelSalesRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateFuelSale(FuelSale fuelSale)
|
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<int>(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)
|
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<FuelSale> ReadFuelSale(DateTime? dateFrom = null, DateTime? dateTo = null, int? gasStationId = null, int? fuelId = null, int? cashierId = null)
|
public IEnumerable<FuelSale> 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<FuelSale>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(fuelSales));
|
||||||
|
return fuelSales;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,32 +4,126 @@ using System.Diagnostics.Contracts;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Dapper;
|
||||||
using GasStation.Entities;
|
using GasStation.Entities;
|
||||||
using GasStation.Entities.Enums;
|
using GasStation.Entities.Enums;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
|
||||||
namespace GasStation.Repositories.Implementations;
|
namespace GasStation.Repositories.Implementations;
|
||||||
|
|
||||||
public class SupplierRepository : ISupplierRepository
|
public class SupplierRepository : ISupplierRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<SupplierRepository> _logger;
|
||||||
|
|
||||||
|
public SupplierRepository(IConnectionString connectionString, ILogger<SupplierRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateSupplier(Supplier supplier)
|
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)
|
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)
|
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<Supplier>(querySelect, new { id });
|
||||||
|
_logger.LogDebug("Найденный объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(supplier));
|
||||||
|
return supplier;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Supplier> ReadSuppliers()
|
public IEnumerable<Supplier> ReadSuppliers()
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Supplier";
|
||||||
|
var suppliers = connection.Query<Supplier>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(suppliers));
|
||||||
|
return suppliers;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateSupplier(Supplier supplier)
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,22 +3,107 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Dapper;
|
||||||
using GasStation.Entities;
|
using GasStation.Entities;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
|
||||||
namespace GasStation.Repositories.Implementations;
|
namespace GasStation.Repositories.Implementations;
|
||||||
|
|
||||||
public class SuppliersFuelRepository : ISuppliersFuelRepository
|
public class SuppliersFuelRepository : ISuppliersFuelRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<SuppliersFuelRepository> _logger;
|
||||||
|
|
||||||
|
public SuppliersFuelRepository(IConnectionString connectionString, ILogger<SuppliersFuelRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateSuppliersFuel(SuppliersFuel suppliersFuel)
|
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<int>(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)
|
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<SuppliersFuel> ReadSuppliersFuels(DateTime? dateFrom = null, DateTime? dateTo = null, int? suppliersId = null, int? fuelId = null)
|
public IEnumerable<SuppliersFuel> 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<SuppliersFuel>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(suppliersFuels));
|
||||||
|
return suppliersFuels;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user