lab-2 first (error in order with many-to-many)
This commit is contained in:
parent
85c14b543e
commit
fa57175cfc
@ -63,7 +63,7 @@ namespace ProjectPublishing.Forms
|
||||
}
|
||||
else
|
||||
{
|
||||
_materialRepository.UpdateMaterial(CreateMaterial(0));
|
||||
_materialRepository.CreateMaterial(CreateMaterial(0));
|
||||
}
|
||||
Close();
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ namespace ProjectPublishing.Forms
|
||||
_materialAdmissionRepository = materialAdmissionRepository ?? throw new ArgumentException(nameof(materialAdmissionRepository));
|
||||
|
||||
comboBoxMaterialId.DataSource = materialRepository.ReadMaterials();
|
||||
comboBoxMaterialId.DisplayMember = "MaterialType";
|
||||
comboBoxMaterialId.DisplayMember = "Name";
|
||||
comboBoxMaterialId.ValueMember = "Id";
|
||||
}
|
||||
|
||||
|
@ -63,7 +63,7 @@ namespace ProjectPublishing.Forms
|
||||
}
|
||||
else
|
||||
{
|
||||
_printingHouseRepository.UpdatePrintingHouse(CreatePrintingHouse(0));
|
||||
_printingHouseRepository.CreatePrintingHouse(CreatePrintingHouse(0));
|
||||
}
|
||||
Close();
|
||||
}
|
||||
|
@ -1,6 +1,10 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using ProjectPublishing.Repositories;
|
||||
using ProjectPublishing.Repositories.Implementations;
|
||||
using Serilog;
|
||||
using Unity;
|
||||
using Unity.Microsoft.Logging;
|
||||
|
||||
namespace ProjectPublishing
|
||||
{
|
||||
@ -22,14 +26,30 @@ namespace ProjectPublishing
|
||||
{
|
||||
var container = new UnityContainer();
|
||||
|
||||
container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
|
||||
|
||||
container.RegisterType<IContactPersonRepository, ContactPersonRepository>();
|
||||
container.RegisterType<ICustomerRepository, CustomerRepository>();
|
||||
container.RegisterType<IMaterialRepository, MaterialRepository>();
|
||||
container.RegisterType<IMaterialAdmissionRepository, MaterialAdmissionRepository>();
|
||||
container.RegisterType<IOrderRepository, OrderRepository>();
|
||||
container.RegisterType<IPrintingHouseRepository, PrintingHouseRepository>();
|
||||
container.RegisterType<IConnectionString, ConnectionString>();
|
||||
|
||||
|
||||
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,20 @@
|
||||
</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.Container" Version="5.11.11" />
|
||||
<PackageReference Include="Unity.Microsoft.Logging" Version="5.11.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@ -28,4 +40,10 @@
|
||||
</EmbeddedResource>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectPublishing.Repositories
|
||||
{
|
||||
public interface IConnectionString
|
||||
{
|
||||
public string ConnectionString { get; }
|
||||
}
|
||||
}
|
@ -9,9 +9,7 @@ namespace ProjectPublishing.Repositories
|
||||
{
|
||||
public interface IMaterialAdmissionRepository
|
||||
{
|
||||
IEnumerable<MaterialsAdmission> ReadMaterialsAdmission(int? amount = null, DateTime? date = null, int? materialId = null);
|
||||
|
||||
MaterialsAdmission ReadAdmissionById(int id);
|
||||
IEnumerable<MaterialsAdmission> ReadMaterialsAdmission();
|
||||
|
||||
void CreateMaterialsAdmission(MaterialsAdmission materialAdmission);
|
||||
|
||||
|
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectPublishing.Repositories.Implementations
|
||||
{
|
||||
public class ConnectionString : IConnectionString
|
||||
{
|
||||
string IConnectionString.ConnectionString => "host=localhost;port=5432;username=postgres;password=postgres;database=publishing";
|
||||
}
|
||||
}
|
@ -1,34 +1,134 @@
|
||||
using ProjectPublishing.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectPublishing.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectPublishing.Repositories.Implementations
|
||||
{
|
||||
public class ContactPersonRepository : IContactPersonRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<ContactPersonRepository> _logger;
|
||||
public ContactPersonRepository(IConnectionString connectionString, ILogger<ContactPersonRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
public void CreateContactPerson(ContactPerson contactPerson)
|
||||
{
|
||||
}
|
||||
|
||||
public void DeleteContactPerson(int id)
|
||||
{
|
||||
}
|
||||
|
||||
public IEnumerable<ContactPerson> ReadContactPeople()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public ContactPerson ReadContactPersonById(int id)
|
||||
{
|
||||
return ContactPerson.CreateContactPerson(0, string.Empty, string.Empty, string.Empty);
|
||||
_logger.LogInformation("Создание объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(contactPerson));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var queryInsert = @"INSERT INTO ContactPersonPublisher (Name, PhoneNumber, Email)
|
||||
VALUES (@Name, @PhoneNumber, @Email)";
|
||||
connection.Execute(queryInsert, contactPerson);
|
||||
connection.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateContactPerson(ContactPerson contactPerson)
|
||||
{
|
||||
_logger.LogInformation("Редактирование объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(contactPerson));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var queryUpdate = @"
|
||||
UPDATE ContactPersonPublisher
|
||||
SET
|
||||
Name=@Name,
|
||||
PhoneNumber=@PhoneNumber,
|
||||
Email=@Email
|
||||
WHERE Id=@Id";
|
||||
connection.Execute(queryUpdate, contactPerson);
|
||||
connection.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public void DeleteContactPerson(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var queryDelete = @"
|
||||
DELETE FROM ContactPersonPublisher
|
||||
WHERE Id=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
connection.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<ContactPerson> ReadContactPeople()
|
||||
{
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM ContactPersonPublisher";
|
||||
var contactPeople = connection.Query<ContactPerson>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(contactPeople));
|
||||
return contactPeople;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public ContactPerson ReadContactPersonById(int id)
|
||||
{
|
||||
_logger.LogInformation("Получение объекта по ID");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var querySelect = @"
|
||||
SELECT * FROM ContactPersonPublisher
|
||||
WHERE Id=@id";
|
||||
var contactPerson = connection.QueryFirst<ContactPerson>(querySelect, new {id});
|
||||
connection.Close();
|
||||
_logger.LogDebug("Найден объект: {json}", JsonConvert.SerializeObject(contactPerson));
|
||||
return contactPerson;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,8 @@
|
||||
using ProjectPublishing.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectPublishing.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -9,26 +13,124 @@ namespace ProjectPublishing.Repositories.Implementations
|
||||
{
|
||||
public class CustomerRepository : ICustomerRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<CustomerRepository> _logger;
|
||||
|
||||
public CustomerRepository(IConnectionString connectionString, ILogger<CustomerRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateCustomer(Customer customer)
|
||||
{
|
||||
}
|
||||
|
||||
public void DeleteCustomer(int id)
|
||||
{
|
||||
}
|
||||
|
||||
public Customer ReadCustomerById(int id)
|
||||
{
|
||||
return Customer.CreateCustomer(0, string.Empty, string.Empty, 0);
|
||||
}
|
||||
|
||||
public IEnumerable<Customer> ReadCustomers()
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Создание объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(customer));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var queryInsert = @"
|
||||
INSERT INTO Customer (PhoneNumber, Email, PublisherId)
|
||||
VALUES (@PhoneNumber, @Email, @PublisherId)";
|
||||
connection.Execute(queryInsert, customer);
|
||||
connection.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateCustomer(Customer customer)
|
||||
{
|
||||
_logger.LogInformation("Редактирование объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(customer));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var queryUpdate = @"
|
||||
UPDATE Customer
|
||||
SET
|
||||
PhoneNumber=@PhoneNumber,
|
||||
Email=@Email,
|
||||
PublisherId=@PublisherId
|
||||
WHERE Id=@Id";
|
||||
connection.Execute(queryUpdate, customer);
|
||||
connection.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteCustomer(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var queryDelete = @"
|
||||
DELETE FROM Customer
|
||||
WHERE Id=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
connection.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Customer ReadCustomerById(int id)
|
||||
{
|
||||
_logger.LogInformation("Получение объекта по ID");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var querySelect = @"
|
||||
SELECT * FROM Customer
|
||||
WHERE Id=@id";
|
||||
var customer = connection.QueryFirst<Customer>(querySelect, new { id });
|
||||
connection.Close();
|
||||
_logger.LogDebug("Найден объект: {json}", JsonConvert.SerializeObject(customer));
|
||||
return customer;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Customer> ReadCustomers()
|
||||
{
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Customer";
|
||||
var contactPeople = connection.Query<Customer>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(contactPeople));
|
||||
return contactPeople;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,8 @@
|
||||
using ProjectPublishing.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectPublishing.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -9,19 +13,51 @@ namespace ProjectPublishing.Repositories.Implementations
|
||||
{
|
||||
public class MaterialAdmissionRepository : IMaterialAdmissionRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<MaterialAdmissionRepository> _logger;
|
||||
public MaterialAdmissionRepository(IConnectionString connectionString, ILogger<MaterialAdmissionRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
public void CreateMaterialsAdmission(MaterialsAdmission materialAdmission)
|
||||
{
|
||||
_logger.LogInformation("Создание объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(materialAdmission));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var queryInsert = @"
|
||||
INSERT INTO MaterialAdmission (Amount, MaterialId)
|
||||
VALUES (@Amount, @MaterialId)";
|
||||
connection.Execute(queryInsert, materialAdmission);
|
||||
connection.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public MaterialsAdmission ReadAdmissionById(int id)
|
||||
public IEnumerable<MaterialsAdmission> ReadMaterialsAdmission()
|
||||
{
|
||||
return MaterialsAdmission.CreateOperation(0, 0, 0);
|
||||
}
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
|
||||
public IEnumerable<MaterialsAdmission> ReadMaterialsAdmission(int? amount = null, DateTime? date = null, int? materialId = null)
|
||||
{
|
||||
return [];
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM MaterialAdmission";
|
||||
var materialAdmissions = connection.Query<MaterialsAdmission>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(materialAdmissions));
|
||||
return materialAdmissions;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,4 +1,8 @@
|
||||
using ProjectPublishing.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectPublishing.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -9,26 +13,122 @@ namespace ProjectPublishing.Repositories.Implementations
|
||||
{
|
||||
public class MaterialRepository : IMaterialRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<MaterialRepository> _logger;
|
||||
|
||||
public MaterialRepository(IConnectionString connectionString, ILogger<MaterialRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateMaterial(Material material)
|
||||
{
|
||||
_logger.LogInformation("Создание объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(material));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var queryInsert = @"INSERT INTO Material (Name, MaterialType)
|
||||
VALUES (@Name, @MaterialType)";
|
||||
connection.Execute(queryInsert, material);
|
||||
connection.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteMaterial(int id)
|
||||
{
|
||||
}
|
||||
|
||||
public Material ReadMaterialById(int id)
|
||||
{
|
||||
return Material.CreateMaterial(0, "", Entities.Enums.MaterialType.Cardboard);
|
||||
}
|
||||
|
||||
public IEnumerable<Material> ReadMaterials()
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var queryDelete = @"
|
||||
DELETE FROM Material
|
||||
WHERE Id=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
connection.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateMaterial(Material material)
|
||||
{
|
||||
_logger.LogInformation("Редактирование объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(material));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var queryUpdate = @"
|
||||
UPDATE Material
|
||||
SET
|
||||
Name=@Name,
|
||||
MaterialType=@MaterialType
|
||||
WHERE Id=@Id";
|
||||
connection.Execute(queryUpdate, material);
|
||||
connection.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Material ReadMaterialById(int id)
|
||||
{
|
||||
_logger.LogInformation("Получение объекта по ID");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var querySelect = @"
|
||||
SELECT * FROM Material
|
||||
WHERE Id=@id";
|
||||
var material = connection.QueryFirst<Material>(querySelect, new { id });
|
||||
connection.Close();
|
||||
_logger.LogDebug("Найден объект: {json}", JsonConvert.SerializeObject(material));
|
||||
return material;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Material> ReadMaterials()
|
||||
{
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Material";
|
||||
var contactPeople = connection.Query<Material>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(contactPeople));
|
||||
return contactPeople;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,8 @@
|
||||
using ProjectPublishing.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectPublishing.Entities;
|
||||
using System;
|
||||
using System.CodeDom;
|
||||
using System.Collections.Generic;
|
||||
@ -10,26 +14,85 @@ namespace ProjectPublishing.Repositories.Implementations
|
||||
{
|
||||
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)
|
||||
{
|
||||
_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 PrintingOrder (Description, ProductType, Amount, Status, CustomerId, PrintingId)
|
||||
VALUES (@Description, @ProductType, @Amount, @Status, @CustomerId, @PrintingId);
|
||||
SELECT MAX(Id) FROM PrintingOrder";
|
||||
var PrintingOrderId = connection.QueryFirst<int>(queryInsert, order, transaction);
|
||||
|
||||
var querySubInsert = @"
|
||||
INSERT INTO OrderMaterials (MaterialId, OrderId, Amount)
|
||||
VALUES (@MaterialsId, @OrderId, @Amount)";
|
||||
foreach (var elem in order.Materials)
|
||||
{
|
||||
connection.Execute(querySubInsert, new { elem.MaterialsId, PrintingOrderId, elem.Amount }, transaction);
|
||||
}
|
||||
transaction.Commit();
|
||||
connection.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteOrder(int id)
|
||||
{
|
||||
}
|
||||
|
||||
public Order ReadOrderById(int id)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var queryDelete = @"
|
||||
DELETE FROM PrintingOrder
|
||||
WHERE Id=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
connection.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Order> ReadOrders()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
|
||||
public void UpdateOrder(Order order)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM PrintingOrder";
|
||||
var printingOrders = connection.Query<Order>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(printingOrders));
|
||||
return printingOrders;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,8 @@
|
||||
using ProjectPublishing.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectPublishing.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -9,26 +13,123 @@ namespace ProjectPublishing.Repositories.Implementations
|
||||
{
|
||||
public class PrintingHouseRepository : IPrintingHouseRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<PrintingHouseRepository> _logger;
|
||||
|
||||
public PrintingHouseRepository(IConnectionString connectionString, ILogger<PrintingHouseRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
public void CreatePrintingHouse(PrintingHouse printingHouse)
|
||||
{
|
||||
}
|
||||
|
||||
public void DeletePrintingHouse(int id)
|
||||
{
|
||||
}
|
||||
|
||||
public PrintingHouse ReadPrintingHouseById(int id)
|
||||
{
|
||||
return PrintingHouse.CreatePrintingHouse(0, string.Empty, string.Empty, string.Empty, string.Empty);
|
||||
}
|
||||
|
||||
public IEnumerable<PrintingHouse> ReadPrintingHouses()
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Создание объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(printingHouse));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var queryInsert = @"
|
||||
INSERT INTO PrintingHouse (Name, Address, PhoneNumber, Email)
|
||||
VALUES (@Name, @Address, @PhoneNumber, @Email)";
|
||||
connection.Execute(queryInsert, printingHouse);
|
||||
connection.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdatePrintingHouse(PrintingHouse printingHouse)
|
||||
{
|
||||
_logger.LogInformation("Редактирование объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(printingHouse));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var queryUpdate = @"
|
||||
UPDATE PrintingHouse
|
||||
SET
|
||||
Name=@Name,
|
||||
Address=@Address,
|
||||
PhoneNumber=@PhoneNumber,
|
||||
Email=@Email
|
||||
WHERE Id=@Id";
|
||||
connection.Execute(queryUpdate, printingHouse);
|
||||
connection.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeletePrintingHouse(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var queryDelete = @"
|
||||
DELETE FROM PrintingHouse
|
||||
WHERE Id=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
connection.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public PrintingHouse ReadPrintingHouseById(int id)
|
||||
{
|
||||
_logger.LogInformation("Получение объекта по ID");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var querySelect = @"
|
||||
SELECT * FROM PrintingHouse
|
||||
WHERE Id=@id";
|
||||
var printingHouse = connection.QueryFirst<PrintingHouse>(querySelect, new { id });
|
||||
connection.Close();
|
||||
_logger.LogDebug("Найден объект: {json}", JsonConvert.SerializeObject(printingHouse));
|
||||
return printingHouse;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<PrintingHouse> ReadPrintingHouses()
|
||||
{
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM PrintingHouse";
|
||||
var printingHouse = connection.Query<PrintingHouse>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(printingHouse));
|
||||
return printingHouse;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
15
ProjectPublishing/ProjectPublishing/appsettings.json
Normal file
15
ProjectPublishing/ProjectPublishing/appsettings.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"Serilog": {
|
||||
"Using": [ "Serilog.Sinks.File" ],
|
||||
"MinimumLevel": "Debug",
|
||||
"WriteTo": [
|
||||
{
|
||||
"Name": "File",
|
||||
"Args": {
|
||||
"path": "Logs/publishing_log.txt",
|
||||
"rollingInterval": "Day"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user