PIbd-21_Putincev_D.M._SUBD/Booking/BookingView/FormBook.cs

153 lines
6.1 KiB
C#

using BookingAbstractions.Models;
using BookingAbstractions.WorkAbstractions;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
namespace BookingView
{
public partial class FormBook : Form
{
private readonly IBookWork _bookLogic;
private readonly IAuthorWork _authorLogic;
private readonly IPublisherWork _publisherLogic;
public FormBook(IBookWork bookLogic, IAuthorWork authorLogic, IPublisherWork publisherLogic)
{
InitializeComponent();
_bookLogic = bookLogic;
_authorLogic = authorLogic;
_publisherLogic = publisherLogic;
}
private void FormBook_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
var books = _bookLogic.GetAll();
dataGridView.Rows.Clear();
if (dataGridView.ColumnCount == 0)
{
dataGridView.Columns.Add("Id", "ID");
dataGridView.Columns.Add("Name", "Название");
dataGridView.Columns.Add("Genre", "Жанр");
dataGridView.Columns.Add("Date", "Дата публикации");
dataGridView.Columns.Add("ISBN", "ISBN");
dataGridView.Columns.Add("Price", "Цена");
dataGridView.Columns.Add("AuthorId", "AuthorId");
dataGridView.Columns.Add("PublisherId", "PublisherId");
dataGridView.Columns["AuthorId"].Visible = false;
dataGridView.Columns["PublisherId"].Visible = false;
dataGridView.Columns.Add("Author", "Автор");
dataGridView.Columns.Add("Publisher", "Издательство");
}
dataGridView.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Genre"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Date"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["ISBN"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Price"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Author"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Publisher"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
comboBoxAuthor.DataSource = _authorLogic.GetAll();
comboBoxAuthor.DisplayMember = "Author";
comboBoxAuthor.ValueMember = "Name";
comboBoxPublisher.DataSource = _publisherLogic.GetAll();
comboBoxPublisher.DisplayMember = "Publisher";
comboBoxPublisher.ValueMember = "Name";
foreach (var book in books)
{
dataGridView.Rows.Add(book.Id, book.Name, book.Genre, book.Date, book.ISBN, book.Price, book.AuthorId, book.PublisherId, _authorLogic.Get(book.AuthorId)?.Name, _publisherLogic.Get(book.PublisherId)?.Name);
}
}
private void ButtonCreate_Click(object sender, EventArgs e)
{
Book newBook = new()
{
Name = textBoxName.Text,
Genre = textBoxGenre.Text,
Date = dateTimePickerDate.Value,
ISBN = Convert.ToInt32(textBoxISBN.Text),
Price = Convert.ToInt32(textBoxPrice.Text),
AuthorId = ((Author?)comboBoxAuthor.SelectedItem)?.Id ?? 0,
PublisherId = ((Publisher?)comboBoxPublisher.SelectedItem)?.Id ?? 0,
};
_bookLogic.Create(newBook);
LoadData();
}
private void ButtonUpdate_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
int bookId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
Book updatedBook = new()
{
Id = bookId,
Name = textBoxName.Text,
Genre = textBoxGenre.Text,
Date = dateTimePickerDate.Value,
ISBN = Convert.ToInt32(textBoxISBN.Text),
Price = Convert.ToInt32(textBoxPrice.Text),
AuthorId = ((Author?)comboBoxAuthor.SelectedItem)?.Id ?? 0,
PublisherId = ((Publisher?)comboBoxPublisher.SelectedItem)?.Id ?? 0,
};
_bookLogic.Update(updatedBook);
LoadData();
}
else
{
MessageBox.Show("Пожалуйста, выберите отзыв, которое необходимо обновить");
}
}
private void ButtonDelete_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
int bookId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
_bookLogic.Delete(bookId);
LoadData();
}
else
{
MessageBox.Show("Пожалуйста, выберите отзыв, которое необходимо удалить");
}
}
private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = dataGridView.Rows[e.RowIndex];
textBoxName.Text = row.Cells["Name"].Value.ToString();
textBoxGenre.Text = row.Cells["Genre"].Value.ToString();
dateTimePickerDate.Value = DateTime.Parse(row.Cells["Date"].Value.ToString());
textBoxISBN.Text = row.Cells["ISBN"].Value.ToString();
textBoxPrice.Text = row.Cells["Price"].Value.ToString();
comboBoxAuthor.SelectedValue = row.Cells["Author"].Value;
comboBoxPublisher.SelectedValue = row.Cells["Publisher"].Value;
}
}
}
}