Пол лабораторной работы №2
This commit is contained in:
parent
33659c851e
commit
38f054099a
@ -13,17 +13,17 @@ public class ProductMovement
|
|||||||
|
|
||||||
public int ProductID { get; private set; }
|
public int ProductID { get; private set; }
|
||||||
|
|
||||||
public Movement MovementProduct { get; private set; }
|
public Movement MovementType { get; private set; }
|
||||||
|
|
||||||
public DateTime Date { get; private set; }
|
public DateTime Date { get; private set; }
|
||||||
|
|
||||||
public static ProductMovement CreateOperation (int it, int productId, Movement movement)
|
public static ProductMovement CreateOperation (int it, int productId, Movement movementType)
|
||||||
{
|
{
|
||||||
return new ProductMovement
|
return new ProductMovement
|
||||||
{
|
{
|
||||||
ID = it,
|
ID = it,
|
||||||
ProductID = productId,
|
ProductID = productId,
|
||||||
MovementProduct = movement,
|
MovementType = movementType,
|
||||||
Date = DateTime.Now
|
Date = DateTime.Now
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using ProjectCompanyFurniture.Repositories;
|
using ProjectCompanyFurniture.Repositories;
|
||||||
using ProjectCompanyFurniture.Repositories.Implementations;
|
using ProjectCompanyFurniture.Repositories.Implementations;
|
||||||
|
using Serilog;
|
||||||
using Unity;
|
using Unity;
|
||||||
|
using Unity.Microsoft.Logging;
|
||||||
|
|
||||||
namespace ProjectCompanyFurniture
|
namespace ProjectCompanyFurniture
|
||||||
{
|
{
|
||||||
@ -21,12 +25,27 @@ namespace ProjectCompanyFurniture
|
|||||||
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<IInvoiceRepository, InvoiceRepository>();
|
container.RegisterType<IInvoiceRepository, InvoiceRepository>();
|
||||||
container.RegisterType<IManufacturerRepository, ManufacturerRepository>();
|
container.RegisterType<IManufacturerRepository, ManufacturerRepository>();
|
||||||
container.RegisterType<IProductMovementRepository, ProductMovementRepository>();
|
container.RegisterType<IProductMovementRepository, ProductMovementRepository>();
|
||||||
container.RegisterType<IProductRepository, ProductRepository>();
|
container.RegisterType<IProductRepository, ProductRepository>();
|
||||||
|
container.RegisterType<IConnectionString, ConnectionString>();
|
||||||
return container;
|
return container;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static LoggerFactory CreateLoggerFactory()
|
||||||
|
{
|
||||||
|
var loggerFactory = new LoggerFactory();
|
||||||
|
loggerFactory.AddSerilog(new LoggerConfiguration()
|
||||||
|
.ReadFrom.Configuration(new ConfigurationBuilder()
|
||||||
|
.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="Serilog" Version="4.1.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" 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>
|
||||||
|
@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace ProjectCompanyFurniture.Repositories;
|
||||||
|
|
||||||
|
public interface IConnectionString
|
||||||
|
{
|
||||||
|
string ConnectionString { get; }
|
||||||
|
}
|
@ -1,7 +1,11 @@
|
|||||||
using ProjectCompanyFurniture.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using ProjectCompanyFurniture.Entities;
|
||||||
using ProjectCompanyFurniture.Entities.Enums;
|
using ProjectCompanyFurniture.Entities.Enums;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Data.SqlClient;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -10,30 +14,111 @@ namespace ProjectCompanyFurniture.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));
|
||||||
public void DeleteClient(int id)
|
try
|
||||||
{
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"INSERT INTO Clients (Name, ClientType, Optovik)
|
||||||
|
VALUES (@Name, @ClientType, @Optovik)";
|
||||||
|
connection.Execute(queryInsert, client);
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
public IEnumerable<Client> ReadClient()
|
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Client ReadClientById(int id)
|
|
||||||
{
|
|
||||||
return Client.CreateEntity(0, string.Empty, ClientType.None, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public IEnumerable<Client> ReadClients()
|
|
||||||
{
|
|
||||||
return [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateClient(Client client)
|
public void UpdateClient(Client client)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(client));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"UPDATE Clients SET
|
||||||
|
Name=@Name,
|
||||||
|
ClientType=@ClientType,
|
||||||
|
Optovik=@Optovik
|
||||||
|
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 SqlConnection(_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 SqlConnection(_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 SqlConnection(_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 ProjectCompanyFurniture.Repositories.Implementations;
|
||||||
|
|
||||||
|
public class ConnectionString : IConnectionString
|
||||||
|
{
|
||||||
|
string IConnectionString.ConnectionString => "";
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
using ProjectCompanyFurniture.Entities;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using ProjectCompanyFurniture.Entities;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -9,6 +10,15 @@ namespace ProjectCompanyFurniture.Repositories.Implementations;
|
|||||||
|
|
||||||
public class InvoiceRepository : IInvoiceRepository
|
public class InvoiceRepository : IInvoiceRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<InvoiceRepository> _logger;
|
||||||
|
|
||||||
|
public InvoiceRepository(IConnectionString connectionString, ILogger<InvoiceRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateInvoice(Invoice invoice)
|
public void CreateInvoice(Invoice invoice)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
using ProjectCompanyFurniture.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using ProjectCompanyFurniture.Entities;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Data.SqlClient;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -9,25 +13,111 @@ namespace ProjectCompanyFurniture.Repositories.Implementations;
|
|||||||
|
|
||||||
public class ManufacturerRepository : IManufacturerRepository
|
public class ManufacturerRepository : IManufacturerRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<ClientRepository> _logger;
|
||||||
|
|
||||||
|
public ManufacturerRepository(IConnectionString connectionString, ILogger<ClientRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateManufacturer(Manufacturer manufacturer)
|
public void CreateManufacturer(Manufacturer manufacturer)
|
||||||
{
|
{
|
||||||
}
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(manufacturer));
|
||||||
public void DeleteManufacturer(int id)
|
try
|
||||||
{
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"INSERT INTO Manufacturers (Name)
|
||||||
|
VALUES (@Name)";
|
||||||
|
connection.Execute(queryInsert, manufacturer);
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
public IEnumerable<Manufacturer> ReadManufacturers()
|
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Manufacturer ReadManufacturerById(int id)
|
|
||||||
{
|
|
||||||
return Manufacturer.CreateEntity(0, string.Empty);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateManufacturer(Manufacturer manufacturer)
|
public void UpdateManufacturer(Manufacturer manufacturer)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(manufacturer));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"UPDATE Manufacturers SET
|
||||||
|
Name=@Name
|
||||||
|
WHERE ID=@id";
|
||||||
|
connection.Execute(queryUpdate, manufacturer);
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteManufacturer(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"DELETE FROM Manufacturers WHERE ID=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Manufacturer ReadManufacturerById(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"SELECT * FROM Manufacturers WHERE [ID]=@id";
|
||||||
|
var manufacturer = connection.QueryFirst<Manufacturer>(querySelect, new
|
||||||
|
{
|
||||||
|
id
|
||||||
|
});
|
||||||
|
_logger.LogDebug("Найденный объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(manufacturer));
|
||||||
|
return manufacturer;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<Manufacturer> ReadManufacturers()
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Manufacturers";
|
||||||
|
var manufacturers = connection.Query<Manufacturer>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(manufacturers));
|
||||||
|
return manufacturers;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,11 @@
|
|||||||
using ProjectCompanyFurniture.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using ProjectCompanyFurniture.Entities;
|
||||||
using ProjectCompanyFurniture.Entities.Enums;
|
using ProjectCompanyFurniture.Entities.Enums;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Data.SqlClient;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -10,12 +14,49 @@ namespace ProjectCompanyFurniture.Repositories.Implementations;
|
|||||||
|
|
||||||
public class ProductMovementRepository : IProductMovementRepository
|
public class ProductMovementRepository : IProductMovementRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<ProductMovementRepository> _logger;
|
||||||
|
|
||||||
|
public ProductMovementRepository(IConnectionString connectionString, ILogger<ProductMovementRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateProductMovement(ProductMovement productMovement)
|
public void CreateProductMovement(ProductMovement productMovement)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(productMovement));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"INSERT INTO ProductMovements (ProductID, MovementType, Date)
|
||||||
|
VALUES (@ProductID, @MovementType, @Date)";
|
||||||
|
connection.Execute(queryInsert, productMovement);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<ProductMovement> ReadProductMovements(DateTime? dateForm = null, DateTime? dateTo = null, int? clientId = null)
|
public IEnumerable<ProductMovement> ReadProductMovements(DateTime? dateForm = null, DateTime? dateTo = null, int? clientId = null)
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM ProductMovements";
|
||||||
|
var productMovements = connection.Query<ProductMovement>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(productMovements));
|
||||||
|
return productMovements;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
using ProjectCompanyFurniture.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using ProjectCompanyFurniture.Entities;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Data.SqlClient;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -9,25 +13,112 @@ namespace ProjectCompanyFurniture.Repositories.Implementations;
|
|||||||
|
|
||||||
public class ProductRepository : IProductRepository
|
public class ProductRepository : IProductRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<ProductRepository> _logger;
|
||||||
|
|
||||||
|
public ProductRepository(IConnectionString connectionString, ILogger<ProductRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateProduct(Product product)
|
public void CreateProduct(Product product)
|
||||||
{
|
{
|
||||||
}
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(product));
|
||||||
public void DeleteProduct(int id)
|
try
|
||||||
{
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"INSERT INTO Products (ManufacturerID, Name, Category, StartingPrice)
|
||||||
|
VALUES (@ManufacturerID, @Name, @Category, @StartingPrice)";
|
||||||
|
connection.Execute(queryInsert, product);
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
public Product ReadProductById(int id)
|
|
||||||
{
|
{
|
||||||
return Product.CreateEntity(0, 0, string.Empty, string.Empty, 0);
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Product> ReadProducts()
|
|
||||||
{
|
|
||||||
return [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateProduct(Product product)
|
public void UpdateProduct(Product product)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(product));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"UPDATE Products SET
|
||||||
|
ManufacturerID=@ManufacturerID,
|
||||||
|
Name=@Name,
|
||||||
|
Category=@Category,
|
||||||
|
StartingPrice=@StartingPrice
|
||||||
|
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 SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"DELETE FROM Productss 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 SqlConnection(_connectionString.ConnectionString);
|
||||||
|
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> ReadProducts()
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"Serilog": {
|
||||||
|
"Using": [ "Serilog.Sinks.File" ],
|
||||||
|
"MinimumLevel": "Debug",
|
||||||
|
"WriteTo": [
|
||||||
|
{
|
||||||
|
"Name": "File",
|
||||||
|
"Args": {
|
||||||
|
"path": "Logs/zoo_log.txt",
|
||||||
|
"rollingInterval": "Day"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user