ProjectLib/ProjectLibrary/Forms/FOrder.cs

74 lines
2.4 KiB
C#
Raw Normal View History

using ProjectLibrary.Repositories;
using ProjectLibrary.Entites;
2024-11-20 19:31:06 +04:00
using System.Xml.Linq;
2024-12-08 13:24:07 +04:00
using ProjectLibrary.Repositores;
using System.Windows.Forms;
namespace ProjectLibrary.Forms
{
public partial class FOrder : Form
{
private readonly IOrderRepository _orderRepository;
2024-12-08 13:24:07 +04:00
private readonly IBookRepository _bookRepository;
2024-12-08 13:24:07 +04:00
public FOrder(IOrderRepository orderRepository, IReaderRepository _readerRepository, IBookRepository bookRepository)
{
InitializeComponent();
2024-12-08 13:24:07 +04:00
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository));
_bookRepository = bookRepository ?? throw new ArgumentNullException(nameof(_bookRepository));
comboBox.DataSource = _readerRepository.ReadReaders();
comboBox.DisplayMember = "Name";
comboBox.ValueMember = "Id";
2024-12-08 13:24:07 +04:00
Book.DataSource = bookRepository.ReadBooks();
Book.DisplayMember = "Name";
Book.ValueMember = "Id";
}
2024-11-20 19:31:06 +04:00
private List<Book_Orders> CreateListBooksFromDataGrid()
{
var list = new List<Book_Orders>();
2024-11-20 19:31:06 +04:00
foreach (DataGridViewRow row in dataGridViewBook.Rows)
{
2024-12-08 13:24:07 +04:00
if (row.Cells["Book"].Value == null)
{
continue;
}
2024-12-20 01:07:20 +04:00
list.Add(Book_Orders.CreateEntity(0, Convert.ToInt32(row.Cells["Book"].Value), Convert.ToInt32(row.Cells["ColumnCount"].Value)));
}
return list;
}
2024-11-20 19:31:06 +04:00
private void ButtonSave_Click(object sender, EventArgs e)
{
try
{
2024-12-08 13:24:07 +04:00
if (comboBox.SelectedItem == null)
{
throw new Exception("Не все поля заполнены.");
}
2024-11-20 19:31:06 +04:00
var order = Orders.CreateEntity(
2024-12-08 13:24:07 +04:00
0,
dtptxtReturnDate.Value,
Convert.ToInt32(comboBox.SelectedValue),
2024-11-20 19:31:06 +04:00
CreateListBooksFromDataGrid()
);
2024-12-08 13:24:07 +04:00
_orderRepository.CreateOrder(order);
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при сохранении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
Close();
}
}
}