95 lines
3.1 KiB
C#
95 lines
3.1 KiB
C#
using ProjectLibrary.Entites;
|
||
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;
|
||
dataGridView.DataSource = library.BookLibrary;
|
||
_orderId = value;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "Ошибка при загрузке данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
|
||
public FLibrary(ILibraryRepository libraryRepository)
|
||
{
|
||
InitializeComponent();
|
||
_libraryRepository = libraryRepository ?? throw new ArgumentNullException(nameof(libraryRepository));
|
||
}
|
||
|
||
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)
|
||
{
|
||
this.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["ColumnBook"].Value), Convert.ToInt32(row.Cells["ColumnCount"].Value)));
|
||
}
|
||
return list;
|
||
}
|
||
}
|
||
}
|