From 09557899c2499e02c5f3067f841c33f3109e14a3 Mon Sep 17 00:00:00 2001 From: nezui1 Date: Mon, 25 Nov 2024 11:59:33 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B2=D1=82=D0=BE=D1=80=D0=B0=D1=8F=20=D0=BF?= =?UTF-8?q?=D0=BE=D1=87=D1=82=D0=B8=20=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProjectCompRepair/Entities/Accessories.cs | 4 +- .../ProjectCompRepair/Entities/Service.cs | 4 +- .../ProjectCompRepair/Forms/FormAccessorie.cs | 8 +- .../ProjectCompRepair/Forms/FormMaster.cs | 2 +- .../ProjectCompRepair/Forms/FormOrder.cs | 2 +- .../ProjectCompRepair/Forms/FormService.cs | 4 +- .../ProjectCompRepair/Forms/FormServices.cs | 2 +- .../ProjectCompRepair/Program.cs | 33 ++++- .../ProjectCompRepair.csproj | 13 +- .../Repositories/IAccessoriesOrder.cs | 19 +++ .../Repositories/IConnectionString.cs | 13 ++ .../AccessoriesOrderRepository.cs | 26 ++++ .../Implemantations/AccessoriesRepository.cs | 140 +++++++++++++++--- .../Implemantations/ConnectionString.cs | 14 ++ .../Implemantations/MasterRepository.cs | 112 +++++++++++++- .../Implemantations/OrderRepository.cs | 132 +++++++++++++++-- .../Implemantations/ServiceRepository.cs | 126 ++++++++++++++-- 17 files changed, 585 insertions(+), 69 deletions(-) create mode 100644 ProjectCompRepair/ProjectCompRepair/Repositories/IAccessoriesOrder.cs create mode 100644 ProjectCompRepair/ProjectCompRepair/Repositories/IConnectionString.cs create mode 100644 ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/AccessoriesOrderRepository.cs create mode 100644 ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/ConnectionString.cs diff --git a/ProjectCompRepair/ProjectCompRepair/Entities/Accessories.cs b/ProjectCompRepair/ProjectCompRepair/Entities/Accessories.cs index b02a5a2..230cfd5 100644 --- a/ProjectCompRepair/ProjectCompRepair/Entities/Accessories.cs +++ b/ProjectCompRepair/ProjectCompRepair/Entities/Accessories.cs @@ -15,9 +15,9 @@ public class Accessories public int Count { get; private set; } - public double Price { get; private set; } + public float Price { get; private set; } - public static Accessories CreateEntity(int id, AccessoriesType accessoriesType, int count, double price) + public static Accessories CreateEntity(int id, AccessoriesType accessoriesType, int count, float price) { return new Accessories { diff --git a/ProjectCompRepair/ProjectCompRepair/Entities/Service.cs b/ProjectCompRepair/ProjectCompRepair/Entities/Service.cs index 553d7e3..c622af7 100644 --- a/ProjectCompRepair/ProjectCompRepair/Entities/Service.cs +++ b/ProjectCompRepair/ProjectCompRepair/Entities/Service.cs @@ -13,9 +13,9 @@ public class Service public ServiceType ServiceType { get; private set; } - public double Price { get; private set; } + public float Price { get; private set; } - public static Service CreateEntity(int id, ServiceType serviceType, double price) + public static Service CreateEntity(int id, ServiceType serviceType, float price) { return new Service { diff --git a/ProjectCompRepair/ProjectCompRepair/Forms/FormAccessorie.cs b/ProjectCompRepair/ProjectCompRepair/Forms/FormAccessorie.cs index 73951d2..8e26b42 100644 --- a/ProjectCompRepair/ProjectCompRepair/Forms/FormAccessorie.cs +++ b/ProjectCompRepair/ProjectCompRepair/Forms/FormAccessorie.cs @@ -74,15 +74,13 @@ namespace ProjectCompRepair.Forms { MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error); } - + Close(); } private void ButtonCancel_Click(object sender, EventArgs e) => Close(); - private Accessories CreateAccessories(int id) - { - return Accessories.CreateEntity(id, (AccessoriesType)comboBoxType.SelectedItem! , (int)numericUpDownCount.Value, (double)numericUpDownPrice.Value); + private Accessories CreateAccessories(int id) => Accessories.CreateEntity(id, (AccessoriesType)comboBoxType.SelectedItem! , (int)numericUpDownCount.Value, (float)numericUpDownPrice.Value); - } + } } diff --git a/ProjectCompRepair/ProjectCompRepair/Forms/FormMaster.cs b/ProjectCompRepair/ProjectCompRepair/Forms/FormMaster.cs index e74cc85..2ac736c 100644 --- a/ProjectCompRepair/ProjectCompRepair/Forms/FormMaster.cs +++ b/ProjectCompRepair/ProjectCompRepair/Forms/FormMaster.cs @@ -69,7 +69,7 @@ public partial class FormMaster : Form { MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error); } - + Close(); } private void ButtonCancel_Click(object sender, EventArgs e) => Close(); diff --git a/ProjectCompRepair/ProjectCompRepair/Forms/FormOrder.cs b/ProjectCompRepair/ProjectCompRepair/Forms/FormOrder.cs index 2aa9ce2..ff67246 100644 --- a/ProjectCompRepair/ProjectCompRepair/Forms/FormOrder.cs +++ b/ProjectCompRepair/ProjectCompRepair/Forms/FormOrder.cs @@ -77,7 +77,7 @@ public partial class FormOrder : Form { MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error); } - + Close(); } private void ButtonCancel_Click(object sender, EventArgs e) => Close(); diff --git a/ProjectCompRepair/ProjectCompRepair/Forms/FormService.cs b/ProjectCompRepair/ProjectCompRepair/Forms/FormService.cs index 28d3bf3..71456c3 100644 --- a/ProjectCompRepair/ProjectCompRepair/Forms/FormService.cs +++ b/ProjectCompRepair/ProjectCompRepair/Forms/FormService.cs @@ -100,7 +100,7 @@ namespace ProjectCompRepair.Forms { MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error); } - + Close(); } private void ButtonCancel_Click(object sender, EventArgs e) => Close(); @@ -112,7 +112,7 @@ namespace ProjectCompRepair.Forms { serviceType |= (ServiceType)elem; } - return Service.CreateEntity(id, serviceType, (double)numericUpDownPrice.Value); + return Service.CreateEntity(id, serviceType, (float)numericUpDownPrice.Value); } } diff --git a/ProjectCompRepair/ProjectCompRepair/Forms/FormServices.cs b/ProjectCompRepair/ProjectCompRepair/Forms/FormServices.cs index 4412c17..afdd8aa 100644 --- a/ProjectCompRepair/ProjectCompRepair/Forms/FormServices.cs +++ b/ProjectCompRepair/ProjectCompRepair/Forms/FormServices.cs @@ -59,7 +59,7 @@ namespace ProjectCompRepair.Forms } try { - var form = _container.Resolve(); + var form = _container.Resolve(); form.Id = findId; form.ShowDialog(); LoadList(); diff --git a/ProjectCompRepair/ProjectCompRepair/Program.cs b/ProjectCompRepair/ProjectCompRepair/Program.cs index 3acb23a..1326170 100644 --- a/ProjectCompRepair/ProjectCompRepair/Program.cs +++ b/ProjectCompRepair/ProjectCompRepair/Program.cs @@ -1,7 +1,14 @@ +using Microsoft.Extensions.Logging; using ProjectCompRepair.Repositories; using ProjectCompRepair.Repositories.Implemantations; +using Serilog; using System; using Unity; +using Microsoft.Extensions.Configuration; +using Unity.Microsoft.Logging; +using Unity.Lifetime; + + namespace ProjectCompRepair { @@ -23,13 +30,29 @@ namespace ProjectCompRepair { var container = new UnityContainer(); - container.RegisterType(); - container.RegisterType(); - container.RegisterType(); - container.RegisterType(); - + 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()); + 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/ProjectCompRepair/ProjectCompRepair/ProjectCompRepair.csproj b/ProjectCompRepair/ProjectCompRepair/ProjectCompRepair.csproj index 2f10b71..0307ec6 100644 --- a/ProjectCompRepair/ProjectCompRepair/ProjectCompRepair.csproj +++ b/ProjectCompRepair/ProjectCompRepair/ProjectCompRepair.csproj @@ -9,8 +9,19 @@ - + + + + + + + + + + + + diff --git a/ProjectCompRepair/ProjectCompRepair/Repositories/IAccessoriesOrder.cs b/ProjectCompRepair/ProjectCompRepair/Repositories/IAccessoriesOrder.cs new file mode 100644 index 0000000..c7b61e5 --- /dev/null +++ b/ProjectCompRepair/ProjectCompRepair/Repositories/IAccessoriesOrder.cs @@ -0,0 +1,19 @@ +using ProjectCompRepair.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectCompRepair.Repositories; + +public interface IAccessoriesOrder +{ + void CreateAccessoriesOrder(AccessoiresOrder accessoriesOrder); + + void DeleteAccessoriesOrder(int id); + + IEnumerable ReadAccessoiresOrder(int? Id = 0, int? orderId = 0, int? count = 0); + + +} diff --git a/ProjectCompRepair/ProjectCompRepair/Repositories/IConnectionString.cs b/ProjectCompRepair/ProjectCompRepair/Repositories/IConnectionString.cs new file mode 100644 index 0000000..c90c50b --- /dev/null +++ b/ProjectCompRepair/ProjectCompRepair/Repositories/IConnectionString.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectCompRepair.Repositories; + +public interface IConnectionString +{ + public string ConnectionString {get;} + +} diff --git a/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/AccessoriesOrderRepository.cs b/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/AccessoriesOrderRepository.cs new file mode 100644 index 0000000..fc0a1c1 --- /dev/null +++ b/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/AccessoriesOrderRepository.cs @@ -0,0 +1,26 @@ +using ProjectCompRepair.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectCompRepair.Repositories.Implemantations; + +public class AccessoriesOrderRepository : IAccessoriesOrder +{ + public void CreateAccessoriesOrder(AccessoiresOrder accessoriesOrder) + { + throw new NotImplementedException(); + } + + public void DeleteAccessoriesOrder(int id) + { + throw new NotImplementedException(); + } + + public IEnumerable ReadAccessoiresOrder(int? Id = 0, int? orderId = 0, int? count = 0) + { + throw new NotImplementedException(); + } +} diff --git a/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/AccessoriesRepository.cs b/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/AccessoriesRepository.cs index 0df9c6b..6fbab98 100644 --- a/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/AccessoriesRepository.cs +++ b/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/AccessoriesRepository.cs @@ -4,33 +4,139 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using System.Data.SqlClient; +using Dapper; +using Npgsql; +using System.Xml.Linq; namespace ProjectCompRepair.Repositories.Implemantations; public class AccessoriesRepository : IAccessoriesRepository { + private readonly IConnectionString _connectionString; + + private readonly ILogger _logger; + + public AccessoriesRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateAccessories(Accessories accessories) { - - } + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект : {json}", JsonConvert.SerializeObject(accessories)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); - public void DeleteAccessories(int id) - { - + var queryInsert = @" +INSERT INTO Accessories (AccessoriesType, Count, Price) +VALUES (@AccessoriesType, @Count, @Price)"; + connection.Execute(queryInsert, accessories); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } - - public IEnumerable ReadAccessories() - { - return []; - } - - public Accessories ReadAccessoriesById(int id) - { - return Accessories.CreateEntity(0, 0, 0, 0); - } - public void UpdateAccessories(Accessories accessories) { - + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект : {json}", JsonConvert.SerializeObject(accessories)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + + var queryUpdate = @"UPDATE Accessories +SET + AccessoriesType = @AccessoriesType, + Count = @Count, + Price = @Price +WHERE ID = @Id;"; + connection.Execute(queryUpdate, accessories); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } + + + public void DeleteAccessories(int id) + { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект : {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + + var queryDelete = @" +DELETE FROM Accessories +WHERE ID = @id +" +; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } + } + public Accessories ReadAccessoriesById(int id) + { + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект : {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + + var querySelect = @" +SELECT * FROM Accessories +WHERE ID = @id +"; + var accessories = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект {json}", JsonConvert.SerializeObject(accessories)); + return accessories; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } + } + public IEnumerable ReadAccessories() + { + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + + var querySelect = @"SELECT * FROM Accessories"; + var accessories = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты : {json}", JsonConvert.SerializeObject(accessories)); + return accessories; + } + catch(Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } + } + + + + + } diff --git a/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/ConnectionString.cs b/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/ConnectionString.cs new file mode 100644 index 0000000..4be1c79 --- /dev/null +++ b/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/ConnectionString.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectCompRepair.Repositories.Implemantations; +public class ConnectionString : IConnectionString +{ + string IConnectionString.ConnectionString => "host=localhost;port=5432;username=postgres;password=123;Database=otp;"; +} + + + diff --git a/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/MasterRepository.cs b/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/MasterRepository.cs index 3bf559a..c8fb445 100644 --- a/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/MasterRepository.cs +++ b/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/MasterRepository.cs @@ -1,6 +1,11 @@ -using ProjectCompRepair.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectCompRepair.Entities; using System; using System.Collections.Generic; +using System.Diagnostics.Metrics; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -9,28 +14,123 @@ namespace ProjectCompRepair.Repositories.Implemantations; public class MasterRepository : IMasterRepository { + private readonly IConnectionString _connectionString; + + private readonly ILogger _logger; + + public MasterRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } public void CreateMaster(Master master) { - + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект : {json}", JsonConvert.SerializeObject(master)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + + var queryInsert = @" +INSERT INTO Master (Name) +VALUES (@Name)"; + connection.Execute(queryInsert, master); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } + public void DeleteMaster(int id) { - + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект : {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + + var queryDelete = @" +DELETE FROM Master +WHERE ID = @id +" +; + connection.Execute(queryDelete, new { id }); } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } +} public IEnumerable ReadMaster() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + + var querySelect = @"SELECT * FROM Master"; + var master = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты : {json}", JsonConvert.SerializeObject(master)); + return master; } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } +} public Master ReadMasterById(int id) { - return Master.CreateEntity(0, string.Empty); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект : {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + + var querySelect = @" +SELECT * FROM Master +WHERE ID = @id +"; + var master = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект {json}", JsonConvert.SerializeObject(master)); + return master; } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } +} public void UpdateMaster(Master master) { - + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект : {json}", JsonConvert.SerializeObject(master)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + + var queryUpdate = @"UPDATE Master +SET + Name = @Name +WHERE ID = @Id;"; + connection.Execute(queryUpdate, master); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; } } +} diff --git a/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/OrderRepository.cs b/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/OrderRepository.cs index 41dad7d..63933cb 100644 --- a/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/OrderRepository.cs +++ b/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/OrderRepository.cs @@ -1,4 +1,8 @@ -using ProjectCompRepair.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectCompRepair.Entities; using System; using System.Collections.Generic; using System.Linq; @@ -9,29 +13,131 @@ namespace ProjectCompRepair.Repositories.Implemantations; 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(); + var queryInsert = @" +INSERT INTO Order (Name, Coment, Date, MasterId, AccessoiresOrders, ServicesOrders) +VALUES (@Name, @Coment, @Date, @MasterId, @AccessoiresOrders, @ServicesOrders)"; + connection.Execute(queryInsert, order); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } + } + public void UpdateOrder(Order order) + { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект : {json}", JsonConvert.SerializeObject(order)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + + var queryUpdate = @"UPDATE Order +SET + Name = @Name, + Coment = @Coment, + Price = @Price, + Date = @Date, + MasterId = @MasterId, + AccessoiresOrders = @AccessoiresOrders, + ServicesOrders = @ServicesOrders +WHERE ID = @Id;"; + connection.Execute(queryUpdate, order); + } + 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); + connection.Open(); + + var queryDelete = @" +DELETE FROM Order +WHERE ID = @id +" +; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } + } + public Order ReadOrderById(int id) + { + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект : {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + + var querySelect = @" +SELECT * FROM Order +WHERE ID = @id +"; + var order = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект {json}", JsonConvert.SerializeObject(order)); + return order; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } public IEnumerable ReadOrder() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + + var querySelect = @"SELECT * FROM Order"; + var order = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты : {json}", JsonConvert.SerializeObject(order)); + return order; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } - public Order ReadOrderById(int id) - { - return Order.CreateElement(0, string.Empty, string.Empty, 0); - } + - public void UpdateOrder(Order order) - { - throw new NotImplementedException(); - } + } diff --git a/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/ServiceRepository.cs b/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/ServiceRepository.cs index 5b0e277..94adadd 100644 --- a/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/ServiceRepository.cs +++ b/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/ServiceRepository.cs @@ -1,4 +1,8 @@ -using ProjectCompRepair.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectCompRepair.Entities; using System; using System.Collections.Generic; using System.Linq; @@ -9,28 +13,124 @@ namespace ProjectCompRepair.Repositories.Implemantations; public class ServiceRepository : IServiceRepository { + private readonly IConnectionString _connectionString; + + private readonly ILogger _logger; + + public ServiceRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } public void CreateService(Service service) { - - } + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект : {json}", JsonConvert.SerializeObject(service)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + var queryInsert = @" +INSERT INTO Service (ServiceType, Price) +VALUES (@ServiceType, @Price)"; + connection.Execute(queryInsert, service); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } + } + public void UpdateService(Service service) + { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект : {json}", JsonConvert.SerializeObject(service)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + + var queryUpdate = @"UPDATE Service +SET + ServiceType = @ServiceType, + Price = @Price +WHERE ID = @Id;"; + connection.Execute(queryUpdate, service); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } + } public void DeleteService(int id) { - - } + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект : {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); - public IEnumerable ReadService() - { - return []; + var queryDelete = @" +DELETE FROM Service +WHERE ID = @id +" +; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public Service ReadServiceById(int id) { - return Service.CreateEntity(0, Entities.Enums.ServiceType.None, 0); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект : {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + + var querySelect = @" +SELECT * FROM Service +WHERE ID = @id +"; + var service = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект {json}", JsonConvert.SerializeObject(service)); + return service; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } + } + public IEnumerable ReadService() + { + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + + var querySelect = @"SELECT * FROM Service"; + var service = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты : {json}", JsonConvert.SerializeObject(service)); + return service; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } - public void UpdateService(Service service) - { - - } + + + }