2024-11-19 19:46:02 +04:00
|
|
|
|
using ProjectLibrary.Repositories;
|
|
|
|
|
using ProjectLibrary.Entites;
|
2024-11-20 19:31:06 +04:00
|
|
|
|
using System.Xml.Linq;
|
2024-11-19 19:46:02 +04:00
|
|
|
|
|
|
|
|
|
namespace ProjectLibrary.Forms
|
|
|
|
|
{
|
|
|
|
|
public partial class FOrder : Form
|
|
|
|
|
{
|
|
|
|
|
private readonly IOrderRepository _orderRepository;
|
|
|
|
|
private int? _orderId;
|
|
|
|
|
|
2024-11-20 19:31:06 +04:00
|
|
|
|
public FOrder(IOrderRepository orderRepository)
|
2024-11-19 19:46:02 +04:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2024-11-20 19:31:06 +04:00
|
|
|
|
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository));
|
2024-11-19 19:46:02 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-20 19:31:06 +04:00
|
|
|
|
public int Id
|
2024-11-19 19:46:02 +04:00
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
2024-11-20 19:31:06 +04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var order = _orderRepository.ReadOrderById(value);
|
|
|
|
|
if (order == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Заказ не найден.");
|
|
|
|
|
}
|
2024-11-19 19:46:02 +04:00
|
|
|
|
|
2024-11-20 19:31:06 +04:00
|
|
|
|
txtOrderDate.Text = order.OrderDate.ToString();
|
|
|
|
|
txtReturnDate.Text = order.ReturnDate.ToString();
|
|
|
|
|
comboBox.SelectedItem = order.ReaderID;
|
|
|
|
|
dataGridViewBook.DataSource = order.BookOrders;
|
|
|
|
|
_orderId = value;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка при загрузке данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
2024-11-19 19:46:02 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-20 19:31:06 +04:00
|
|
|
|
private List<Book_Orders> CreateListBooksFromDataGrid()
|
2024-11-19 19:46:02 +04:00
|
|
|
|
{
|
|
|
|
|
var list = new List<Book_Orders>();
|
2024-11-20 19:31:06 +04:00
|
|
|
|
foreach (DataGridViewRow row in dataGridViewBook.Rows)
|
2024-11-19 19:46:02 +04:00
|
|
|
|
{
|
2024-11-20 19:31:06 +04:00
|
|
|
|
if (row.Cells["ColumnBook"].Value == null)
|
2024-11-19 19:46:02 +04:00
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-11-20 19:31:06 +04:00
|
|
|
|
list.Add(Book_Orders.CreateEntity(0, Convert.ToInt32(row.Cells["ColumnBook"].Value)));
|
2024-11-19 19:46:02 +04:00
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
2024-11-20 19:31:06 +04:00
|
|
|
|
|
2024-11-19 19:46:02 +04:00
|
|
|
|
private void ButtonSave_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-11-20 19:31:06 +04:00
|
|
|
|
if (string.IsNullOrWhiteSpace(txtOrderDate.Text) || string.IsNullOrWhiteSpace(txtReturnDate.Text) || comboBox.SelectedItem == null)
|
2024-11-19 19:46:02 +04:00
|
|
|
|
{
|
|
|
|
|
throw new Exception("Не все поля заполнены.");
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-20 19:31:06 +04:00
|
|
|
|
var order = Orders.CreateEntity(
|
2024-11-19 19:46:02 +04:00
|
|
|
|
_orderId ?? 0,
|
|
|
|
|
DateTime.Parse(txtOrderDate.Text),
|
|
|
|
|
DateTime.Parse(txtReturnDate.Text),
|
2024-11-20 19:31:06 +04:00
|
|
|
|
int.Parse(comboBox.SelectedItem.ToString()),
|
|
|
|
|
CreateListBooksFromDataGrid()
|
2024-11-19 19:46:02 +04:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (_orderId.HasValue)
|
|
|
|
|
{
|
2024-11-20 19:31:06 +04:00
|
|
|
|
_orderRepository.UpdateOrder(order);
|
2024-11-19 19:46:02 +04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-11-20 19:31:06 +04:00
|
|
|
|
_orderRepository.CreateOrder(order);
|
2024-11-19 19:46:02 +04:00
|
|
|
|
}
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка при сохранении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ButtonCancel_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|