114 lines
3.7 KiB
C#
114 lines
3.7 KiB
C#
using ProjectLibrary.Entites;
|
||
using ProjectLibrary.Repositores;
|
||
using ProjectLibrary.Repositories;
|
||
using ProjectLibrary.Repositories.Implementations;
|
||
using System.Windows.Forms;
|
||
|
||
|
||
namespace ProjectLibrary.Forms
|
||
{
|
||
public partial class FLibrary : Form
|
||
{
|
||
private readonly ILibraryRepository _libraryRepository;
|
||
private int? _orderId;
|
||
public int Id
|
||
{
|
||
set
|
||
{
|
||
try
|
||
{
|
||
var library = _libraryRepository.ReadLibraryById(value);
|
||
if (library == null)
|
||
{
|
||
throw new InvalidOperationException("Заказ не найден.");
|
||
}
|
||
|
||
txtName.Text = library.Name;
|
||
txtAddress.Text = library.Address;
|
||
SetListBooksToDataGrid(library.BookLibrary);
|
||
_orderId = value;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "Ошибка при загрузке данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
|
||
public FLibrary(ILibraryRepository libraryRepository, IBookRepository bookRepository)
|
||
{
|
||
InitializeComponent();
|
||
_libraryRepository = libraryRepository ?? throw new ArgumentNullException(nameof(libraryRepository));
|
||
|
||
Book.DataSource = bookRepository.ReadBooks();
|
||
Book.DisplayMember = "Name";
|
||
Book.ValueMember = "Id";
|
||
}
|
||
|
||
private void ButtonSave_Click(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
if (string.IsNullOrWhiteSpace(txtName.Text) || string.IsNullOrWhiteSpace(txtAddress.Text))
|
||
{
|
||
throw new Exception("Не все поля заполнены.");
|
||
}
|
||
|
||
var library = Library.CreateEntity(
|
||
_orderId ?? 0,
|
||
txtName.Text,
|
||
txtAddress.Text,
|
||
CreateListBooksFromDataGrid()
|
||
);
|
||
|
||
if (_orderId.HasValue)
|
||
{
|
||
_libraryRepository.UpdateLibrary(library);
|
||
}
|
||
else
|
||
{
|
||
_libraryRepository.CreateLibrary(library);
|
||
}
|
||
|
||
|
||
Close();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "Ошибка при сохранении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
|
||
private void buttonCancel_Click_Click(object sender, EventArgs e)
|
||
{
|
||
Close();
|
||
}
|
||
|
||
private List<Book_Library> CreateListBooksFromDataGrid()
|
||
{
|
||
var list = new List<Book_Library>();
|
||
foreach (DataGridViewRow row in dataGridView.Rows)
|
||
{
|
||
if (row.Cells["Book"].Value == null || row.Cells["Count"].Value == null)
|
||
{
|
||
continue;
|
||
}
|
||
list.Add(Book_Library.CreateEntity(0, Convert.ToInt32(row.Cells["Book"].Value), Convert.ToInt32(row.Cells["Count"].Value)));
|
||
}
|
||
return list;
|
||
}
|
||
|
||
private void SetListBooksToDataGrid(IEnumerable<Book_Library> list)
|
||
{
|
||
int index = 0;
|
||
foreach (Book_Library elem in list)
|
||
{
|
||
dataGridView.Rows.Add();
|
||
dataGridView.Rows[index].Cells["Book"].Value = elem.BookID;
|
||
dataGridView.Rows[index].Cells["Count"].Value = elem.Count;
|
||
index++;
|
||
}
|
||
}
|
||
}
|
||
}
|