ПИбд-21. Некрасов А.А. Вторая лабораторная #2
@ -65,7 +65,7 @@ namespace ProjectShoeShop
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_container.Resolve<FormSupply>().ShowDialog();
|
_container.Resolve<FormSupplys>().ShowDialog();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
@ -60,7 +60,7 @@ namespace ProjectShoeShop.Forms
|
|||||||
|
|
||||||
if (_clientId.HasValue)
|
if (_clientId.HasValue)
|
||||||
{
|
{
|
||||||
_clientRepository.CreateClient(CreateClient(_clientId.Value));
|
_clientRepository.UpdateClient(CreateClient(_clientId.Value));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -51,7 +51,7 @@ namespace ProjectShoeShop.Forms
|
|||||||
foreach (DataGridViewRow row in dataGridView.Rows)
|
foreach (DataGridViewRow row in dataGridView.Rows)
|
||||||
{
|
{
|
||||||
if (row.Cells["ColumnShoe"].Value == null ||
|
if (row.Cells["ColumnShoe"].Value == null ||
|
||||||
row.Cells["Size"].Value == null ||
|
row.Cells["ColumnSize"].Value == null ||
|
||||||
row.Cells["ColumnNumberOfPairs"].Value == null)
|
row.Cells["ColumnNumberOfPairs"].Value == null)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
@ -59,7 +59,7 @@ namespace ProjectShoeShop.Forms
|
|||||||
list.Add(OrderItem.CreateElement(0,
|
list.Add(OrderItem.CreateElement(0,
|
||||||
Convert.ToInt32(row.Cells["ColumnShoe"].Value),
|
Convert.ToInt32(row.Cells["ColumnShoe"].Value),
|
||||||
Convert.ToInt32(row.Cells["ColumnNumberOfPairs"].Value),
|
Convert.ToInt32(row.Cells["ColumnNumberOfPairs"].Value),
|
||||||
Convert.ToInt32(row.Cells["Size"].Value)));
|
Convert.ToInt32(row.Cells["ColumnSize"].Value)));
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
@ -7,32 +7,6 @@ namespace ProjectShoeShop.Forms
|
|||||||
public partial class FormSupply : Form
|
public partial class FormSupply : Form
|
||||||
{
|
{
|
||||||
private readonly ISupplyRepository _supplyRepository;
|
private readonly ISupplyRepository _supplyRepository;
|
||||||
private readonly IShoesRepository _shoesRepository;
|
|
||||||
private int? _supplyId;
|
|
||||||
|
|
||||||
public int Id
|
|
||||||
{
|
|
||||||
set
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var supply = _supplyRepository.ReadSupplyById(value);
|
|
||||||
if (supply == null)
|
|
||||||
{
|
|
||||||
throw new InvalidDataException(nameof(supply));
|
|
||||||
}
|
|
||||||
|
|
||||||
comboBox.SelectedItem = supply.SupplyType;
|
|
||||||
_supplyId = value;
|
|
||||||
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public FormSupply(ISupplyRepository supplyRepository, IShoesRepository shoesRepository)
|
public FormSupply(ISupplyRepository supplyRepository, IShoesRepository shoesRepository)
|
||||||
{
|
{
|
||||||
@ -41,8 +15,6 @@ namespace ProjectShoeShop.Forms
|
|||||||
throw new ArgumentNullException(nameof(supplyRepository));
|
throw new ArgumentNullException(nameof(supplyRepository));
|
||||||
|
|
||||||
comboBox.DataSource = Enum.GetValues(typeof(SupplyType));
|
comboBox.DataSource = Enum.GetValues(typeof(SupplyType));
|
||||||
_shoesRepository = shoesRepository ??
|
|
||||||
throw new ArgumentNullException(nameof(shoesRepository));
|
|
||||||
|
|
||||||
ColumnShoe.DataSource = shoesRepository.ReadShoes();
|
ColumnShoe.DataSource = shoesRepository.ReadShoes();
|
||||||
ColumnShoe.DisplayMember = "Name";
|
ColumnShoe.DisplayMember = "Name";
|
||||||
@ -57,15 +29,7 @@ namespace ProjectShoeShop.Forms
|
|||||||
{
|
{
|
||||||
throw new Exception("Имеется незаполненное поле");
|
throw new Exception("Имеется незаполненное поле");
|
||||||
}
|
}
|
||||||
|
_supplyRepository.CreateSupply(CreateSupply(0));
|
||||||
if (_supplyId.HasValue)
|
|
||||||
{
|
|
||||||
_supplyRepository.UpdateSupply(CreateSupply(_supplyId.Value));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_supplyRepository.CreateSupply(CreateSupply(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
Close();
|
Close();
|
||||||
}
|
}
|
||||||
@ -82,21 +46,21 @@ namespace ProjectShoeShop.Forms
|
|||||||
|
|
||||||
private Supply CreateSupply(int id) => Supply.CreateEntity(id,(SupplyType)comboBox.SelectedItem!, DateTime.Now, CreateListSupplyShoesFromDataGrid());
|
private Supply CreateSupply(int id) => Supply.CreateEntity(id,(SupplyType)comboBox.SelectedItem!, DateTime.Now, CreateListSupplyShoesFromDataGrid());
|
||||||
|
|
||||||
private List<Entities.SupplyShoes> CreateListSupplyShoesFromDataGrid()
|
private List<SupplyShoes> CreateListSupplyShoesFromDataGrid()
|
||||||
{
|
{
|
||||||
var list = new List<Entities.SupplyShoes>();
|
var list = new List<SupplyShoes>();
|
||||||
foreach (DataGridViewRow row in dataGridView.Rows)
|
foreach (DataGridViewRow row in dataGridView.Rows)
|
||||||
{
|
{
|
||||||
if (row.Cells["ColumnShoe"].Value == null ||
|
if (row.Cells["ColumnShoe"].Value == null ||
|
||||||
row.Cells["Size"].Value == null ||
|
row.Cells["ColumnSize"].Value == null ||
|
||||||
row.Cells["ColumnNumberOfPairs"].Value == null)
|
row.Cells["ColumnNumberOfPairs"].Value == null)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
list.Add(Entities.SupplyShoes.CreateElement(0,
|
list.Add(SupplyShoes.CreateElement(0,
|
||||||
Convert.ToInt32(row.Cells["ColumnShoe"].Value),
|
Convert.ToInt32(row.Cells["ColumnShoe"].Value),
|
||||||
Convert.ToInt32(row.Cells["ColumnNumberOfPairs"].Value),
|
Convert.ToInt32(row.Cells["ColumnNumberOfPairs"].Value),
|
||||||
Convert.ToInt32(row.Cells["Size"].Value)));
|
Convert.ToInt32(row.Cells["ColumnSize"].Value)));
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,6 @@
|
|||||||
panel1 = new Panel();
|
panel1 = new Panel();
|
||||||
buttonDelete = new Button();
|
buttonDelete = new Button();
|
||||||
buttonAdd = new Button();
|
buttonAdd = new Button();
|
||||||
buttonUpdate = new Button();
|
|
||||||
((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit();
|
((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit();
|
||||||
panel1.SuspendLayout();
|
panel1.SuspendLayout();
|
||||||
SuspendLayout();
|
SuspendLayout();
|
||||||
@ -57,7 +56,6 @@
|
|||||||
//
|
//
|
||||||
// panel1
|
// panel1
|
||||||
//
|
//
|
||||||
panel1.Controls.Add(buttonUpdate);
|
|
||||||
panel1.Controls.Add(buttonDelete);
|
panel1.Controls.Add(buttonDelete);
|
||||||
panel1.Controls.Add(buttonAdd);
|
panel1.Controls.Add(buttonAdd);
|
||||||
panel1.Dock = DockStyle.Right;
|
panel1.Dock = DockStyle.Right;
|
||||||
@ -88,17 +86,6 @@
|
|||||||
buttonAdd.UseVisualStyleBackColor = true;
|
buttonAdd.UseVisualStyleBackColor = true;
|
||||||
buttonAdd.Click += ButtonAdd_Click;
|
buttonAdd.Click += ButtonAdd_Click;
|
||||||
//
|
//
|
||||||
// buttonUpdate
|
|
||||||
//
|
|
||||||
buttonUpdate.BackgroundImage = Properties.Resources._0_4348_objects_pencil_png;
|
|
||||||
buttonUpdate.BackgroundImageLayout = ImageLayout.Stretch;
|
|
||||||
buttonUpdate.Location = new Point(65, 203);
|
|
||||||
buttonUpdate.Name = "buttonUpdate";
|
|
||||||
buttonUpdate.Size = new Size(75, 67);
|
|
||||||
buttonUpdate.TabIndex = 2;
|
|
||||||
buttonUpdate.UseVisualStyleBackColor = true;
|
|
||||||
buttonUpdate.Click += ButtonUpdate_Click;
|
|
||||||
//
|
|
||||||
// FormSupplys
|
// FormSupplys
|
||||||
//
|
//
|
||||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||||
@ -120,6 +107,5 @@
|
|||||||
private Panel panel1;
|
private Panel panel1;
|
||||||
private Button buttonDelete;
|
private Button buttonDelete;
|
||||||
private Button buttonAdd;
|
private Button buttonAdd;
|
||||||
private Button buttonUpdate;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -92,25 +92,5 @@ namespace ProjectShoeShop.Forms
|
|||||||
id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
|
id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ButtonUpdate_Click(object sender, EventArgs e)
|
|
||||||
{
|
|
||||||
if (!TryGetIdentifierFromSelectedRow(out var findId))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var form = _container.Resolve<FormSupply>();
|
|
||||||
form.Id = findId;
|
|
||||||
form.ShowDialog();
|
|
||||||
LoadList();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
MessageBox.Show(ex.Message, "Ошибка при изменении",
|
|
||||||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using ProjectShoeShop.Repositories;
|
using ProjectShoeShop.Repositories;
|
||||||
using ProjectShoeShop.Repositories.Implementations;
|
using ProjectShoeShop.Repositories.Implementations;
|
||||||
|
using Serilog;
|
||||||
using Unity;
|
using Unity;
|
||||||
|
using Unity.Microsoft.Logging;
|
||||||
|
|
||||||
namespace ProjectShoeShop
|
namespace ProjectShoeShop
|
||||||
{
|
{
|
||||||
@ -21,15 +25,26 @@ namespace ProjectShoeShop
|
|||||||
private static IUnityContainer CreateContainer()
|
private static IUnityContainer CreateContainer()
|
||||||
{
|
{
|
||||||
var container = new UnityContainer();
|
var container = new UnityContainer();
|
||||||
|
container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
|
||||||
|
|
||||||
container.RegisterType<IClientRepository, ClientRepository>();
|
container.RegisterType<IClientRepository, ClientRepository>();
|
||||||
container.RegisterType<IOrderRepository, OrderRepository>();
|
container.RegisterType<IOrderRepository, OrderRepository>();
|
||||||
container.RegisterType<IShoesRepository, ShoesRepository>();
|
container.RegisterType<IShoesRepository, ShoesRepository>();
|
||||||
container.RegisterType<ISupplyShoesRepository, SupplyShoes>();
|
container.RegisterType<IConnectionString, ConnectionString>();
|
||||||
container.RegisterType<ISupplyRepository, SupplyRepository>();
|
container.RegisterType<ISupplyRepository, SupplyRepository>();
|
||||||
|
|
||||||
return container;
|
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,8 +9,19 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<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="9.0.0" />
|
||||||
|
<PackageReference Include="Serilog.Settings.Configuration" Version="9.0.0" />
|
||||||
|
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
|
||||||
<PackageReference Include="Unity" Version="5.11.10" />
|
<PackageReference Include="Unity" Version="5.11.10" />
|
||||||
<PackageReference Include="Unity.Container" Version="5.11.11" />
|
<PackageReference Include="Unity.Container" Version="5.11.11" />
|
||||||
|
<PackageReference Include="Unity.Microsoft.Logging" Version="5.11.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -16,4 +16,6 @@ public interface IClientRepository
|
|||||||
void CreateClient(Client client);
|
void CreateClient(Client client);
|
||||||
|
|
||||||
void DeleteClient(int id);
|
void DeleteClient(int id);
|
||||||
|
|
||||||
|
void UpdateClient(Client client);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace ProjectShoeShop.Repositories;
|
||||||
|
|
||||||
|
public interface IConnectionString
|
||||||
|
{
|
||||||
|
public string ConnectionString { get; }
|
||||||
|
}
|
@ -10,12 +10,9 @@ namespace ProjectShoeShop.Repositories;
|
|||||||
public interface ISupplyRepository
|
public interface ISupplyRepository
|
||||||
{
|
{
|
||||||
IEnumerable<Supply> ReadSupply(DateTime? dateForm = null, DateTime? dateTo = null);
|
IEnumerable<Supply> ReadSupply(DateTime? dateForm = null, DateTime? dateTo = null);
|
||||||
Supply ReadSupplyById(int id);
|
|
||||||
|
|
||||||
void CreateSupply(Supply supply);
|
void CreateSupply(Supply supply);
|
||||||
|
|
||||||
void UpdateSupply(Supply supply);
|
|
||||||
|
|
||||||
void DeleteSupply(int id);
|
void DeleteSupply(int id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,29 +1,121 @@
|
|||||||
using System;
|
using Dapper;
|
||||||
using System.Collections.Generic;
|
using Microsoft.Extensions.Logging;
|
||||||
using System.Linq;
|
using Newtonsoft.Json;
|
||||||
using System.Text;
|
using Npgsql;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using ProjectShoeShop.Entities;
|
using ProjectShoeShop.Entities;
|
||||||
|
|
||||||
namespace ProjectShoeShop.Repositories.Implementations;
|
namespace ProjectShoeShop.Repositories.Implementations;
|
||||||
|
|
||||||
public class ClientRepository : IClientRepository
|
public class ClientRepository : IClientRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<ClientRepository> _logger;
|
||||||
|
|
||||||
|
public ClientRepository(IConnectionString connectionString, ILogger<ClientRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateClient(Client client)
|
public void CreateClient(Client client)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(client));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO Client (Name)
|
||||||
|
VALUES (@Name)";
|
||||||
|
connection.Execute(queryInsert, client);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteClient(int id)
|
public void DeleteClient(int id)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM Client
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Client> ReadClient()
|
public IEnumerable<Client> ReadClient()
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Client";
|
||||||
|
var clients = connection.Query<Client>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(clients));
|
||||||
|
return clients;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Client ReadClientById(int id)
|
public Client ReadClientById(int id)
|
||||||
{
|
{
|
||||||
return Client.CreateEntity(0, string.Empty);
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Client
|
||||||
|
WHERE Id=@id";
|
||||||
|
var client = connection.QueryFirst<Client>(querySelect, new { id });
|
||||||
|
_logger.LogDebug("Найденный объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(client));
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdateClient(Client client)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(client));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"
|
||||||
|
UPDATE Client
|
||||||
|
SET
|
||||||
|
Name=@Name
|
||||||
|
WHERE Id=@Id";
|
||||||
|
connection.Execute(queryUpdate, client);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace ProjectShoeShop.Repositories.Implementations;
|
||||||
|
|
||||||
|
public class ConnectionString : IConnectionString
|
||||||
|
{
|
||||||
|
string IConnectionString.ConnectionString => "Host=localhost;Port=5432;Username=postgres;Password=030405;Database=OTP_shoes";
|
||||||
|
}
|
@ -1,21 +1,77 @@
|
|||||||
using System;
|
using Dapper;
|
||||||
using System.Collections.Generic;
|
using Microsoft.Extensions.Logging;
|
||||||
using System.Linq;
|
using Newtonsoft.Json;
|
||||||
using System.Text;
|
using Npgsql;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using ProjectShoeShop.Entities;
|
using ProjectShoeShop.Entities;
|
||||||
|
|
||||||
namespace ProjectShoeShop.Repositories.Implementations;
|
namespace ProjectShoeShop.Repositories.Implementations;
|
||||||
|
|
||||||
public class OrderRepository : IOrderRepository
|
public class OrderRepository : IOrderRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<OrderRepository> _logger;
|
||||||
|
|
||||||
|
public OrderRepository(IConnectionString connectionString, ILogger<OrderRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateOrder(Order order)
|
public void CreateOrder(Order order)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(order));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new
|
||||||
|
NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
connection.Open();
|
||||||
|
using var transaction = connection.BeginTransaction();
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO ""Order"" (ClientId, OrderDate)
|
||||||
|
VALUES (@ClientId, @OrderDate);
|
||||||
|
SELECT MAX(Id) FROM ""Order""";
|
||||||
|
var orderId =
|
||||||
|
connection.QueryFirst<int>(queryInsert, order, transaction);
|
||||||
|
var querySubInsert = @"
|
||||||
|
INSERT INTO OrderItem (OrderId, ShoesId, NumberOfPairs, Size)
|
||||||
|
VALUES (@OrderId, @ShoesId, @NumberOfPairs, @Size)";
|
||||||
|
foreach (var elem in order.OrderItems)
|
||||||
|
{
|
||||||
|
connection.Execute(querySubInsert, new
|
||||||
|
{
|
||||||
|
orderId,
|
||||||
|
elem.ShoesId,
|
||||||
|
elem.NumberOfPairs,
|
||||||
|
elem.Size
|
||||||
|
}, transaction);
|
||||||
|
}
|
||||||
|
transaction.Commit();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Order> ReadOrder(DateTime? dateForm = null, DateTime? dateTo = null)
|
public IEnumerable<Order> ReadOrder(DateTime? dateForm = null, DateTime? dateTo = null)
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM \"Order\"";
|
||||||
|
var contractorFuels = connection.Query<Order>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(contractorFuels));
|
||||||
|
return contractorFuels;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,36 +1,123 @@
|
|||||||
using System;
|
using Dapper;
|
||||||
using System.Collections.Generic;
|
using Microsoft.Extensions.Logging;
|
||||||
using System.Linq;
|
using Newtonsoft.Json;
|
||||||
using System.Text;
|
using Npgsql;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using ProjectShoeShop.Entities;
|
using ProjectShoeShop.Entities;
|
||||||
|
|
||||||
namespace ProjectShoeShop.Repositories.Implementations;
|
namespace ProjectShoeShop.Repositories.Implementations;
|
||||||
|
|
||||||
public class ShoesRepository : IShoesRepository
|
public class ShoesRepository : IShoesRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<ShoesRepository> _logger;
|
||||||
|
|
||||||
|
public ShoesRepository(IConnectionString connectionString, ILogger<ShoesRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateShoes(Shoes shoes)
|
public void CreateShoes(Shoes shoes)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(shoes));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO Shoes (Name, Price, ShoesType)
|
||||||
|
VALUES (@Name, @Price, @ShoesType)";
|
||||||
|
connection.Execute(queryInsert, shoes);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteShoes(int id)
|
public void DeleteShoes(int id)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM Shoes
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Shoes> ReadShoes()
|
public IEnumerable<Shoes> ReadShoes()
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Shoes";
|
||||||
|
var shoeses = connection.Query<Shoes>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(shoeses));
|
||||||
|
return shoeses;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Shoes ReadShoesById(int id)
|
public Shoes ReadShoesById(int id)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Shoes
|
||||||
|
WHERE Id=@id";
|
||||||
|
var shoes = connection.QueryFirst<Shoes>(querySelect, new { id });
|
||||||
|
_logger.LogDebug("Найденный объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(shoes));
|
||||||
|
return shoes;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateShoes(Shoes shoes)
|
public void UpdateShoes(Shoes shoes)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(shoes));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"
|
||||||
|
UPDATE Shoes
|
||||||
|
SET
|
||||||
|
Name=@Name,
|
||||||
|
Price=@Price,
|
||||||
|
ShoesType=@ShoesType
|
||||||
|
WHERE Id=@Id";
|
||||||
|
connection.Execute(queryUpdate, shoes);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,32 +1,106 @@
|
|||||||
using ProjectShoeShop.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using ProjectShoeShop.Entities;
|
||||||
|
|
||||||
namespace ProjectShoeShop.Repositories.Implementations;
|
namespace ProjectShoeShop.Repositories.Implementations;
|
||||||
|
|
||||||
public class SupplyRepository : ISupplyRepository
|
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)
|
public void CreateSupply(Supply supply)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(supply));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new
|
||||||
|
NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
connection.Open();
|
||||||
|
using var transaction = connection.BeginTransaction();
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO Supply (SupplyType, DateOfReceipt)
|
||||||
|
VALUES (@SupplyType, @DateOfReceipt);
|
||||||
|
SELECT MAX(Id) FROM Supply";
|
||||||
|
var supplyId =
|
||||||
|
connection.QueryFirst<int>(queryInsert, supply, transaction);
|
||||||
|
var querySubInsert = @"
|
||||||
|
INSERT INTO SupplyShoes (SupplyId, ShoesId, NumberOfPairs, Size)
|
||||||
|
VALUES (@SupplyId, @ShoesId, @NumberOfPairs, @Size)";
|
||||||
|
foreach (var elem in supply.SupplyShoes)
|
||||||
|
{
|
||||||
|
connection.Execute(querySubInsert, new
|
||||||
|
{
|
||||||
|
supplyId,
|
||||||
|
elem.ShoesId,
|
||||||
|
elem.NumberOfPairs,
|
||||||
|
elem.Size
|
||||||
|
}, transaction);
|
||||||
|
}
|
||||||
|
transaction.Commit();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Supply> ReadSupply(DateTime? dateForm = null, DateTime? dateTo = null)
|
public IEnumerable<Supply> ReadSupply(DateTime? dateForm = null, DateTime? dateTo = null)
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Supply";
|
||||||
|
var supplies = connection.Query<Supply>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(supplies));
|
||||||
|
return supplies;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Supply ReadSupplyById(int id)
|
|
||||||
{
|
|
||||||
return Supply.CreateEntity(0, Entities.Enums.SupplyType.None, DateTime.Now, []);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void UpdateSupply(Supply supply)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void DeleteSupply(int id)
|
public void DeleteSupply(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 SupplyShoes
|
||||||
|
WHERE SupplyId = @id";
|
||||||
|
connection.Execute(queryDeleteSub, new { id }, transaction);
|
||||||
|
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM Supply
|
||||||
|
WHERE Id = @id";
|
||||||
|
connection.Execute(queryDelete, new { id }, transaction);
|
||||||
|
|
||||||
|
transaction.Commit();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
15
ProjectShoeShop/ProjectShoeShop/appsettings.json
Normal file
15
ProjectShoeShop/ProjectShoeShop/appsettings.json
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"Serilog": {
|
||||||
|
"Using": [ "Serilog.Sinks.File" ],
|
||||||
|
"MinimumLevel": "Debug",
|
||||||
|
"WriteTo": [
|
||||||
|
{
|
||||||
|
"Name": "File",
|
||||||
|
"Args": {
|
||||||
|
"path": "Logs/log.txt",
|
||||||
|
"rollingInterval": "Day"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user