lab2
This commit is contained in:
parent
bec5af8d32
commit
f1259119a7
@ -10,7 +10,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="9.0.0" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="9.0.0" />
|
||||
<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>
|
||||
@ -32,4 +44,10 @@
|
||||
<Folder Include="Properties\DataSources\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -4,7 +4,6 @@
|
||||
public DateTime Date { get; private set; }
|
||||
public Status Status { get; private set; }
|
||||
public int ClientId { get; private set; }
|
||||
public FabricType FabricType { get; private set; }
|
||||
public IEnumerable<ModelOrder> ModelOrder { get; private set; } = [];
|
||||
|
||||
public static Order CreateOperation(int id,DateTime date, Status status, int clientId, IEnumerable<ModelOrder> modelOrder)
|
||||
|
6
Atelier/Atelier/Forms/FormModel.Designer.cs
generated
6
Atelier/Atelier/Forms/FormModel.Designer.cs
generated
@ -74,12 +74,12 @@
|
||||
//
|
||||
numericUpDownPrice.DecimalPlaces = 2;
|
||||
numericUpDownPrice.Location = new Point(120, 118);
|
||||
numericUpDownPrice.Maximum = new decimal(new int[] { 39999, 0, 0, 131072 });
|
||||
numericUpDownPrice.Minimum = new decimal(new int[] { 39999, 0, 0, 131072 });
|
||||
numericUpDownPrice.Maximum = new decimal(5000); ;
|
||||
numericUpDownPrice.Minimum = new decimal(500);
|
||||
numericUpDownPrice.Name = "numericUpDownPrice";
|
||||
numericUpDownPrice.Size = new Size(150, 27);
|
||||
numericUpDownPrice.TabIndex = 3;
|
||||
numericUpDownPrice.Value = new decimal(new int[] { 39999, 0, 0, 131072 });
|
||||
numericUpDownPrice.Value = new decimal(500);
|
||||
//
|
||||
// buttonSave
|
||||
//
|
||||
|
@ -54,7 +54,7 @@ namespace Atelier.Forms
|
||||
{
|
||||
try
|
||||
{
|
||||
if (comboBoxModel.SelectedIndex < 399.99 || dataGridView1.RowCount < 1)
|
||||
if (numericUpDownPrice.Value < 500 || dataGridView1.RowCount < 1 || comboBoxModel.SelectedIndex < 1)
|
||||
{
|
||||
throw new Exception("Имеются незаполненные поля");
|
||||
}
|
||||
|
@ -1,6 +1,12 @@
|
||||
using Atelier;
|
||||
using Atelier.Repositories;
|
||||
using Atelier.Repositories.Implementations;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Serilog;
|
||||
using Unity;
|
||||
using Unity.Lifetime;
|
||||
using Unity.Microsoft.Logging;
|
||||
|
||||
internal static class Program
|
||||
{
|
||||
@ -18,11 +24,26 @@ internal static class Program
|
||||
private static IUnityContainer CreateContainer()
|
||||
{
|
||||
var container = new UnityContainer();
|
||||
container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
|
||||
|
||||
container.RegisterType<IClientRepository,ClientRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IFabricRepository,FabricRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IModelRepository, ModelRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IOrderRepository,OrderRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IStorageRepository,StorageRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IConnectionString, ConnectionString>(new TransientLifetimeManager());
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
12
Atelier/Atelier/Repositories/IConnectionString.cs
Normal file
12
Atelier/Atelier/Repositories/IConnectionString.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Atelier.Repositories;
|
||||
|
||||
internal interface IConnectionString
|
||||
{
|
||||
string ConnectionString { get; }
|
||||
}
|
@ -1,23 +1,136 @@
|
||||
using Microsoft.VisualBasic.FileIO;
|
||||
using Atelier.Repositories;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.VisualBasic.Devices;
|
||||
using Microsoft.VisualBasic.FileIO;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using System.Data.SqlClient;
|
||||
|
||||
internal class ClientRepository : IClientRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
|
||||
private readonly ILogger<ClientRepository> _logger;
|
||||
|
||||
|
||||
public ClientRepository(IConnectionString connectionString, Logger<ClientRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
public void CreateClient(Client client)
|
||||
{
|
||||
}
|
||||
public void DeleteClient(int id)
|
||||
{
|
||||
}
|
||||
public Client ReadClientById(int id)
|
||||
{
|
||||
|
||||
return Client.CreateEntity(0, string.Empty, string.Empty, string.Empty);
|
||||
}
|
||||
public IEnumerable<Client> ReadClients()
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(client));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"
|
||||
INSERT INTO Clients (FirstName, LastName, ContactInformation)
|
||||
VALUES (@FirstName, @LastName, @ContactInformation)";
|
||||
connection.Execute(queryInsert, 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 Clients
|
||||
SET
|
||||
FirstName=@FirstName,
|
||||
LastName=@LastName,
|
||||
ContactInformation=@ContactInformation
|
||||
WHERE Id=@Id";
|
||||
connection.Execute(queryUpdate, client);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void DeleteClient(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM Clients
|
||||
WHERE Id=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Client ReadClientById(int id)
|
||||
{
|
||||
_logger.LogInformation("Получение объекта по идентификатору");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"
|
||||
SELECT * FROM Clients
|
||||
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 IEnumerable<Client> ReadClients()
|
||||
{
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Clients";
|
||||
var clients = connection.Query<Client>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(clients));
|
||||
return clients;
|
||||
}
|
||||
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 Atelier.Repositories.Implementations;
|
||||
|
||||
internal class ConnectionString : IConnectionString
|
||||
{
|
||||
string IConnectionString.ConnectionString => "Server=localhost, 5432;Database=;Uid=postgres;Pwd=postgres;";
|
||||
}
|
@ -1,20 +1,133 @@
|
||||
internal class FabricRepository : IFabricRepository
|
||||
using Atelier.Repositories;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
|
||||
internal class FabricRepository : IFabricRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
|
||||
private readonly ILogger<FabricRepository> _logger;
|
||||
|
||||
|
||||
public FabricRepository(IConnectionString connectionString, Logger<FabricRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
public void CreateFabric(Fabric fabric)
|
||||
{
|
||||
}
|
||||
public void DeleteFabric(int id)
|
||||
{
|
||||
}
|
||||
public Fabric ReadFabricById(int id)
|
||||
{
|
||||
return Fabric.CreateEntity(0, FabricType.None, Color.None, 0);
|
||||
}
|
||||
public IEnumerable<Fabric> ReadFabrics()
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(fabric));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"
|
||||
INSERT INTO Fabrics (FabricType, Color, Metrage)
|
||||
VALUES (@FabricType, @Color, @Metrage)";
|
||||
connection.Execute(queryInsert, fabric);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
public void UpdateFabric(Fabric fabric)
|
||||
{
|
||||
_logger.LogInformation("Редактирование объекта");
|
||||
|
||||
_logger.LogDebug("Объект: {json}",
|
||||
JsonConvert.SerializeObject(fabric));
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryUpdate = @"
|
||||
UPDATE Fabrics
|
||||
SET
|
||||
FabricType=@FabricType,
|
||||
Color=@Color,
|
||||
Metrage=@Metrage
|
||||
WHERE Id=@Id";
|
||||
connection.Execute(queryUpdate, fabric);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteFabric(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM Fabrics
|
||||
WHERE Id=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Fabric ReadFabricById(int id)
|
||||
{
|
||||
_logger.LogInformation("Получение объекта по идентификатору");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"
|
||||
SELECT * FROM Fabrics
|
||||
WHERE Id=@id";
|
||||
var fabric = connection.QueryFirst<Fabric>(querySelect, new
|
||||
{
|
||||
id
|
||||
});
|
||||
_logger.LogDebug("Найденный объект: {json}",
|
||||
JsonConvert.SerializeObject(fabric));
|
||||
return fabric;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Fabric> ReadFabrics()
|
||||
{
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Fabrics";
|
||||
var fabrics = connection.Query<Fabric>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(fabrics));
|
||||
return fabrics;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,20 +1,168 @@
|
||||
internal class ModelRepository : IModelRepository
|
||||
using Atelier.Repositories;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using System.Collections.Generic;
|
||||
|
||||
internal class ModelRepository : IModelRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<ModelRepository> _logger;
|
||||
|
||||
public ModelRepository(IConnectionString connectionString, ILogger<ModelRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
public void CreateModel(Model model)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(model));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
using var transaction = connection.BeginTransaction();
|
||||
|
||||
var queryInsert = @"
|
||||
INSERT INTO Models (ModelType, Price)
|
||||
VALUES (@ModelType, @Price)
|
||||
RETURNING Id";
|
||||
|
||||
var modelId = connection.QueryFirst<int>(queryInsert, model, transaction);
|
||||
|
||||
var querySubInsert = @"
|
||||
INSERT INTO FabricModel (ModelId, FabricId, Count)
|
||||
VALUES (@ModelId, @FabricId, @Count)";
|
||||
|
||||
foreach (var elem in model.FabricModel)
|
||||
{
|
||||
connection.Execute(querySubInsert, new
|
||||
{
|
||||
ModelId = modelId,
|
||||
elem.FabricId,
|
||||
elem.Count
|
||||
}, transaction);
|
||||
}
|
||||
|
||||
transaction.Commit();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public void DeleteModel(int id)
|
||||
{
|
||||
}
|
||||
public Model ReadModelById(int id)
|
||||
{
|
||||
return Model.CreateEntity(0, ModelType.None, 0, Enumerable.Empty<FabricModel>());
|
||||
}
|
||||
public IEnumerable<Model> ReadModels()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public void UpdateModel(Model model)
|
||||
{
|
||||
_logger.LogInformation("Редактирование объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(model));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
using var transaction = connection.BeginTransaction();
|
||||
|
||||
var queryUpdateModel = @"
|
||||
UPDATE Models
|
||||
SET ModelType = @ModelType, Price = @Price
|
||||
WHERE Id = @Id";
|
||||
|
||||
connection.Execute(queryUpdateModel, model, transaction);
|
||||
|
||||
var queryDeleteFabricModel = @"
|
||||
DELETE FROM FabricModel
|
||||
WHERE ModelId = @Id";
|
||||
|
||||
connection.Execute(queryDeleteFabricModel, new { model.Id }, transaction);
|
||||
|
||||
var queryInsertFabricModel = @"
|
||||
INSERT INTO FabricModel (ModelId, FabricId, Count)
|
||||
VALUES (@ModelId, @FabricId, @Count)";
|
||||
|
||||
foreach (var elem in model.FabricModel)
|
||||
{
|
||||
connection.Execute(queryInsertFabricModel, new
|
||||
{
|
||||
ModelId = model.Id,
|
||||
elem.FabricId,
|
||||
elem.Count
|
||||
}, transaction);
|
||||
}
|
||||
|
||||
transaction.Commit();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteModel(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM Models
|
||||
WHERE Id = @id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Model ReadModelById(int id)
|
||||
{
|
||||
_logger.LogInformation("Получение объекта по идентификатору");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"
|
||||
SELECT * FROM Models
|
||||
WHERE Id = @id";
|
||||
var model = connection.QueryFirstOrDefault<Model>(querySelect, new { id });
|
||||
if (model == null)
|
||||
{
|
||||
_logger.LogWarning("Объект с идентификатором {id} не найден", id);
|
||||
throw new KeyNotFoundException($"Модель с идентификатором {id} не найдена");
|
||||
}
|
||||
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(model));
|
||||
return model;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Model> ReadModels()
|
||||
{
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Models";
|
||||
var models = connection.Query<Model>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(models));
|
||||
return models;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,100 @@
|
||||
internal class OrderRepository : IOrderRepository
|
||||
using Atelier.Repositories;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using Serilog.Core;
|
||||
|
||||
|
||||
internal class OrderRepository : IOrderRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<Order> _logger;
|
||||
public OrderRepository(IConnectionString connectionString,
|
||||
ILogger<Order> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
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 Orders (Date, Status, ClientId)
|
||||
VALUES (@Date, @Status, @ClientId);
|
||||
SELECT MAX(Id) FROM Orders";
|
||||
var orderId =
|
||||
connection.QueryFirst<int>(queryInsert, order, transaction);
|
||||
var querySubInsert = @"
|
||||
INSERT INTO ModelOrder (OrderId, ModelId, Count)
|
||||
VALUES (@OrderId,@ModelId, @Count)";
|
||||
foreach (var elem in order.ModelOrder)
|
||||
{
|
||||
connection.Execute(querySubInsert, new
|
||||
{
|
||||
orderId,
|
||||
elem.ModelId,
|
||||
elem.Count
|
||||
}, transaction);
|
||||
}
|
||||
transaction.Commit();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
public void DeleteOrder(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM Orders
|
||||
WHERE Id=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
public IEnumerable<Order> ReadOrders(int? id = null, DateTime?dateForm = null, DateTime? dateTo = null,
|
||||
Status? status = null, int? clientId = null)
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"SELECT * FROM Orders";
|
||||
var orders =
|
||||
connection.Query<Order>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(orders));
|
||||
return orders;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,16 +1,75 @@
|
||||
internal class StorageRepository : IStorageRepository
|
||||
using Atelier.Repositories;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
|
||||
internal class StorageRepository : IStorageRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<Storage> _logger;
|
||||
public StorageRepository(IConnectionString connectionString,
|
||||
ILogger<Storage> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
public void CreateStorage(Storage storage)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(storage));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"
|
||||
INSERT INTO Storages (StockMetrage, FabricId)
|
||||
VALUES (@StockMetrage, @FabricId)";
|
||||
connection.Execute(queryInsert, storage);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public void UpdateStorage(Storage storage)
|
||||
{
|
||||
}
|
||||
|
||||
public void DeleteStorage(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM Storades
|
||||
WHERE Id=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public IEnumerable<Storage> ReadStorage(double? minStockMetrage = null, double? maxStockMetrage = null, int? fabricId = null)
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"SELECT * FROM Storages";
|
||||
var storages =
|
||||
connection.Query<Storage>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(storages));
|
||||
return storages;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
15
Atelier/Atelier/appsettings.json
Normal file
15
Atelier/Atelier/appsettings.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"Serilog": {
|
||||
"Using": [ "Serilog.Sinks.File" ],
|
||||
"MinimumLevel": "Debug",
|
||||
"WriteTo": [
|
||||
{
|
||||
"Name": "File",
|
||||
"Args": {
|
||||
"path": "Logs/atelier_log.txt",
|
||||
"rollingInterval": "Day"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user