почти все. сломалось
This commit is contained in:
parent
bb69e427c8
commit
f6e89b6040
@ -14,34 +14,22 @@ public class Order
|
||||
public DateTime DateIssue { get; private set; }
|
||||
public int FullPrice { get; private set; }
|
||||
public int MasterId { get; private set; }
|
||||
public List<OrderDetail> OrderDetails { get; private set; }
|
||||
|
||||
private Order()
|
||||
public IEnumerable<OrderDetail> OrderDetails { get; private set; } = [];
|
||||
|
||||
public static Order CreateOperation(int id, int fullPrice, int masterId, DateTime dateCompletion,
|
||||
DateTime dateIssue, IEnumerable<OrderDetail> orderDetails)
|
||||
{
|
||||
OrderDetails = new List<OrderDetail>();
|
||||
}
|
||||
|
||||
public static Order CreateOperation(int id, int fullPrice, int masterId, DateTime dateCompletion, DateTime dateIssue)
|
||||
{
|
||||
if (dateCompletion > DateTime.Now || dateIssue > DateTime.Now)
|
||||
{
|
||||
throw new ArgumentException("Дата начала работы и дата завершения не могут быть в будущем.");
|
||||
}
|
||||
|
||||
|
||||
return new Order
|
||||
{
|
||||
Id = id,
|
||||
OrderDate = DateTime.Now, // Автоматически задается текущая дата
|
||||
OrderDate = DateTime.Now,
|
||||
DateCompletion = dateCompletion,
|
||||
DateIssue = dateIssue,
|
||||
FullPrice = fullPrice,
|
||||
MasterId = masterId
|
||||
MasterId = masterId,
|
||||
OrderDetails = orderDetails
|
||||
};
|
||||
}
|
||||
|
||||
public void AddOrderDetail(int detailId, int detailCount)
|
||||
{
|
||||
var orderDetail = OrderDetail.CreateOperation(this.Id, detailId, detailCount);
|
||||
OrderDetails.Add(orderDetail);
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ public class OrderDetail
|
||||
{
|
||||
public int OrderId { get; private set; }
|
||||
public int DetailId { get; private set; }
|
||||
public int DetailCount { get; private set; }
|
||||
public int DetailCount { get; private set; }
|
||||
|
||||
public static OrderDetail CreateOperation(int orderId, int detailId, int detailCount)
|
||||
{
|
||||
|
@ -165,6 +165,7 @@
|
||||
groupBox1.Size = new Size(401, 224);
|
||||
groupBox1.TabIndex = 12;
|
||||
groupBox1.TabStop = false;
|
||||
groupBox1.Text = "Деталь";
|
||||
//
|
||||
// dataGridView
|
||||
//
|
||||
@ -183,7 +184,7 @@
|
||||
//
|
||||
// Detail
|
||||
//
|
||||
Detail.HeaderText = "Деталь";
|
||||
Detail.HeaderText = "Название детали";
|
||||
Detail.Name = "Detail";
|
||||
//
|
||||
// DetailCount
|
||||
|
@ -1,5 +1,7 @@
|
||||
using ProjectRepairCompany.Entities;
|
||||
using ProjectRepairCompany.Entities.Enums;
|
||||
using ProjectRepairCompany.Repositories;
|
||||
using ProjectRepairCompany.Repositories.Implementations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.Metrics;
|
||||
@ -8,100 +10,121 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace ProjectRepairCompany.Forms
|
||||
namespace ProjectRepairCompany.Forms;
|
||||
|
||||
public partial class FormOrder : Form
|
||||
{
|
||||
public partial class FormOrder : Form
|
||||
private readonly IOrderRepository _orderRepository;
|
||||
private int? _orderId;
|
||||
|
||||
public int Id
|
||||
{
|
||||
private readonly IOrderRepository _orderRepository;
|
||||
private readonly IDetailRepository _detailRepository;
|
||||
private int? _orderId;
|
||||
|
||||
public int Id
|
||||
{
|
||||
set
|
||||
{
|
||||
try
|
||||
{
|
||||
var order = _orderRepository.ReadOrderById(value);
|
||||
if (order == null)
|
||||
{
|
||||
throw new InvalidOperationException(nameof(order));
|
||||
}
|
||||
numericUpDownFullPrice.Value = order.FullPrice;
|
||||
comboBoxMaster.SelectedValue = order.MasterId;
|
||||
dateTimeCompletion.Value = order.DateCompletion;
|
||||
dateTimeIssue.Value = order.DateIssue;
|
||||
|
||||
foreach (var detail in order.OrderDetails)
|
||||
{
|
||||
dataGridView.Rows.Add(detail.DetailId, detail.DetailCount);
|
||||
}
|
||||
|
||||
_orderId = value;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public FormOrder(IOrderRepository orderRepository, IMasterRepository masterRepository, IDetailRepository detailRepository)
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository));
|
||||
_detailRepository = detailRepository ?? throw new ArgumentNullException(nameof(detailRepository));
|
||||
|
||||
comboBoxMaster.DataSource = masterRepository.ReadMasters();
|
||||
comboBoxMaster.DisplayMember = "MasterName";
|
||||
comboBoxMaster.ValueMember = "Id";
|
||||
|
||||
Detail.DataSource = _detailRepository.ReadDetails();
|
||||
Detail.DisplayMember = "NameDetail";
|
||||
Detail.ValueMember = "Id";
|
||||
}
|
||||
|
||||
private void ButtonSave_Click(object sender, EventArgs e)
|
||||
set
|
||||
{
|
||||
try
|
||||
{
|
||||
if (dataGridView.RowCount < 1 || comboBoxMaster.SelectedIndex < 0)
|
||||
var order = _orderRepository.ReadOrderById(value);
|
||||
if (order == null)
|
||||
{
|
||||
throw new Exception("Имеются незаполненные поля.");
|
||||
throw new InvalidOperationException(nameof(order));
|
||||
}
|
||||
|
||||
var order = Order.CreateOperation(_orderId ?? 0, Convert.ToInt32(numericUpDownFullPrice.Value),
|
||||
(int)comboBoxMaster.SelectedValue, dateTimeCompletion.Value, dateTimeIssue.Value);
|
||||
comboBoxMaster.SelectedValue = order.MasterId;
|
||||
numericUpDownFullPrice.Value = (decimal)order.FullPrice;
|
||||
dateTimeCompletion.Value = order.DateCompletion;
|
||||
dateTimeIssue.Value = order.DateIssue;
|
||||
|
||||
foreach (DataGridViewRow row in dataGridView.Rows)
|
||||
dataGridView.Rows.Clear();
|
||||
foreach (var detail in order.OrderDetails)
|
||||
{
|
||||
if (row.Cells["Detail"].Value != null && row.Cells["DetailCount"].Value != null)
|
||||
{
|
||||
var detailId = (int)row.Cells["Detail"].Value;
|
||||
var detailCount = (int)row.Cells["DetailCount"].Value;
|
||||
order.AddOrderDetail(detailId, detailCount);
|
||||
}
|
||||
dataGridView.Rows.Add(detail.DetailId, detail.DetailCount);
|
||||
}
|
||||
|
||||
if (_orderId.HasValue)
|
||||
{
|
||||
_orderRepository.UpdateOrder(order);
|
||||
}
|
||||
else
|
||||
{
|
||||
_orderRepository.CreateOrder(order);
|
||||
}
|
||||
|
||||
Close();
|
||||
_orderId = value;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
|
||||
}
|
||||
|
||||
public FormOrder(IOrderRepository orderRepository, IMasterRepository masterRepository, IDetailRepository detailRepository)
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository));
|
||||
|
||||
|
||||
comboBoxMaster.DataSource = masterRepository.ReadMasters();
|
||||
comboBoxMaster.DisplayMember = "MasterName";
|
||||
comboBoxMaster.ValueMember = "Id";
|
||||
|
||||
Detail.DataSource = detailRepository.ReadDetails();
|
||||
Detail.DisplayMember = "NameDetail";
|
||||
Detail.ValueMember = "Id";
|
||||
}
|
||||
|
||||
private void ButtonSave_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (dataGridView.RowCount < 1 || comboBoxMaster.SelectedIndex < 0)
|
||||
{
|
||||
throw new Exception("Имеются незаполненные поля.");
|
||||
}
|
||||
|
||||
var order = CreateOrder();
|
||||
if (_orderId.HasValue)
|
||||
{
|
||||
_orderRepository.UpdateOrder(order);
|
||||
}
|
||||
else
|
||||
{
|
||||
_orderRepository.CreateOrder(order);
|
||||
}
|
||||
DialogResult = DialogResult.OK;
|
||||
Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
|
||||
|
||||
private List<OrderDetail> CreateListOrderDetailsFromDataGrid()
|
||||
{
|
||||
var list = new List<OrderDetail>();
|
||||
foreach (DataGridViewRow row in dataGridView.Rows)
|
||||
{
|
||||
if (row.Cells["Detail"].Value == null ||
|
||||
row.Cells["DetailCount"].Value == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
list.Add(OrderDetail.CreateOperation(0,
|
||||
Convert.ToInt32(row.Cells["Detail"].Value),
|
||||
Convert.ToInt32(row.Cells["DetailCount"].Value)));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
private Order CreateOrder()
|
||||
{
|
||||
var orderDetails = CreateListOrderDetailsFromDataGrid();
|
||||
|
||||
return Order.CreateOperation(
|
||||
_orderId ?? 0,
|
||||
Convert.ToInt32(numericUpDownFullPrice.Value),
|
||||
(int)comboBoxMaster.SelectedValue,
|
||||
dateTimeCompletion.Value,
|
||||
dateTimeIssue.Value,
|
||||
orderDetails
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
using ProjectRepairCompany.Entities;
|
||||
using ProjectRepairCompany.Repositories;
|
||||
using ProjectRepairCompany.Repositories.Implementations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
@ -61,9 +62,14 @@ namespace ProjectRepairCompany.Forms
|
||||
try
|
||||
{
|
||||
var form = _container.Resolve<FormOrder>();
|
||||
form.Id = findId;
|
||||
form.ShowDialog();
|
||||
LoadList();
|
||||
|
||||
form.Id = findId;
|
||||
|
||||
if (form.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
LoadList();
|
||||
dataGridView.Refresh();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -93,17 +99,8 @@ namespace ProjectRepairCompany.Forms
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadList()
|
||||
{
|
||||
try
|
||||
{
|
||||
dataGridView.DataSource = _orderRepository.ReadOrders();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message, "Ошибка загрузки данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
private void LoadList() => dataGridView.DataSource =_orderRepository.ReadOrders();
|
||||
|
||||
|
||||
private bool TryGetIdentifierFromSelectRow(out int id)
|
||||
{
|
||||
|
@ -25,85 +25,101 @@ public class OrderRepository : IOrderRepository
|
||||
}
|
||||
public void CreateOrder(Order order)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(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 ""Order"" (OrderDate, DateCompletion, DateIssue, FullPrice, MasterId)
|
||||
VALUES (@OrderDate, @DateCompletion, @DateIssue, @FullPrice, @MasterId);
|
||||
SELECT MAX(Id) FROM ""Order""";
|
||||
INSERT INTO ""Order"" (OrderDate, DateCompletion, DateIssue, FullPrice, MasterId)
|
||||
VALUES (@OrderDate, @DateCompletion, @DateIssue, @FullPrice, @MasterId)
|
||||
RETURNING Id";
|
||||
|
||||
var orderId = connection.QueryFirst<int>(queryInsert, order, transaction);
|
||||
_logger.LogInformation("Новый заказ создан с Id {OrderId}", orderId);
|
||||
|
||||
var querySubInsert = @"
|
||||
INSERT INTO OrderDetail (OrderId, DetailId, DetailCount)
|
||||
VALUES (@OrderId, @DetailId, @DetailCount)";
|
||||
foreach (var elem in order.OrderDetails)
|
||||
INSERT INTO OrderDetail (OrderId, DetailId, DetailCount)
|
||||
VALUES (@OrderId, @DetailId, @DetailCount)";
|
||||
|
||||
foreach (var elem in order.OrderDetails)
|
||||
{
|
||||
connection.Execute(querySubInsert, new {orderId, elem.DetailId, elem.DetailCount}, transaction);
|
||||
_logger.LogDebug("Добавление детали: DetailId = {DetailId}, DetailCount = {DetailCount}",
|
||||
elem.DetailId, elem.DetailCount);
|
||||
connection.Execute(querySubInsert, new { orderId, elem.DetailId, elem.DetailCount }, transaction);
|
||||
}
|
||||
|
||||
transaction.Commit();
|
||||
_logger.LogInformation("Заказ с деталями успешно добавлен.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка добавления");
|
||||
_logger.LogError(ex, "Ошибка при добавлении заказа.");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateOrder(Order order)
|
||||
{
|
||||
_logger.LogInformation("Обновление заказа с Id {OrderId}", order.Id);
|
||||
_logger.LogDebug("Обновляемый объект заказа: {json}", JsonConvert.SerializeObject(order));
|
||||
|
||||
try
|
||||
{
|
||||
_logger.LogInformation("Обновление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(order));
|
||||
try
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
using var transaction = connection.BeginTransaction();
|
||||
|
||||
var queryUpdate = @"
|
||||
UPDATE ""Order""
|
||||
SET DateCompletion = @DateCompletion,
|
||||
DateIssue = @DateIssue,
|
||||
FullPrice = @FullPrice,
|
||||
MasterId = @MasterId
|
||||
WHERE Id = @Id";
|
||||
|
||||
connection.Execute(queryUpdate, new
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
using var transaction = connection.BeginTransaction();
|
||||
order.Id,
|
||||
order.DateCompletion,
|
||||
order.DateIssue,
|
||||
order.FullPrice,
|
||||
order.MasterId
|
||||
}, transaction);
|
||||
|
||||
var queryUpdate = @"
|
||||
UPDATE ""Order""
|
||||
SET DateCompletion = @DateCompletion,
|
||||
DateIssue = @DateIssue,
|
||||
FullPrice = @FullPrice,
|
||||
MasterId = @MasterId
|
||||
WHERE Id = @Id";
|
||||
_logger.LogInformation("Заказ с Id {OrderId} обновлен.", order.Id);
|
||||
|
||||
connection.Execute(queryUpdate, new
|
||||
var queryDeleteDetails = "DELETE FROM OrderDetail WHERE OrderId = @OrderId";
|
||||
connection.Execute(queryDeleteDetails, new { order.Id }, transaction);
|
||||
|
||||
var queryInsertDetails = @"
|
||||
INSERT INTO OrderDetail (OrderId, DetailId, DetailCount)
|
||||
VALUES (@OrderId, @DetailId, @DetailCount)";
|
||||
|
||||
foreach (var detail in order.OrderDetails)
|
||||
{
|
||||
connection.Execute(queryInsertDetails, new
|
||||
{
|
||||
order.Id,
|
||||
order.DateCompletion,
|
||||
order.DateIssue,
|
||||
order.FullPrice,
|
||||
order.MasterId
|
||||
detail.DetailId,
|
||||
detail.DetailCount
|
||||
}, transaction);
|
||||
|
||||
var queryDeleteDetails = "DELETE FROM OrderDetail WHERE OrderId = @OrderId";
|
||||
connection.Execute(queryDeleteDetails, new { order.Id }, transaction);
|
||||
|
||||
var queryInsertDetails = @"
|
||||
INSERT INTO OrderDetail (OrderId, DetailId, DetailCount)
|
||||
VALUES (@OrderId, @DetailId, @DetailCount)";
|
||||
|
||||
foreach (var detail in order.OrderDetails)
|
||||
{
|
||||
connection.Execute(queryInsertDetails, new
|
||||
{
|
||||
order.Id,
|
||||
detail.DetailId,
|
||||
detail.DetailCount
|
||||
}, transaction);
|
||||
}
|
||||
|
||||
transaction.Commit();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при обновлении");
|
||||
throw;
|
||||
}
|
||||
|
||||
transaction.Commit();
|
||||
_logger.LogInformation("Детали для заказа с Id {OrderId} обновлены.", order.Id);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при обновлении заказа с Id {OrderId}.", order.Id);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void DeleteOrder(int id)
|
||||
|
Loading…
x
Reference in New Issue
Block a user