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