diff --git a/ProjectConfectFactory/ProjectConfectFactory/Entities/Product.cs b/ProjectConfectFactory/ProjectConfectFactory/Entities/Product.cs index b1838b0..403d968 100644 --- a/ProjectConfectFactory/ProjectConfectFactory/Entities/Product.cs +++ b/ProjectConfectFactory/ProjectConfectFactory/Entities/Product.cs @@ -1,4 +1,5 @@ -using ProjectConfectFactory.Entities.Enums; +using ConfectFactory.Entities; +using ProjectConfectFactory.Entities.Enums; namespace ProjectConfectFactory.Entities; @@ -21,4 +22,17 @@ public class Product Price = price }; } + + public static Product CreateEntity(TempRecipe tempRecipe, + IEnumerable recipe) + { + return new Product + { + Id = tempRecipe.Id, + Name = tempRecipe.Name, + ProductType = tempRecipe.ProductType, + Recipe = recipe, + Price = tempRecipe.Price + }; + } } diff --git a/ProjectConfectFactory/ProjectConfectFactory/Entities/Recipe.cs b/ProjectConfectFactory/ProjectConfectFactory/Entities/Recipe.cs index b5898c0..2a5db64 100644 --- a/ProjectConfectFactory/ProjectConfectFactory/Entities/Recipe.cs +++ b/ProjectConfectFactory/ProjectConfectFactory/Entities/Recipe.cs @@ -5,9 +5,9 @@ public class Recipe { public int Id { get; private set; } public int ComponentId { get; private set; } - public double CountComponent { get; private set; } + public decimal CountComponent { get; private set; } - public static Recipe CreateElement(int id, int componentId, double countComponent) + public static Recipe CreateElement(int id, int componentId, decimal countComponent) { return new Recipe { diff --git a/ProjectConfectFactory/ProjectConfectFactory/Entities/TempRecipe.cs b/ProjectConfectFactory/ProjectConfectFactory/Entities/TempRecipe.cs new file mode 100644 index 0000000..8e44020 --- /dev/null +++ b/ProjectConfectFactory/ProjectConfectFactory/Entities/TempRecipe.cs @@ -0,0 +1,13 @@ +using ProjectConfectFactory.Entities.Enums; + +namespace ConfectFactory.Entities; + +public class TempRecipe +{ + public int Id { get; private set; } + public string Name { get; private set; } = string.Empty; + public ProductType ProductType { get; private set; } + public decimal Price { get; private set; } + public int ComponentId { get; private set; } + public decimal CountComponent { get; private set; } +} diff --git a/ProjectConfectFactory/ProjectConfectFactory/Forms/FormProduct.cs b/ProjectConfectFactory/ProjectConfectFactory/Forms/FormProduct.cs index c1a0941..752ef93 100644 --- a/ProjectConfectFactory/ProjectConfectFactory/Forms/FormProduct.cs +++ b/ProjectConfectFactory/ProjectConfectFactory/Forms/FormProduct.cs @@ -49,6 +49,13 @@ public partial class FormProduct : Form textBoxProductName.Text = product.Name; numericUpDownProductPrice.Value = (decimal)product.Price; _productId = value; + + foreach (var elem in product.Recipe) + { + var row = dataGridViewRecipe.Rows[dataGridViewRecipe.Rows.Add()]; + row.Cells["ColumnComponent"].Value = elem.ComponentId; + row.Cells["ColumnCount"].Value = elem.CountComponent; + } } catch (Exception ex) { diff --git a/ProjectConfectFactory/ProjectConfectFactory/Program.cs b/ProjectConfectFactory/ProjectConfectFactory/Program.cs index f69322a..e768395 100644 --- a/ProjectConfectFactory/ProjectConfectFactory/Program.cs +++ b/ProjectConfectFactory/ProjectConfectFactory/Program.cs @@ -2,6 +2,12 @@ using ProjectConfectFactory.Repositories.Implementations; using ProjectConfectFactory.Repositories; using Unity; using Unity.Lifetime; +using ConfectFactory.Repositories.Implementations; +using ConfectFactory.Repositories; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using Serilog; +using Unity.Microsoft.Logging; namespace ProjectConfectFactory { @@ -23,13 +29,28 @@ namespace ProjectConfectFactory { var container = new UnityContainer(); + container.AddExtension(new LoggingExtension(CreateLoggerFactory())); + container.RegisterType(new TransientLifetimeManager()); container.RegisterType(new TransientLifetimeManager()); container.RegisterType(new TransientLifetimeManager()); container.RegisterType(new TransientLifetimeManager()); container.RegisterType(new TransientLifetimeManager()); + container.RegisterType(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; + } } } \ No newline at end of file diff --git a/ProjectConfectFactory/ProjectConfectFactory/ProjectConfectFactory.csproj b/ProjectConfectFactory/ProjectConfectFactory/ProjectConfectFactory.csproj index accbdf0..e24f472 100644 --- a/ProjectConfectFactory/ProjectConfectFactory/ProjectConfectFactory.csproj +++ b/ProjectConfectFactory/ProjectConfectFactory/ProjectConfectFactory.csproj @@ -9,7 +9,19 @@ + + + + + + + + + + + + @@ -27,4 +39,10 @@ + + + PreserveNewest + + + \ No newline at end of file diff --git a/ProjectConfectFactory/ProjectConfectFactory/Repositories/IConnectionString.cs b/ProjectConfectFactory/ProjectConfectFactory/Repositories/IConnectionString.cs new file mode 100644 index 0000000..9d0e126 --- /dev/null +++ b/ProjectConfectFactory/ProjectConfectFactory/Repositories/IConnectionString.cs @@ -0,0 +1,7 @@ +namespace ConfectFactory.Repositories +{ + public interface IConnectionString + { + string ConnectionString { get; } + } +} diff --git a/ProjectConfectFactory/ProjectConfectFactory/Repositories/Implementations/ComponentRepository.cs b/ProjectConfectFactory/ProjectConfectFactory/Repositories/Implementations/ComponentRepository.cs index e7589dd..3fbd3eb 100644 --- a/ProjectConfectFactory/ProjectConfectFactory/Repositories/Implementations/ComponentRepository.cs +++ b/ProjectConfectFactory/ProjectConfectFactory/Repositories/Implementations/ComponentRepository.cs @@ -1,28 +1,132 @@ -using ProjectConfectFactory.Entities; +using ConfectFactory.Repositories; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectConfectFactory.Entities; namespace ProjectConfectFactory.Repositories.Implementations; public class ComponentRepository : IComponentRepository { + private readonly IConnectionString _connectionString; + + private readonly ILogger _logger; + public ComponentRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateComponent(Component component) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(component)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + var queryInsert = @" +INSERT INTO Components (Name, Unit, Count) +VALUES (@Name, @Unit, @Count)"; + connection.Execute(queryInsert, component); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public void DeleteComponent(int id) { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + var queryDelete = @" +DELETE FROM Components +WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public Component ReadComponentById(int id) { - return Component.CreateEntity(0, string.Empty, string.Empty, 0); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + var querySelect = @" +SELECT * FROM Components +WHERE Id=@id"; + var component = connection.QueryFirst(querySelect, new + { + id + }); + _logger.LogDebug("Найденный объект: {json}", + JsonConvert.SerializeObject(component)); + return component; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } public IEnumerable ReadComponents() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + var querySelect = "SELECT * FROM Components"; + var components = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(components)); + return components; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } public void UpdateComponent(Component component) { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(component)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + var queryUpdate = @" +UPDATE Components +SET + Name=@Name, + Unit=@Unit, + Count=@Count +WHERE Id=@Id"; + + connection.Execute(queryUpdate, component); + + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } } diff --git a/ProjectConfectFactory/ProjectConfectFactory/Repositories/Implementations/ConnectionString.cs b/ProjectConfectFactory/ProjectConfectFactory/Repositories/Implementations/ConnectionString.cs new file mode 100644 index 0000000..372c8cd --- /dev/null +++ b/ProjectConfectFactory/ProjectConfectFactory/Repositories/Implementations/ConnectionString.cs @@ -0,0 +1,7 @@ + +namespace ConfectFactory.Repositories.Implementations; + +public class ConnectionString : IConnectionString +{ + string IConnectionString.ConnectionString => "host=localhost;port=5432;database=con_factory;username=postgres;password=postgres"; +} diff --git a/ProjectConfectFactory/ProjectConfectFactory/Repositories/Implementations/MyProductRepository.cs b/ProjectConfectFactory/ProjectConfectFactory/Repositories/Implementations/MyProductRepository.cs index 6186d65..f715c7b 100644 --- a/ProjectConfectFactory/ProjectConfectFactory/Repositories/Implementations/MyProductRepository.cs +++ b/ProjectConfectFactory/ProjectConfectFactory/Repositories/Implementations/MyProductRepository.cs @@ -1,29 +1,168 @@ -using ProjectConfectFactory.Entities; -using ProjectConfectFactory.Entities.Enums; +using ConfectFactory.Entities; +using ConfectFactory.Repositories; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectConfectFactory.Entities; namespace ProjectConfectFactory.Repositories.Implementations; public class MyProductRepository : IProductRepository { + private readonly IConnectionString _connectionString; + + private readonly ILogger _logger; + public MyProductRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } public void CreateProduct(Product product) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(product)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + using var transaction = connection.BeginTransaction(); + var queryInsert = @" +INSERT INTO Products (Name, ProductType, Price) +VALUES (@Name, @ProductType, @Price); +SELECT MAX(Id) FROM Products"; + var productId = + connection.QueryFirst(queryInsert, product, transaction); + + var querySubInsert = @" +INSERT INTO Recipe (productId, ComponentId, CountComponent) +VALUES (@productId,@ComponentId, @CountComponent)"; + foreach (var elem in product.Recipe) + { + connection.Execute(querySubInsert, new + { + productId, + elem.ComponentId, + elem.CountComponent + }, transaction); + } + + transaction.Commit(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public void DeleteProduct(int id) { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" +DELETE FROM Products +WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public Product ReadProductById(int id) { - return Product.CreateEntity(0, string.Empty, ProductType.None, [] ,0); ; + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" +SELECT p.*, pc.ComponentId, pc.CountComponent FROM Products p +INNER JOIN Recipe pc ON pc.ProductId = p.Id +WHERE p.Id=@id"; + var product = connection.Query(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", + JsonConvert.SerializeObject(product)); + return product.GroupBy(x => x.Id, y => y, + (key, value) => Product.CreateEntity(value.First(), + value.Select(z => Recipe. + CreateElement(0, z.ComponentId, z.CountComponent)))).ToList().First(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } public IEnumerable ReadProducts() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" +SELECT p.*, pc.ComponentId, pc.CountComponent FROM Products p +INNER JOIN Recipe pc ON pc.ProductId = p.Id"; + var products = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(products)); + return products.GroupBy(x => x.Id, y => y, + (key, value) => Product.CreateEntity(value.First(), + value.Select(z => Recipe. + CreateElement(0, z.ComponentId, z.CountComponent)))).ToList(); + } + 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 NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + using var transaction = connection.BeginTransaction(); + var queryUpdate = @" +UPDATE Products +SET +Name=@Name, +ProductType=@ProductType, +Price=@Price +WHERE Id=@Id; +DELETE FROM Recipe +WHERE ProductId=@Id"; + connection.Execute(queryUpdate, product); + var querySubInsert = @" +INSERT INTO Recipe (ProductId, ComponentId, CountComponent) +VALUES (@Id,@ComponentId, @CountComponent)"; + foreach (var elem in product.Recipe) + { + connection.Execute(querySubInsert, new + { + product.Id, + elem.ComponentId, + elem.CountComponent + }, transaction); + } + transaction.Commit(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } } diff --git a/ProjectConfectFactory/ProjectConfectFactory/Repositories/Implementations/OrderRepository.cs b/ProjectConfectFactory/ProjectConfectFactory/Repositories/Implementations/OrderRepository.cs index 22b6503..356819c 100644 --- a/ProjectConfectFactory/ProjectConfectFactory/Repositories/Implementations/OrderRepository.cs +++ b/ProjectConfectFactory/ProjectConfectFactory/Repositories/Implementations/OrderRepository.cs @@ -1,24 +1,96 @@ -using ProjectConfectFactory.Entities; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using ConfectFactory.Repositories; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectConfectFactory.Entities; namespace ProjectConfectFactory.Repositories.Implementations; public class OrderRepository : IOrderRepository { + private readonly IConnectionString _connectionString; + + private readonly ILogger _logger; + public OrderRepository(IConnectionString connectionString, + ILogger 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 (Name,Phone,DateTime) +VALUES (@Name,@Phone, @DateTime); +SELECT MAX(Id) FROM Orders"; + var orderId = connection.QueryFirst(queryInsert, order, transaction); + var querySubInsert = @" +INSERT INTO OrderProduct (OrderId, ProductId, Count, Price) +VALUES (@OrderId,@ProductId, @Count, @Price)"; + foreach (var elem in order.OrderProducts) + { + connection.Execute(querySubInsert, new + { + orderId, + elem.ProductId, + elem.Count, + elem.Price + }, + 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 ReadOrder(DateTime? dateForm = null, DateTime? dateTo = null, int? ProductId = null) { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @"SELECT * FROM Orders"; + var orders = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(orders)); + return orders; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } diff --git a/ProjectConfectFactory/ProjectConfectFactory/Repositories/Implementations/PurchaseRepository.cs b/ProjectConfectFactory/ProjectConfectFactory/Repositories/Implementations/PurchaseRepository.cs index 56a29f2..2eca27a 100644 --- a/ProjectConfectFactory/ProjectConfectFactory/Repositories/Implementations/PurchaseRepository.cs +++ b/ProjectConfectFactory/ProjectConfectFactory/Repositories/Implementations/PurchaseRepository.cs @@ -1,4 +1,9 @@ -using ProjectConfectFactory.Entities; +using ConfectFactory.Repositories; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectConfectFactory.Entities; using System; using System.Collections.Generic; using System.Linq; @@ -9,12 +14,52 @@ namespace ProjectConfectFactory.Repositories.Implementations; public class PurchaseRepository : IPurchaseRepository { + private readonly IConnectionString _connectionString; + + private readonly ILogger _logger; + + public PurchaseRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } public void CreatePurchase(Purchase purchase) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(purchase)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" +INSERT INTO Purchases (DateTime, SellerId, ComponentId) +VALUES (@DateTime, @SellerId, @ComponentId)"; + connection.Execute(queryInsert, purchase); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public IEnumerable ReadPurchases(DateTime? dateForm = null, DateTime? dateTo = null, int? SellerId = null, int? ComponentId = null) { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Purchases"; + var purchases = + connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(purchases)); + return purchases; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } diff --git a/ProjectConfectFactory/ProjectConfectFactory/Repositories/Implementations/SellerRepository.cs b/ProjectConfectFactory/ProjectConfectFactory/Repositories/Implementations/SellerRepository.cs index 93d0c49..b7c72cc 100644 --- a/ProjectConfectFactory/ProjectConfectFactory/Repositories/Implementations/SellerRepository.cs +++ b/ProjectConfectFactory/ProjectConfectFactory/Repositories/Implementations/SellerRepository.cs @@ -1,35 +1,132 @@ using ProjectConfectFactory.Entities; -using ProjectConfectFactory.Entities.Enums; -using Microsoft.VisualBasic.FileIO; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using ConfectFactory.Repositories; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using Dapper; namespace ProjectConfectFactory.Repositories.Implementations; public class SellerRepository : ISellerRepository { + private readonly IConnectionString _connectionString; + + private readonly ILogger _logger; + public SellerRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateSeller(Seller seller) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(seller)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + var queryInsert = @" +INSERT INTO Sellers (Name, Phone, DeliveryTime) +VALUES (@Name, @Phone, @DeliveryTime)"; + connection.Execute(queryInsert, seller); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public void DeleteSeller(int id) { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + var queryDelete = @" +DELETE FROM Sellers +WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public Seller ReadSellerById(int id) { - return Seller.CreateEntity(0, string.Empty, string.Empty, DeliveryType.None); ; + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + var querySelect = @" +SELECT * FROM Sellers +WHERE Id=@id"; + var seller = connection.QueryFirst(querySelect, new + { + id + }); + _logger.LogDebug("Найденный объект: {json}", + JsonConvert.SerializeObject(seller)); + return seller; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } public IEnumerable ReadSellers() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + var querySelect = "SELECT * FROM Sellers"; + var sellers = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(sellers)); + return sellers; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } public void UpdateSeller(Seller seller) { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(seller)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + var queryUpdate = @" +UPDATE Sellers +SET + Name=@Name, + Phone=@Phone, + DeliveryTime=@DeliveryTime +WHERE Id=@Id"; + + connection.Execute(queryUpdate, seller); + + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } } diff --git a/ProjectConfectFactory/ProjectConfectFactory/appsettings.json b/ProjectConfectFactory/ProjectConfectFactory/appsettings.json new file mode 100644 index 0000000..54f1bbe --- /dev/null +++ b/ProjectConfectFactory/ProjectConfectFactory/appsettings.json @@ -0,0 +1,15 @@ +{ + "Serilog": { + "Using": [ "Serilog.Sinks.File" ], + "MinimumLevel": "Debug", + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "Logs/conFactory_log.txt", + "rollingInterval": "Day" + } + } + ] + } +} \ No newline at end of file