This commit is contained in:
nezui1 2024-11-25 14:49:17 +04:00
parent 09557899c2
commit 95d2100685
8 changed files with 129 additions and 175 deletions

View File

@ -23,15 +23,17 @@ public class Order
public IEnumerable<ServicesOrder> 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<AccessoiresOrder> accessoiresOrders, IEnumerable<ServicesOrder> servicesOrder)
{
return new Order()
{
Id = id,
Name = name,
Coment = coment,
Date = DateTime.Now,
MasterID = masterId
Date = date,
MasterID = masterId,
AccessoiresOrders = accessoiresOrders,
ServicesOrders = servicesOrder
};
}

View File

@ -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<AccessoiresOrder>();
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<ServicesOrder> CreateListServicesOrderFromDataGrid()
{
var list = new List<ServicesOrder>();
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;
}

View File

@ -123,18 +123,6 @@
<metadata name="ColumnCount.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="ColumnAccessories.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="ColumnCount.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="ColumnService.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="ColumnCountServ.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="ColumnService.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>

View File

@ -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;

View File

@ -41,6 +41,7 @@ namespace ProjectCompRepair.Forms
}
_service = value;
numericUpDownPrice.Value = (int)service.Price;
}
catch (Exception ex)
{

View File

@ -17,8 +17,6 @@ public interface IOrderRepository
void CreateOrder(Order order);
void UpdateOrder(Order order);
void DeleteOrder(int id);
}

View File

@ -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<AccessoiresOrder> ReadAccessoiresOrder(int? Id = 0, int? orderId = 0, int? count = 0)
{
throw new NotImplementedException();
}
}

View File

@ -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<AccessoriesRepository> _logger;
public OrderRepository(IConnectionString connectionString, ILogger<AccessoriesRepository> logger)
public class OrderRepository : IOrderRepository
{
_connectionString = connectionString;
_logger = logger;
}
private readonly IConnectionString _connectionString;
private readonly ILogger<AccessoriesRepository> _logger;
public OrderRepository(IConnectionString connectionString, ILogger<AccessoriesRepository> 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<int>(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
{
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)
public void DeleteOrder(int id)
{
_logger.LogError(ex, "Ошибка при редактировании объекта");
throw;
_logger.LogInformation("Удаление объекта");
_logger.LogDebug("Объект : {id}", id);
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
var queryDelete = @"
DELETE FROM Orderl
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 Orderl
WHERE ID = @id
";
var order = connection.QueryFirst<Order>(querySelect, new { id });
_logger.LogDebug("Найденный объект {json}", JsonConvert.SerializeObject(order));
return order;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при поиске объекта");
throw;
}
}
public IEnumerable<Order> ReadOrder()
{
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
var querySelect = @"SELECT * FROM Orderl";
var order = connection.Query<Order>(querySelect);
_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
{
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<Order>(querySelect, new { id });
_logger.LogDebug("Найденный объект {json}", JsonConvert.SerializeObject(order));
return order;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при поиске объекта");
throw;
}
}
public IEnumerable<Order> ReadOrder()
{
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
var querySelect = @"SELECT * FROM Order";
var order = connection.Query<Order>(querySelect);
_logger.LogDebug("Полученные объекты : {json}", JsonConvert.SerializeObject(order));
return order;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при чтении объектов");
throw;
}
}
}