diff --git a/ProjectCompRepair/ProjectCompRepair/Entities/Order.cs b/ProjectCompRepair/ProjectCompRepair/Entities/Order.cs index 89561d1..24de9bb 100644 --- a/ProjectCompRepair/ProjectCompRepair/Entities/Order.cs +++ b/ProjectCompRepair/ProjectCompRepair/Entities/Order.cs @@ -23,15 +23,17 @@ public class Order public IEnumerable ServicesOrders { get; private set; } = []; - public static Order CreateElement(int id, string name, string coment, int masterId) + public static Order CreateElement(int id, string name,DateTime date, string coment, int masterId, IEnumerable accessoiresOrders, IEnumerable servicesOrder) { return new Order() { Id = id, Name = name, Coment = coment, - Date = DateTime.Now, - MasterID = masterId + Date = date, + MasterID = masterId, + AccessoiresOrders = accessoiresOrders, + ServicesOrders = servicesOrder }; } diff --git a/ProjectCompRepair/ProjectCompRepair/Forms/FormOrder.cs b/ProjectCompRepair/ProjectCompRepair/Forms/FormOrder.cs index ff67246..f779905 100644 --- a/ProjectCompRepair/ProjectCompRepair/Forms/FormOrder.cs +++ b/ProjectCompRepair/ProjectCompRepair/Forms/FormOrder.cs @@ -70,7 +70,7 @@ public partial class FormOrder : Form { throw new Exception("Имеются незаполненые поля"); } - _orderRepository.CreateOrder(Order.CreateElement(0, textBoxName.Text, textBoxComent.Text, (int)comboBoxMaster.SelectedValue!)); + _orderRepository.CreateOrder(Order.CreateElement(0, textBoxName.Text,dateTimePicker.Value, textBoxComent.Text, (int)comboBoxMaster.SelectedValue!, CreateListAccessoiresOrderFromDataGrid(), CreateListServicesOrderFromDataGrid())); Close(); } catch (Exception ex) @@ -87,13 +87,13 @@ public partial class FormOrder : Form var list = new List(); foreach (DataGridViewRow row in dataGridViewAccessories.Rows) { - if (row.Cells["ColumnAccessoires"].Value == null || + if (row.Cells["ColumnAccessories"].Value == null || row.Cells["ColumnCount"].Value == null) { continue; } list.Add(AccessoiresOrder.CreateElement(0, - Convert.ToInt32(row.Cells["ColumnAccessoires"].Value), + Convert.ToInt32(row.Cells["ColumnAccessories"].Value), Convert.ToInt32(row.Cells["ColumnCount"].Value))); } return list; @@ -102,16 +102,16 @@ public partial class FormOrder : Form private List CreateListServicesOrderFromDataGrid() { var list = new List(); - foreach (DataGridViewRow row in dataGridViewAccessories.Rows) + foreach (DataGridViewRow row in dataGridViewServices.Rows) { - if (row.Cells["ColumnServices"].Value == null || - row.Cells["ColumnCountServices"].Value == null) + if (row.Cells["ColumnService"].Value == null || + row.Cells["ColumnCountServ"].Value == null) { continue; } list.Add(ServicesOrder.CreateElement(0, - Convert.ToInt32(row.Cells["ColumnAccessoires"].Value), - Convert.ToInt32(row.Cells["ColumnCount"].Value))); + Convert.ToInt32(row.Cells["ColumnService"].Value), + Convert.ToInt32(row.Cells["ColumnCountServ"].Value))); } return list; } diff --git a/ProjectCompRepair/ProjectCompRepair/Forms/FormOrder.resx b/ProjectCompRepair/ProjectCompRepair/Forms/FormOrder.resx index 3fa5b7f..ca35006 100644 --- a/ProjectCompRepair/ProjectCompRepair/Forms/FormOrder.resx +++ b/ProjectCompRepair/ProjectCompRepair/Forms/FormOrder.resx @@ -123,18 +123,6 @@ True - - True - - - True - - - True - - - True - True diff --git a/ProjectCompRepair/ProjectCompRepair/Forms/FormOrders.Designer.cs b/ProjectCompRepair/ProjectCompRepair/Forms/FormOrders.Designer.cs index fb0ed62..9c3e00a 100644 --- a/ProjectCompRepair/ProjectCompRepair/Forms/FormOrders.Designer.cs +++ b/ProjectCompRepair/ProjectCompRepair/Forms/FormOrders.Designer.cs @@ -29,7 +29,6 @@ private void InitializeComponent() { buttonDel = new Button(); - buttonChange = new Button(); buttonCreate = new Button(); dataGridView = new DataGridView(); panelButtons = new Panel(); @@ -41,24 +40,13 @@ // buttonDel.BackgroundImage = Properties.Resources.pngwing_com__7_; buttonDel.BackgroundImageLayout = ImageLayout.Stretch; - buttonDel.Location = new Point(40, 255); + buttonDel.Location = new Point(40, 173); buttonDel.Name = "buttonDel"; buttonDel.Size = new Size(94, 91); buttonDel.TabIndex = 2; buttonDel.UseVisualStyleBackColor = true; buttonDel.Click += ButtonDel_Click; // - // buttonChange - // - buttonChange.BackgroundImage = Properties.Resources.pngwing_com__8_; - buttonChange.BackgroundImageLayout = ImageLayout.Stretch; - buttonChange.Location = new Point(40, 149); - buttonChange.Name = "buttonChange"; - buttonChange.Size = new Size(94, 89); - buttonChange.TabIndex = 1; - buttonChange.UseVisualStyleBackColor = true; - buttonChange.Click += ButtonChange_Click; - // // buttonCreate // buttonCreate.BackgroundImage = Properties.Resources.pngwing_com__6_; @@ -83,7 +71,6 @@ // panelButtons // panelButtons.Controls.Add(buttonDel); - panelButtons.Controls.Add(buttonChange); panelButtons.Controls.Add(buttonCreate); panelButtons.Dock = DockStyle.Right; panelButtons.Location = new Point(545, 0); @@ -109,7 +96,6 @@ #endregion private Button buttonDel; - private Button buttonChange; private Button buttonCreate; private DataGridView dataGridView; private Panel panelButtons; diff --git a/ProjectCompRepair/ProjectCompRepair/Forms/FormService.cs b/ProjectCompRepair/ProjectCompRepair/Forms/FormService.cs index 71456c3..b53dade 100644 --- a/ProjectCompRepair/ProjectCompRepair/Forms/FormService.cs +++ b/ProjectCompRepair/ProjectCompRepair/Forms/FormService.cs @@ -41,6 +41,7 @@ namespace ProjectCompRepair.Forms } _service = value; + numericUpDownPrice.Value = (int)service.Price; } catch (Exception ex) { diff --git a/ProjectCompRepair/ProjectCompRepair/Repositories/IOrderRepository.cs b/ProjectCompRepair/ProjectCompRepair/Repositories/IOrderRepository.cs index 863a410..8a02e54 100644 --- a/ProjectCompRepair/ProjectCompRepair/Repositories/IOrderRepository.cs +++ b/ProjectCompRepair/ProjectCompRepair/Repositories/IOrderRepository.cs @@ -17,8 +17,6 @@ public interface IOrderRepository void CreateOrder(Order order); - void UpdateOrder(Order order); - void DeleteOrder(int id); } diff --git a/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/AccessoriesOrderRepository.cs b/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/AccessoriesOrderRepository.cs deleted file mode 100644 index fc0a1c1..0000000 --- a/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/AccessoriesOrderRepository.cs +++ /dev/null @@ -1,26 +0,0 @@ -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/OrderRepository.cs b/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/OrderRepository.cs index 63933cb..42d1ebe 100644 --- a/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/OrderRepository.cs +++ b/ProjectCompRepair/ProjectCompRepair/Repositories/Implemantations/OrderRepository.cs @@ -1,27 +1,27 @@ -using Dapper; -using Microsoft.Extensions.Logging; -using Newtonsoft.Json; -using Npgsql; -using ProjectCompRepair.Entities; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; + using Dapper; + using Microsoft.Extensions.Logging; + using Newtonsoft.Json; + using Npgsql; + using ProjectCompRepair.Entities; + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using System.Threading.Tasks; -namespace ProjectCompRepair.Repositories.Implemantations; + namespace ProjectCompRepair.Repositories.Implemantations; -public class OrderRepository : IOrderRepository -{ - private readonly IConnectionString _connectionString; - - private readonly ILogger _logger; - - public OrderRepository(IConnectionString connectionString, ILogger logger) + public class OrderRepository : IOrderRepository { - _connectionString = connectionString; - _logger = logger; - } + 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("Добавление объекта"); @@ -30,11 +30,43 @@ public class OrderRepository : IOrderRepository { using var connection = new NpgsqlConnection(_connectionString.ConnectionString); connection.Open(); + using var transaction = connection.BeginTransaction(); var queryInsert = @" -INSERT INTO Order (Name, Coment, Date, MasterId, AccessoiresOrders, ServicesOrders) -VALUES (@Name, @Coment, @Date, @MasterId, @AccessoiresOrders, @ServicesOrders)"; - connection.Execute(queryInsert, order); + INSERT INTO Orderl (Name, Coment, Date, MasterId) + VALUES (@Name, @Coment, @Date, @MasterId); + SELECT MAX(ID) FROM Orderl"; + + + var orderId = connection.QueryFirst(queryInsert, order, transaction); + + var querySubInsertAccessories = @" + INSERT INTO AccessoriesOrder (OrderId, Count) + VALUES (@OrderId, @Count)"; + + foreach (var elem in order.AccessoiresOrders) + { + connection.Execute(querySubInsertAccessories, new + { + OrderId = orderId, + elem.Count + }, transaction); + } + + var querySubInsertServices = @" + INSERT INTO ServicesOrder (OrderId, Count) + VALUES (@OrderId, @Count)"; + + foreach (var elem in order.ServicesOrders) + { + connection.Execute(querySubInsertServices, new + { + OrderId = orderId, + elem.Count + }, transaction); + } + + transaction.Commit(); } catch (Exception ex) { @@ -42,102 +74,75 @@ VALUES (@Name, @Coment, @Date, @MasterId, @AccessoiresOrders, @ServicesOrders)"; throw; } } - public void UpdateOrder(Order order) - { - _logger.LogInformation("Редактирование объекта"); - _logger.LogDebug("Объект : {json}", JsonConvert.SerializeObject(order)); - try + + public void DeleteOrder(int id) { - using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - connection.Open(); + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект : {id}", id); + 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); + var queryDelete = @" + DELETE FROM Orderl + WHERE ID = @id + " + ; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } - catch (Exception ex) + public Order ReadOrderById(int id) { - _logger.LogError(ex, "Ошибка при редактировании объекта"); - throw; + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект : {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + + var querySelect = @" + SELECT * FROM Orderl + 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 void DeleteOrder(int id) - { - _logger.LogInformation("Удаление объекта"); - _logger.LogDebug("Объект : {id}", id); - try + public IEnumerable ReadOrder() { - using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - connection.Open(); + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); - var queryDelete = @" -DELETE FROM Order -WHERE ID = @id -" -; - connection.Execute(queryDelete, new { id }); + var querySelect = @"SELECT * FROM Orderl"; + var order = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты : {json}", JsonConvert.SerializeObject(order)); + return order; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } - 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() - { - _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; - } - } -} + }