PIbd-23_Kulikova_D.R._LabWork_2 #3
@ -9,16 +9,16 @@ namespace ProjectGasStation.Entities.Enums;
|
||||
[Flags]
|
||||
public enum Category
|
||||
{
|
||||
Fuel = 0,
|
||||
Gas = 1,
|
||||
LiquidFuel = 2,
|
||||
None = 0,
|
||||
Fuel = 1,
|
||||
Oils = 2,
|
||||
Chemicals = 4,
|
||||
Oils = 8,
|
||||
CareProducts = 16,
|
||||
CareForInterior = 32,
|
||||
CareForBody = 64,
|
||||
MineralOils = 128,
|
||||
SyntheticOils = 256,
|
||||
None = 512
|
||||
Gas = 8,
|
||||
LiquidFuel = 16,
|
||||
CareProducts = 32,
|
||||
CareForInterior = 64,
|
||||
CareForBody = 128,
|
||||
MineralOils = 256,
|
||||
SyntheticOils = 512
|
||||
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ public class Sale
|
||||
public int Id { get; set; }
|
||||
public DateTime DateTime { get; set; }
|
||||
public int WorkerId { get; set; }
|
||||
public IEnumerable<SaleReceiptProduct> SaleProduct { get; set; } = [];
|
||||
public IEnumerable<SaleReceiptProduct> SaleProducts { get; set; } = [];
|
||||
|
||||
|
||||
public static Sale CreateOperation(int id, int workerId, IEnumerable<SaleReceiptProduct> saleProduct)
|
||||
@ -21,7 +21,7 @@ public class Sale
|
||||
Id = id,
|
||||
DateTime = DateTime.Now,
|
||||
WorkerId = workerId,
|
||||
SaleProduct = saleProduct
|
||||
SaleProducts = saleProduct
|
||||
|
||||
};
|
||||
}
|
||||
|
@ -29,13 +29,12 @@ namespace ProjectGasStation.Forms
|
||||
if (supplier == null)
|
||||
{
|
||||
throw new InvalidDataException(nameof(supplier));
|
||||
|
||||
textBoxSupplierName.Text = supplier.Name;
|
||||
textBoxSupplierPhoneNumber.Text = supplier.PhoneNumber;
|
||||
textBoxSupplierAddress.Text = supplier.Address;
|
||||
_supplierId = value;
|
||||
}
|
||||
|
||||
textBoxSupplierName.Text = supplier.Name;
|
||||
textBoxSupplierPhoneNumber.Text = supplier.PhoneNumber;
|
||||
textBoxSupplierAddress.Text = supplier.Address;
|
||||
_supplierId = value;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -72,6 +71,7 @@ namespace ProjectGasStation.Forms
|
||||
{
|
||||
_supplierRepository.CreateSupplier(CreateSupplier(0));
|
||||
}
|
||||
Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
@ -163,7 +163,7 @@
|
||||
<data name="buttonDel.BackgroundImage" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||
<value>
|
||||
iVBORw0KGgoAAAANSUhEUgAAA8cAAAPHCAIAAACKbawNAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAO
|
||||
vQAADr0BR/uQrQAA/7JJREFUeF7s/Ql4ZOt+1/euKql773OOz9nn7Km7NXX33ufYgA0hiQEn12DDg3nM
|
||||
vAAADrwBlbxySQAA/7JJREFUeF7s/Ql4ZOt+1/euKql773OOz9nn7Km7NXX33ufYgA0hiQEn12DDg3nM
|
||||
BYNvgHuBSwiEJAQIMeMDgQuX4fgBEgMhNpAbB4whDPZzc9m71ZpnqVtSd6sndbfUk8aaJNWwqmrVXCXp
|
||||
/tb6v3vt1SWpJ01V6/19ntflpWq1TquqtNZXa7/1LmOXiIiIiIgOh1VNRERERHRYrGoiIiIiosNiVRMR
|
||||
ERERHRarmoiIiIjosFjVRERERESHxaomIiIiIjosVjURERER0WGxqomIiIiIDotVTURERER0WKxqIiIi
|
||||
@ -1468,7 +1468,7 @@
|
||||
<data name="buttonAdd.BackgroundImage" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||
<value>
|
||||
iVBORw0KGgoAAAANSUhEUgAAA80AAAPNCAIAAAA81B+iAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAO
|
||||
vQAADr0BR/uQrQAA/7JJREFUeF7s/fd/G9l+5wk//8f+MOGGzt0SM5X62p6d3Z2d2ZmdxzvPeHftfd17
|
||||
vAAADrwBlbxySQAA/7JJREFUeF7s/fd/G9l+5wk//8f+MOGGzt0SM5X62p6d3Z2d2ZmdxzvPeHftfd17
|
||||
u1stiQkg1X3X9ozDBPuOfce+yel67HG6HudpBYoSJVIkAVQVSIpUoCiKyoERoSIyxedb9S3VhUBSTYoA
|
||||
AdT5vF+fV+mgCIJAhVNvHJ065/+zDgAAAAAAACg38GwAAAAAAADKDzwbAAAAAACA8gPPBgAAAAAAoPzA
|
||||
swEAAAAAACg/8GwAAAAAAADKDzwbAAAAAACA8gPPBgAAAAAAoPzAswEAAAAAACg/8GwAAAAAAADKDzwb
|
||||
|
@ -89,6 +89,7 @@ namespace ProjectGasStation.Forms
|
||||
form.ShowDialog();
|
||||
LoadList();
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message, "Ошибка при изменении", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
|
@ -1,6 +1,11 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using ProjectGasStation.Repositories;
|
||||
using ProjectGasStation.Repositories.Implementations;
|
||||
using Serilog;
|
||||
using Unity;
|
||||
using Unity.Lifetime;
|
||||
using Unity.Microsoft.Logging;
|
||||
|
||||
namespace ProjectGasStation
|
||||
{
|
||||
@ -22,14 +27,34 @@ namespace ProjectGasStation
|
||||
{
|
||||
var container = new UnityContainer();
|
||||
|
||||
container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
|
||||
|
||||
container.RegisterType<IProductRepository, ProductRepository>();
|
||||
container.RegisterType<IReceiptRepository, ReceiptRepository>();
|
||||
container.RegisterType<ISaleRepository, SaleRepository>();
|
||||
container.RegisterType<ISupplierRepository, SupplierRepository>();
|
||||
container.RegisterType<IWorkerRepository, WorkerRepository>();
|
||||
|
||||
container.RegisterType<IConectionString, ConectionString>(new SingletonLifetimeManager());
|
||||
|
||||
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -9,7 +9,19 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Dapper" Version="2.1.35" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="Npgsql" Version="9.0.2" />
|
||||
<PackageReference Include="Serilog" Version="4.2.0" />
|
||||
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.4" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
|
||||
<PackageReference Include="System.Data.SqlClient" Version="4.9.0" />
|
||||
<PackageReference Include="Unity" Version="5.11.10" />
|
||||
<PackageReference Include="Unity.Microsoft.Logging" Version="5.11.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@ -27,4 +39,10 @@
|
||||
</EmbeddedResource>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="appsettings.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectGasStation.Repositories;
|
||||
|
||||
public interface IConectionString
|
||||
{
|
||||
public string ConectionString { get; }
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectGasStation.Repositories.Implementations;
|
||||
|
||||
internal class ConectionString : IConectionString
|
||||
{
|
||||
string IConectionString.ConectionString => "Server=localhost, 5432;Database=gas_station;Uid=postgres;Pwd=13081306";
|
||||
|
||||
}
|
@ -1,6 +1,11 @@
|
||||
using ProjectGasStation.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectGasStation.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@ -9,27 +14,127 @@ namespace ProjectGasStation.Repositories.Implementations;
|
||||
|
||||
internal class ProductRepository : IProductRepository
|
||||
{
|
||||
|
||||
public readonly IConectionString _conectionString;
|
||||
private readonly ILogger<ProductRepository> _logger;
|
||||
|
||||
public ProductRepository(IConectionString conectionString, ILogger<ProductRepository> logger)
|
||||
{
|
||||
_conectionString = conectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateProduct(Product product)
|
||||
{
|
||||
|
||||
}
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(product));
|
||||
|
||||
public void DeleteProduct(int id)
|
||||
{
|
||||
}
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_conectionString.ConectionString);
|
||||
connection.Open();
|
||||
|
||||
public IEnumerable<Product> ReadProduct()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public Product ReadProductById(int id)
|
||||
{
|
||||
return Product.CreatEntity(0, string.Empty, 0, Entities.Enums.Category.None);
|
||||
var queryInsert = @" INSERT INTO Products (Name, Price, Category)
|
||||
VALUES (@Name, @Price, @Category)";
|
||||
connection.Execute(queryInsert, product);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateProduct(Product product)
|
||||
{
|
||||
|
||||
_logger.LogInformation("Редакитрование объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(product));
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_conectionString.ConectionString);
|
||||
connection.Open();
|
||||
|
||||
var queryUpdate = @" UPDATE Products
|
||||
SET
|
||||
Name = @Name,
|
||||
Price = @Price,
|
||||
Category = @Category
|
||||
WHERE ID = @Id";
|
||||
connection.Execute(queryUpdate, product);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteProduct(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_conectionString.ConectionString);
|
||||
connection.Open();
|
||||
|
||||
var queryDelete = @" DELETE FROM Products
|
||||
WHERE ID = @id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Product ReadProductById(int id)
|
||||
{
|
||||
_logger.LogInformation("Получение объекта по идентификатору");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_conectionString.ConectionString);
|
||||
|
||||
|
||||
var querySelect = @" SELECT * FROM Products
|
||||
WHERE ID = @id";
|
||||
var product = connection.QueryFirst<Product>(querySelect, new { id });
|
||||
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(product));
|
||||
|
||||
return product;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public IEnumerable<Product> ReadProduct()
|
||||
{
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_conectionString.ConectionString);
|
||||
connection.Open();
|
||||
|
||||
var querySelect = @" SELECT * FROM Products";
|
||||
var products = connection.Query<Product>(querySelect);
|
||||
_logger.LogDebug("Получены объекты: {json}", JsonConvert.SerializeObject(products));
|
||||
|
||||
return products;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,12 @@
|
||||
using ProjectGasStation.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectGasStation.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.WebSockets;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@ -9,16 +14,86 @@ namespace ProjectGasStation.Repositories.Implementations;
|
||||
|
||||
internal class ReceiptRepository : IReceiptRepository
|
||||
{
|
||||
public readonly IConectionString _conectionString;
|
||||
private readonly ILogger<ReceiptRepository> _logger;
|
||||
|
||||
public ReceiptRepository(IConectionString conectionString, ILogger<ReceiptRepository> logger)
|
||||
{
|
||||
_conectionString = conectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateReceipt(Receipt receipt)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(receipt));
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_conectionString.ConectionString);
|
||||
connection.Open();
|
||||
using var transaction = connection.BeginTransaction();
|
||||
var queryInsert = @"
|
||||
INSERT INTO Receipt (DateTime, SupplierID)
|
||||
VALUES (@DateTime, @SupplierID);
|
||||
SELECT MAX(Id) FROM Receipt";
|
||||
var receiptId = connection.QueryFirst<int>(queryInsert, receipt, transaction);
|
||||
|
||||
var querySubInsert = @"
|
||||
INSERT INTO ReceiptProducts(ProductID, ReceiptID, Quantity)
|
||||
VALUES (@ProductID, @ReceiptID, @Quantity)";
|
||||
foreach (var elem in receipt.ReceiptProducts)
|
||||
{
|
||||
connection.Execute(querySubInsert, new { elem.ProductId, receiptId, elem.Quantity }, transaction);
|
||||
}
|
||||
transaction.Commit();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteReceipt(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_conectionString.ConectionString);
|
||||
connection.Open();
|
||||
|
||||
var queryDelete = @" DELETE FROM Receipt
|
||||
WHERE ID = @id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Receipt> ReadReceipt(DateTime? dateForm = null, DateTime? dateTo = null, int? productId = null, int? supplierId = null)
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_conectionString.ConectionString);
|
||||
connection.Open();
|
||||
var querySelect = @" SELECT * FROM Receipt";
|
||||
var receipt = connection.Query<Receipt>(querySelect);
|
||||
_logger.LogDebug("Получены объекты: {json}", JsonConvert.SerializeObject(receipt));
|
||||
|
||||
return receipt;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,8 @@
|
||||
using ProjectGasStation.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectGasStation.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -9,16 +13,87 @@ namespace ProjectGasStation.Repositories.Implementations;
|
||||
|
||||
internal class SaleRepository : ISaleRepository
|
||||
{
|
||||
|
||||
public readonly IConectionString _conectionString;
|
||||
private readonly ILogger<SaleRepository> _logger;
|
||||
|
||||
public SaleRepository(IConectionString conectionString, ILogger<SaleRepository> logger)
|
||||
{
|
||||
_conectionString = conectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateSale(Sale sale)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(sale));
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_conectionString.ConectionString);
|
||||
connection.Open();
|
||||
using var transaction = connection.BeginTransaction();
|
||||
var queryInsert = @"
|
||||
INSERT INTO Sale (DateTime, WorkerID)
|
||||
VALUES (@DateTime, @WorkerID);
|
||||
SELECT MAX(Id) FROM Sale";
|
||||
var saleId = connection.QueryFirst<int>(queryInsert, sale, transaction);
|
||||
|
||||
var querySubInsert = @"
|
||||
INSERT INTO SaleProducts(SaleID, ProductID, Quantity)
|
||||
VALUES (@SaleID, @ProductID, @Quantity)";
|
||||
foreach (var elem in sale.SaleProducts)
|
||||
{
|
||||
connection.Execute(querySubInsert, new { saleId, elem.ProductId, elem.Quantity }, transaction);
|
||||
}
|
||||
transaction.Commit();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteSale(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_conectionString.ConectionString);
|
||||
connection.Open();
|
||||
|
||||
var queryDelete = @" DELETE FROM Sale
|
||||
WHERE ID = @id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Sale> ReadSale(DateTime? dateForm = null, DateTime? dateTo = null, int? productId = null, int? workerId = null)
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_conectionString.ConectionString);
|
||||
connection.Open();
|
||||
var querySelect = @" SELECT * FROM Sale";
|
||||
var sale = connection.Query<Sale>(querySelect);
|
||||
_logger.LogDebug("Получены объекты: {json}", JsonConvert.SerializeObject(sale));
|
||||
|
||||
return sale;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,11 @@
|
||||
using ProjectGasStation.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectGasStation.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@ -9,26 +14,125 @@ namespace ProjectGasStation.Repositories.Implementations;
|
||||
|
||||
public class SupplierRepository : ISupplierRepository
|
||||
{
|
||||
|
||||
public readonly IConectionString _conectionString;
|
||||
private readonly ILogger<SupplierRepository> _logger;
|
||||
|
||||
public SupplierRepository(IConectionString conectionString, ILogger<SupplierRepository> logger)
|
||||
{
|
||||
_conectionString = conectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateSupplier(Supplier supplier)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(supplier));
|
||||
|
||||
}
|
||||
public void DeleteSupplier(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_conectionString.ConectionString);
|
||||
connection.Open();
|
||||
|
||||
}
|
||||
public Supplier ReadSupplierById(int id)
|
||||
{
|
||||
return Supplier.CreateEntity(0, string.Empty, string.Empty, string.Empty);
|
||||
}
|
||||
public IEnumerable<Supplier> ReadSupplier()
|
||||
{
|
||||
return [];
|
||||
var queryInsert = @" INSERT INTO Suppliers (Name, PhoneNumber, Address)
|
||||
VALUES (@Name, @PhoneNumber, @Address)";
|
||||
connection.Execute(queryInsert, supplier);
|
||||
}
|
||||
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(_conectionString.ConectionString);
|
||||
connection.Open();
|
||||
|
||||
var queryUpdate = @" UPDATE Suppliers
|
||||
SET
|
||||
Name = @SupplierName,
|
||||
PhoneNumber = @PhoneNumber,
|
||||
Address = @Address
|
||||
WHERE ID = @Id";
|
||||
connection.Execute(queryUpdate, 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(_conectionString.ConectionString);
|
||||
connection.Open();
|
||||
|
||||
var queryDelete = @" DELETE FROM Suppliers
|
||||
WHERE ID = @id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public Supplier ReadSupplierById(int id)
|
||||
{
|
||||
_logger.LogInformation("Получение объекта по идентификатору");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_conectionString.ConectionString);
|
||||
|
||||
|
||||
var querySelect = @" SELECT * FROM Suppliers 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> ReadSupplier()
|
||||
{
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_conectionString.ConectionString);
|
||||
connection.Open();
|
||||
|
||||
var querySelect = @" SELECT * FROM Suppliers";
|
||||
var suppliers = connection.Query<Supplier>(querySelect);
|
||||
_logger.LogDebug("Получены объекты: {json}", JsonConvert.SerializeObject(suppliers));
|
||||
|
||||
return suppliers;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,4 +1,8 @@
|
||||
using ProjectGasStation.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectGasStation.Entities;
|
||||
using ProjectGasStation.Entities.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@ -10,25 +14,130 @@ namespace ProjectGasStation.Repositories.Implementations;
|
||||
|
||||
public class WorkerRepository : IWorkerRepository
|
||||
{
|
||||
|
||||
public readonly IConectionString _conectionString;
|
||||
private readonly ILogger<WorkerRepository> _logger;
|
||||
|
||||
public WorkerRepository(IConectionString conectionString, ILogger<WorkerRepository> logger)
|
||||
{
|
||||
_conectionString = conectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateWorker(Worker worker)
|
||||
{
|
||||
}
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(worker));
|
||||
|
||||
public void DeleteWorker(int id)
|
||||
{
|
||||
}
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_conectionString.ConectionString);
|
||||
connection.Open();
|
||||
|
||||
public IEnumerable<Worker> ReadWorker()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
var queryInsert = @" INSERT INTO Workers (Name, JobTitle)
|
||||
VALUES (@Name, @JobTitle)";
|
||||
connection.Execute(queryInsert, worker);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
|
||||
public Worker ReadWorkerById(int id)
|
||||
{
|
||||
return Worker.CreateEntity(0, string.Empty, JobTitle.None);
|
||||
}
|
||||
|
||||
public void UpdateWorker(Worker worker)
|
||||
{
|
||||
_logger.LogInformation("Редакитрование объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(worker));
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_conectionString.ConectionString);
|
||||
connection.Open();
|
||||
|
||||
var queryUpdate = @" UPDATE Workers
|
||||
SET
|
||||
Name = @Name,
|
||||
JobTitle = @JobTitle
|
||||
WHERE ID = @Id";
|
||||
connection.Execute(queryUpdate, worker);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void DeleteWorker(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_conectionString.ConectionString);
|
||||
connection.Open();
|
||||
|
||||
var queryDelete = @" DELETE FROM Workers
|
||||
WHERE ID = @id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Worker ReadWorkerById(int id)
|
||||
{
|
||||
_logger.LogInformation("Получение объекта по идентификатору");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_conectionString.ConectionString);
|
||||
|
||||
|
||||
var querySelect = @" SELECT * FROM Workers WHERE ID = @id";
|
||||
var worker = connection.QueryFirst<Worker>(querySelect, new { id });
|
||||
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(worker));
|
||||
|
||||
return worker;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Worker> ReadWorker()
|
||||
{
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_conectionString.ConectionString);
|
||||
connection.Open();
|
||||
|
||||
var querySelect = @" SELECT * FROM Workers";
|
||||
var workers = connection.Query<Worker>(querySelect);
|
||||
_logger.LogDebug("Получены объекты: {json}", JsonConvert.SerializeObject(workers));
|
||||
|
||||
return workers;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
15
ProjectGasStation/ProjectGasStation/appsettings.json
Normal file
15
ProjectGasStation/ProjectGasStation/appsettings.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"Serilog": {
|
||||
"Using": [ "Serilog.Sinks.File" ],
|
||||
"MinimumLevel": "Debug",
|
||||
"WriteTo": [
|
||||
{
|
||||
"Name": "File",
|
||||
"Args": {
|
||||
"path": "Logs/gs_log.txt",
|
||||
"rollingInterval": "Day"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user