diff --git a/ProjectCompanyFurniture/ProjectCompanyFurniture/Entities/ProductMovement.cs b/ProjectCompanyFurniture/ProjectCompanyFurniture/Entities/ProductMovement.cs index cba9cff..add15c2 100644 --- a/ProjectCompanyFurniture/ProjectCompanyFurniture/Entities/ProductMovement.cs +++ b/ProjectCompanyFurniture/ProjectCompanyFurniture/Entities/ProductMovement.cs @@ -13,17 +13,17 @@ public class ProductMovement 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 static ProductMovement CreateOperation (int it, int productId, Movement movement) + public static ProductMovement CreateOperation (int it, int productId, Movement movementType) { return new ProductMovement { ID = it, ProductID = productId, - MovementProduct = movement, + MovementType = movementType, Date = DateTime.Now }; } diff --git a/ProjectCompanyFurniture/ProjectCompanyFurniture/Program.cs b/ProjectCompanyFurniture/ProjectCompanyFurniture/Program.cs index 95b14d8..6eab39e 100644 --- a/ProjectCompanyFurniture/ProjectCompanyFurniture/Program.cs +++ b/ProjectCompanyFurniture/ProjectCompanyFurniture/Program.cs @@ -1,6 +1,10 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; using ProjectCompanyFurniture.Repositories; using ProjectCompanyFurniture.Repositories.Implementations; +using Serilog; using Unity; +using Unity.Microsoft.Logging; namespace ProjectCompanyFurniture { @@ -21,12 +25,27 @@ namespace ProjectCompanyFurniture private static IUnityContainer CreateContainer() { var container = new UnityContainer(); + + container.AddExtension(new LoggingExtension(CreateLoggerFactory())); + container.RegisterType(); container.RegisterType(); container.RegisterType(); container.RegisterType(); container.RegisterType(); + container.RegisterType(); 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; + } } } \ No newline at end of file diff --git a/ProjectCompanyFurniture/ProjectCompanyFurniture/ProjectCompanyFurniture.csproj b/ProjectCompanyFurniture/ProjectCompanyFurniture/ProjectCompanyFurniture.csproj index 872c2a9..36b824f 100644 --- a/ProjectCompanyFurniture/ProjectCompanyFurniture/ProjectCompanyFurniture.csproj +++ b/ProjectCompanyFurniture/ProjectCompanyFurniture/ProjectCompanyFurniture.csproj @@ -9,8 +9,19 @@ + + + + + + + + + + + diff --git a/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/IConnectionString.cs b/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/IConnectionString.cs new file mode 100644 index 0000000..945e1a9 --- /dev/null +++ b/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/IConnectionString.cs @@ -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; } +} diff --git a/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/ClientRepository.cs b/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/ClientRepository.cs index 9b6f13a..03d1b66 100644 --- a/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/ClientRepository.cs +++ b/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/ClientRepository.cs @@ -1,7 +1,11 @@ -using ProjectCompanyFurniture.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using ProjectCompanyFurniture.Entities; using ProjectCompanyFurniture.Entities.Enums; using System; using System.Collections.Generic; +using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -10,30 +14,111 @@ namespace ProjectCompanyFurniture.Repositories.Implementations; public class ClientRepository : IClientRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + + public ClientRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateClient(Client client) { - } - - public void DeleteClient(int id) - { - } - - public IEnumerable ReadClient() - { - return []; - } - - public Client ReadClientById(int id) - { - return Client.CreateEntity(0, string.Empty, ClientType.None, false); - } - - public IEnumerable ReadClients() - { - return []; + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(client)); + 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) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } 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(querySelect, new + { + id + }); + _logger.LogDebug("Найденный объект: {json}", + JsonConvert.SerializeObject(client)); + return client; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } + } + + public IEnumerable ReadClients() + { + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new SqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Clients"; + var clients = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(clients)); + return clients; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } diff --git a/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/ConnectionString.cs b/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/ConnectionString.cs new file mode 100644 index 0000000..ccc3216 --- /dev/null +++ b/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/ConnectionString.cs @@ -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 => ""; +} diff --git a/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/InvoiceRepository.cs b/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/InvoiceRepository.cs index 1bdfddc..2ca394b 100644 --- a/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/InvoiceRepository.cs +++ b/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/InvoiceRepository.cs @@ -1,4 +1,5 @@ -using ProjectCompanyFurniture.Entities; +using Microsoft.Extensions.Logging; +using ProjectCompanyFurniture.Entities; using System; using System.Collections.Generic; using System.Linq; @@ -9,6 +10,15 @@ namespace ProjectCompanyFurniture.Repositories.Implementations; public class InvoiceRepository : IInvoiceRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + + public InvoiceRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateInvoice(Invoice invoice) { } diff --git a/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/ManufacturerRepository.cs b/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/ManufacturerRepository.cs index 934d720..ae5c854 100644 --- a/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/ManufacturerRepository.cs +++ b/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/ManufacturerRepository.cs @@ -1,6 +1,10 @@ -using ProjectCompanyFurniture.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using ProjectCompanyFurniture.Entities; using System; using System.Collections.Generic; +using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -9,25 +13,111 @@ namespace ProjectCompanyFurniture.Repositories.Implementations; public class ManufacturerRepository : IManufacturerRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + + public ManufacturerRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateManufacturer(Manufacturer manufacturer) { - } - - public void DeleteManufacturer(int id) - { - } - - public IEnumerable ReadManufacturers() - { - return []; - } - - public Manufacturer ReadManufacturerById(int id) - { - return Manufacturer.CreateEntity(0, string.Empty); + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(manufacturer)); + try + { + using var connection = new SqlConnection(_connectionString.ConnectionString); + var queryInsert = @"INSERT INTO Manufacturers (Name) + VALUES (@Name)"; + connection.Execute(queryInsert, manufacturer); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } 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(querySelect, new + { + id + }); + _logger.LogDebug("Найденный объект: {json}", + JsonConvert.SerializeObject(manufacturer)); + return manufacturer; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } + } + + public IEnumerable ReadManufacturers() + { + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new SqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Manufacturers"; + var manufacturers = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(manufacturers)); + return manufacturers; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } + } + + } diff --git a/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/ProductMovementRepository.cs b/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/ProductMovementRepository.cs index 676087c..db960d5 100644 --- a/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/ProductMovementRepository.cs +++ b/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/ProductMovementRepository.cs @@ -1,7 +1,11 @@ -using ProjectCompanyFurniture.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using ProjectCompanyFurniture.Entities; using ProjectCompanyFurniture.Entities.Enums; using System; using System.Collections.Generic; +using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -10,12 +14,49 @@ namespace ProjectCompanyFurniture.Repositories.Implementations; public class ProductMovementRepository : IProductMovementRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + + public ProductMovementRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + 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 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(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(productMovements)); + return productMovements; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } diff --git a/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/ProductRepository.cs b/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/ProductRepository.cs index 6ff8a8f..0a6a839 100644 --- a/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/ProductRepository.cs +++ b/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/ProductRepository.cs @@ -1,6 +1,10 @@ -using ProjectCompanyFurniture.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using ProjectCompanyFurniture.Entities; using System; using System.Collections.Generic; +using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -9,25 +13,112 @@ namespace ProjectCompanyFurniture.Repositories.Implementations; public class ProductRepository : IProductRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + + public ProductRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateProduct(Product product) { - } - - public void DeleteProduct(int id) - { - } - - public Product ReadProductById(int id) - { - return Product.CreateEntity(0, 0, string.Empty, string.Empty, 0); - } - - public IEnumerable ReadProducts() - { - return []; + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(product)); + 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) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } 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(querySelect, new + { + id + }); + _logger.LogDebug("Найденный объект: {json}", + JsonConvert.SerializeObject(product)); + return product; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } + } + + public IEnumerable ReadProducts() + { + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new SqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Products"; + var products = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(products)); + return products; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } diff --git a/ProjectCompanyFurniture/ProjectCompanyFurniture/appsettings.json b/ProjectCompanyFurniture/ProjectCompanyFurniture/appsettings.json new file mode 100644 index 0000000..f5d8617 --- /dev/null +++ b/ProjectCompanyFurniture/ProjectCompanyFurniture/appsettings.json @@ -0,0 +1,15 @@ +{ + "Serilog": { + "Using": [ "Serilog.Sinks.File" ], + "MinimumLevel": "Debug", + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "Logs/zoo_log.txt", + "rollingInterval": "Day" + } + } + ] + } +} \ No newline at end of file