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