diff --git a/ProjectWorkshop/ProjectWorkshop/Entities/Product.cs b/ProjectWorkshop/ProjectWorkshop/Entities/Product.cs index 38e90ad..6002222 100644 --- a/ProjectWorkshop/ProjectWorkshop/Entities/Product.cs +++ b/ProjectWorkshop/ProjectWorkshop/Entities/Product.cs @@ -10,11 +10,8 @@ public class Product public double Price { get; private set; } - public DateTime AssemblyDate { get; private set; } - public ProductType ProductType { get; private set; } - public static Product CreateEntity(int id, string productName, double price, ProductType productType) { return new Product @@ -22,8 +19,7 @@ public class Product ID = id, ProductName = productName ?? string.Empty, Price = price, - ProductType = productType, - AssemblyDate = DateTime.Now + ProductType = productType }; } } diff --git a/ProjectWorkshop/ProjectWorkshop/Forms/FormAssemblerShifts.Designer.cs b/ProjectWorkshop/ProjectWorkshop/Forms/FormAssemblerShifts.Designer.cs index 103e3e5..b80d6c4 100644 --- a/ProjectWorkshop/ProjectWorkshop/Forms/FormAssemblerShifts.Designer.cs +++ b/ProjectWorkshop/ProjectWorkshop/Forms/FormAssemblerShifts.Designer.cs @@ -39,18 +39,20 @@ // panel.Controls.Add(buttonAdd); panel.Dock = DockStyle.Right; - panel.Location = new Point(1081, 0); + panel.Location = new Point(582, 0); + panel.Margin = new Padding(2, 1, 2, 1); panel.Name = "panel"; - panel.Size = new Size(265, 783); + panel.Size = new Size(143, 367); panel.TabIndex = 0; // // buttonAdd // buttonAdd.BackgroundImage = Properties.Resources.plus; buttonAdd.BackgroundImageLayout = ImageLayout.Stretch; - buttonAdd.Location = new Point(65, 47); + buttonAdd.Location = new Point(35, 22); + buttonAdd.Margin = new Padding(2, 1, 2, 1); buttonAdd.Name = "buttonAdd"; - buttonAdd.Size = new Size(152, 145); + buttonAdd.Size = new Size(82, 68); buttonAdd.TabIndex = 0; buttonAdd.UseVisualStyleBackColor = true; buttonAdd.Click += ButtonAdd_Click; @@ -65,22 +67,24 @@ dataGridViewAssemblerShifts.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; dataGridViewAssemblerShifts.Dock = DockStyle.Fill; dataGridViewAssemblerShifts.Location = new Point(0, 0); + dataGridViewAssemblerShifts.Margin = new Padding(2, 1, 2, 1); dataGridViewAssemblerShifts.MultiSelect = false; dataGridViewAssemblerShifts.Name = "dataGridViewAssemblerShifts"; dataGridViewAssemblerShifts.ReadOnly = true; dataGridViewAssemblerShifts.RowHeadersVisible = false; dataGridViewAssemblerShifts.RowHeadersWidth = 82; dataGridViewAssemblerShifts.SelectionMode = DataGridViewSelectionMode.FullRowSelect; - dataGridViewAssemblerShifts.Size = new Size(1081, 783); + dataGridViewAssemblerShifts.Size = new Size(582, 367); dataGridViewAssemblerShifts.TabIndex = 1; // // FormAssemblerShifts // - AutoScaleDimensions = new SizeF(13F, 32F); + AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1346, 783); + ClientSize = new Size(725, 367); Controls.Add(dataGridViewAssemblerShifts); Controls.Add(panel); + Margin = new Padding(2, 1, 2, 1); Name = "FormAssemblerShifts"; Text = "Выходы на смену"; Load += FormAssemblerShifts_Load; diff --git a/ProjectWorkshop/ProjectWorkshop/Forms/FormAssemblies.Designer.cs b/ProjectWorkshop/ProjectWorkshop/Forms/FormAssemblies.Designer.cs index 2da87a3..fcff778 100644 --- a/ProjectWorkshop/ProjectWorkshop/Forms/FormAssemblies.Designer.cs +++ b/ProjectWorkshop/ProjectWorkshop/Forms/FormAssemblies.Designer.cs @@ -41,18 +41,20 @@ panel.Controls.Add(buttonDel); panel.Controls.Add(buttonAdd); panel.Dock = DockStyle.Right; - panel.Location = new Point(1062, 0); + panel.Location = new Point(572, 0); + panel.Margin = new Padding(2, 1, 2, 1); panel.Name = "panel"; - panel.Size = new Size(279, 798); + panel.Size = new Size(150, 374); panel.TabIndex = 0; // // buttonDel // buttonDel.BackgroundImage = Properties.Resources.minus; buttonDel.BackgroundImageLayout = ImageLayout.Stretch; - buttonDel.Location = new Point(67, 305); + buttonDel.Location = new Point(36, 143); + buttonDel.Margin = new Padding(2, 1, 2, 1); buttonDel.Name = "buttonDel"; - buttonDel.Size = new Size(150, 151); + buttonDel.Size = new Size(81, 71); buttonDel.TabIndex = 1; buttonDel.UseVisualStyleBackColor = true; buttonDel.Click += ButtonDel_Click; @@ -61,9 +63,10 @@ // buttonAdd.BackgroundImage = Properties.Resources.plus; buttonAdd.BackgroundImageLayout = ImageLayout.Stretch; - buttonAdd.Location = new Point(67, 40); + buttonAdd.Location = new Point(36, 19); + buttonAdd.Margin = new Padding(2, 1, 2, 1); buttonAdd.Name = "buttonAdd"; - buttonAdd.Size = new Size(150, 151); + buttonAdd.Size = new Size(81, 71); buttonAdd.TabIndex = 0; buttonAdd.UseVisualStyleBackColor = true; buttonAdd.Click += ButtonAdd_Click; @@ -78,22 +81,24 @@ dataGridViewAssemblies.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; dataGridViewAssemblies.Dock = DockStyle.Fill; dataGridViewAssemblies.Location = new Point(0, 0); + dataGridViewAssemblies.Margin = new Padding(2, 1, 2, 1); dataGridViewAssemblies.MultiSelect = false; dataGridViewAssemblies.Name = "dataGridViewAssemblies"; dataGridViewAssemblies.ReadOnly = true; dataGridViewAssemblies.RowHeadersVisible = false; dataGridViewAssemblies.RowHeadersWidth = 82; dataGridViewAssemblies.SelectionMode = DataGridViewSelectionMode.FullRowSelect; - dataGridViewAssemblies.Size = new Size(1062, 798); + dataGridViewAssemblies.Size = new Size(572, 374); dataGridViewAssemblies.TabIndex = 1; // // FormAssemblies // - AutoScaleDimensions = new SizeF(13F, 32F); + AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1341, 798); + ClientSize = new Size(722, 374); Controls.Add(dataGridViewAssemblies); Controls.Add(panel); + Margin = new Padding(2, 1, 2, 1); Name = "FormAssemblies"; Text = "Сборки"; Load += FormAssemblies_Load; diff --git a/ProjectWorkshop/ProjectWorkshop/Forms/FormShifts.Designer.cs b/ProjectWorkshop/ProjectWorkshop/Forms/FormShifts.Designer.cs index 9ca333a..ea7cec0 100644 --- a/ProjectWorkshop/ProjectWorkshop/Forms/FormShifts.Designer.cs +++ b/ProjectWorkshop/ProjectWorkshop/Forms/FormShifts.Designer.cs @@ -52,7 +52,7 @@ // buttonDel.BackgroundImage = Properties.Resources.minus; buttonDel.BackgroundImageLayout = ImageLayout.Stretch; - buttonDel.Location = new Point(93, 551); + buttonDel.Location = new Point(93, 542); buttonDel.Name = "buttonDel"; buttonDel.Size = new Size(150, 151); buttonDel.TabIndex = 2; @@ -63,7 +63,7 @@ // buttonUpd.BackgroundImage = Properties.Resources.pencil; buttonUpd.BackgroundImageLayout = ImageLayout.Stretch; - buttonUpd.Location = new Point(93, 295); + buttonUpd.Location = new Point(93, 294); buttonUpd.Name = "buttonUpd"; buttonUpd.Size = new Size(150, 151); buttonUpd.TabIndex = 1; diff --git a/ProjectWorkshop/ProjectWorkshop/Forms/FormShifts.cs b/ProjectWorkshop/ProjectWorkshop/Forms/FormShifts.cs index 4a2f278..3ba6719 100644 --- a/ProjectWorkshop/ProjectWorkshop/Forms/FormShifts.cs +++ b/ProjectWorkshop/ProjectWorkshop/Forms/FormShifts.cs @@ -1,4 +1,5 @@ using ProjectWorkshop.Repositories; +using ProjectWorkshop.Repositories.Implementations; using System; using System.Collections.Generic; using System.ComponentModel; diff --git a/ProjectWorkshop/ProjectWorkshop/Program.cs b/ProjectWorkshop/ProjectWorkshop/Program.cs index b9b1494..3afff63 100644 --- a/ProjectWorkshop/ProjectWorkshop/Program.cs +++ b/ProjectWorkshop/ProjectWorkshop/Program.cs @@ -1,6 +1,11 @@ using ProjectWorkshop.Repositories.Implementations; using ProjectWorkshop.Repositories; using Unity; +using ProjectWorkshop.Repositories.Implementation; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using Serilog; +using Unity.Microsoft.Logging; namespace ProjectWorkshop { @@ -22,6 +27,8 @@ namespace ProjectWorkshop { var container = new UnityContainer(); + container.AddExtension(new LoggingExtension(CreateLoggerFactory())); + container.RegisterType(); container.RegisterType(); container.RegisterType(); @@ -29,7 +36,21 @@ namespace ProjectWorkshop container.RegisterType(); container.RegisterType(); + container.RegisterType(); + 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/ProjectWorkshop/ProjectWorkshop/ProjectWorkshop.csproj b/ProjectWorkshop/ProjectWorkshop/ProjectWorkshop.csproj index dd71423..c42d821 100644 --- a/ProjectWorkshop/ProjectWorkshop/ProjectWorkshop.csproj +++ b/ProjectWorkshop/ProjectWorkshop/ProjectWorkshop.csproj @@ -9,7 +9,18 @@ + + + + + + + + + + + @@ -31,4 +42,10 @@ + + + PreserveNewest + + + \ No newline at end of file diff --git a/ProjectWorkshop/ProjectWorkshop/Repositories/IConnectonString.cs b/ProjectWorkshop/ProjectWorkshop/Repositories/IConnectonString.cs new file mode 100644 index 0000000..31a6b10 --- /dev/null +++ b/ProjectWorkshop/ProjectWorkshop/Repositories/IConnectonString.cs @@ -0,0 +1,7 @@ +namespace ProjectWorkshop.Repositories; + +public interface IConnectionString +{ + public string ConnectionString { get; } + +} diff --git a/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/AssemblerRepository.cs b/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/AssemblerRepository.cs index b1cd3d1..11e0373 100644 --- a/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/AssemblerRepository.cs +++ b/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/AssemblerRepository.cs @@ -1,4 +1,8 @@ -using ProjectWorkshop.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectWorkshop.Entities; using System; using System.Collections.Generic; using System.Linq; @@ -9,25 +13,123 @@ namespace ProjectWorkshop.Repositories.Implementations; public class AssemblerRepository : IAssemblerRepository { + private readonly IConnectionString _connectionString; + + private readonly ILogger _logger; + + public AssemblerRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateAssembler(Assembler assembler) { - } + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(assembler)); - public void DeleteAssembler(int id) - { - } + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + var queryInsert = @" + INSERT INTO Assembler (FullName, AssemblerRank, WorkExperience) + VALUES (@FullName, @AssemblerRank, @WorkExperience)"; + connection.Execute(queryInsert, assembler); - public Assembler ReadAssemblerByID(int id) - { - return Assembler.CreateEntity(0, string.Empty, Entities.Enums.AssemblerRank.None, DateTime.Now); - } - - public IEnumerable ReadAssemblers() - { - return []; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public void UpdateAssembler(Assembler assembler) { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(assembler)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" + UPDATE Assembler + SET + FullName=@FullName, + AssemblerRank=@AssemblerRank, + WorkExperience=@WorkExperience + WHERE ID=@id"; + + connection.Execute(queryUpdate, assembler); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } + + public void DeleteAssembler(int id) + { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" + DELETE FROM Assembler + WHERE ID=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } + } + + public Assembler ReadAssemblerByID(int id) + { + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" + SELECT * FROM Assembler + WHERE ID=@id"; + var assembler = connection.QueryFirst(querySelect, new + { + id + }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(assembler)); + return assembler; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } + } + + public IEnumerable ReadAssemblers() + { + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Assembler"; + var assemblers = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(assemblers)); + return assemblers; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } + } + } diff --git a/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/AssemblerShiftRepository.cs b/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/AssemblerShiftRepository.cs index 70d25ea..c53bab3 100644 --- a/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/AssemblerShiftRepository.cs +++ b/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/AssemblerShiftRepository.cs @@ -1,4 +1,8 @@ -using ProjectWorkshop.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectWorkshop.Entities; using System; using System.Collections.Generic; using System.Linq; @@ -9,12 +13,49 @@ namespace ProjectWorkshop.Repositories.Implementations; public class AssemblerShiftRepository : IAssemblerShiftRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + public AssemblerShiftRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } public void CreateAssemblerShift(AssemblerShift assemblerShift) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(assemblerShift)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" + INSERT INTO AssemblerShift (WorkHours, AssemblerID_Assembler, ShiftID_Shift) + VALUES (@WorkHours, @AssemblerID_Assembler, @ShiftID_Shift)"; + connection.Execute(queryInsert, assemblerShift); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public IEnumerable ReadAssemblerShifts(int? workHours = null, int? assemblerID = null, int? shiftID = null) { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM AssemblerShift"; + var assemblerShifts = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(assemblerShifts)); + return assemblerShifts; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } diff --git a/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/AssemblyRepository.cs b/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/AssemblyRepository.cs index 02bc872..abc33b7 100644 --- a/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/AssemblyRepository.cs +++ b/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/AssemblyRepository.cs @@ -1,4 +1,8 @@ -using ProjectWorkshop.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectWorkshop.Entities; using System; using System.Collections.Generic; using System.Linq; @@ -9,16 +13,111 @@ namespace ProjectWorkshop.Repositories.Implementations; public class AssemblyRepository : IAssemblyRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + + public AssemblyRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateAssembly(Assembly assembly) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(assembly)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + using var transaction = connection.BeginTransaction(); + + var queryInsert = @" + INSERT INTO Assembly (Count, AssemblerID_Assembler) + VALUES (@Count, @AssemblerID_Assembler) + RETURNING ID"; + var assemblyId = connection.QueryFirst(queryInsert, new + { + assembly.Count, + assembly.AssemblerID_Assembler + }, transaction); + + var querySubInsert = @" + INSERT INTO ProductAssembly (ProductID_Product, AssemblyID_Assembly, Count) + VALUES (@ProductID_Product, @AssemblyID_Assembly, @Count)"; + + foreach (var elem in assembly.ProductAssembly) + { + connection.Execute(querySubInsert, new + { + AssemblyID_Assembly = assemblyId, + elem.ProductID_Product, + elem.Count + }, transaction); + } + + transaction.Commit(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public void DeleteAssembly(int id) { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" + DELETE FROM Assembly + WHERE ID=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } - public IEnumerable ReadAssemblies(int? productID = null, int? assemblyID = null, int? count = null) + public IEnumerable ReadAssemblies(int? id = null, int? assemblyID = null, int? count = null) { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Assembly"; + var assemblies = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(assemblies)); + return assemblies; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } + } + + public IEnumerable GetAssemblers() + { + _logger.LogInformation("Получение всех сборщиков"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var query = "SELECT ID, Name FROM Assembler"; + var assemblers = connection.Query(query); + _logger.LogDebug("Полученные сборщики: {json}", JsonConvert.SerializeObject(assemblers)); + return assemblers; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при получении сборщиков"); + throw; + } } } diff --git a/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/ConnectionString.cs b/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/ConnectionString.cs new file mode 100644 index 0000000..7993079 --- /dev/null +++ b/ProjectWorkshop/ProjectWorkshop/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 ProjectWorkshop.Repositories.Implementation; + +internal class ConnectionString : IConnectionString +{ + string IConnectionString.ConnectionString => "Host=localhost;Port=5432;Database=otp;Username=postgres;Password=admin"; +} diff --git a/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/ProductAssemblyRepository.cs b/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/ProductAssemblyRepository.cs index 6042cf2..c7be548 100644 --- a/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/ProductAssemblyRepository.cs +++ b/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/ProductAssemblyRepository.cs @@ -1,4 +1,8 @@ -using ProjectWorkshop.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectWorkshop.Entities; using System; using System.Collections.Generic; using System.Linq; @@ -7,13 +11,55 @@ using System.Threading.Tasks; namespace ProjectWorkshop.Repositories.Implementations; -internal class ProductAssemblyRepository : IProductAssemblyRepository +public class ProductAssemblyRepository : IProductAssemblyRepository { + private readonly IConnectionString _connectionString; + + private readonly ILogger _logger; + + public ProductAssemblyRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateProductAssembly(ProductAssembly productAssembly) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(productAssembly)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" + INSERT INTO Assembly (Count, ProductType) + VALUES (@Count, @ProductType) + RETURNING ID"; + connection.Execute(queryInsert, productAssembly); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public void DeleteProductAssembly(int id) { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" + DELETE FROM Assembly + WHERE ID=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } } diff --git a/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/ProductRepository.cs b/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/ProductRepository.cs index fdeb4ee..40a0942 100644 --- a/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/ProductRepository.cs +++ b/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/ProductRepository.cs @@ -1,4 +1,8 @@ -using ProjectWorkshop.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectWorkshop.Entities; using ProjectWorkshop.Entities.Enums; using System; using System.Collections.Generic; @@ -10,25 +14,116 @@ namespace ProjectWorkshop.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) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(product)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" + INSERT INTO Product (ProductName, Price, ProductType) + VALUES (@ProductName, @Price, @ProductType) + RETURNING ID"; + 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 NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" + UPDATE Product + SET ProductName=@ProductName, Price=@Price, ProductType=@ProductType + 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 + NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" + DELETE FROM Product + 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, 0, ProductType.None); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" + SELECT * FROM Product + 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() { - return []; - } - - public void UpdateProduct(Product product) - { + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Product"; + var products = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(products)); + return products; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } diff --git a/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/ShiftRepository.cs b/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/ShiftRepository.cs index d0fe5c0..46a50de 100644 --- a/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/ShiftRepository.cs +++ b/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/ShiftRepository.cs @@ -1,4 +1,8 @@ -using ProjectWorkshop.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectWorkshop.Entities; using System; using System.Collections.Generic; using System.Linq; @@ -7,27 +11,112 @@ using System.Threading.Tasks; namespace ProjectWorkshop.Repositories.Implementations; -internal class ShiftRepository : IShiftRepository +public class ShiftRepository : IShiftRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + public ShiftRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } public void CreateShift(Shift shift) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(shift)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" + INSERT INTO Shift (ShiftDate) + VALUES (@ShiftDate)"; + connection.Execute(queryInsert, shift); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } + } + public void UpdateShift(Shift shift) + { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(shift)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" + UPDATE Shift + SET ShiftDate=@ShiftDate + WHERE Id=@Id"; + connection.Execute(queryUpdate, shift); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } public void DeleteShift(int id) { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" + DELETE FROM Shift + WHERE ID=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public Shift ReadShiftByID(int id) { - return Shift.CreateEntity(0, DateTime.Now); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" + SELECT * FROM Shift + WHERE ID=@id"; + var shift = connection.QueryFirst(querySelect, new + { + id + }); + _logger.LogDebug("Найденный объект: {json}", + JsonConvert.SerializeObject(shift)); + return shift; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } public IEnumerable ReadShifts() { - return []; - } - - public void UpdateShift(Shift shift) - { + _logger.LogInformation("Получение всех смен"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT ID, ShiftDate FROM Shift"; + return connection.Query(querySelect); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при получении смен"); + throw; + } } } diff --git a/ProjectWorkshop/ProjectWorkshop/appsettings.json b/ProjectWorkshop/ProjectWorkshop/appsettings.json new file mode 100644 index 0000000..540cd04 --- /dev/null +++ b/ProjectWorkshop/ProjectWorkshop/appsettings.json @@ -0,0 +1,15 @@ +{ + "Serilog": { + "Using": [ "Serilog.Sinks.File" ], + "MinimumLevel": "Debug", + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "Logs.txt", + "rollingInterval": "Day" + } + } + ] + } +} \ No newline at end of file