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

142 lines
5.3 KiB
C#

using BookingAbstractions.Models;
using BookingAbstractions.WorkAbstractions;
using System.Windows.Forms.VisualStyles;
namespace BookingView
{
public partial class FormBooking : Form
{
private readonly IBookingWork _bookingLogic;
private readonly IClientWork _clientLogic;
private readonly IBookWork _bookLogic;
public FormBooking(IBookingWork bookingLogic, IClientWork clientLogic, IBookWork bookLogic)
{
InitializeComponent();
_bookingLogic = bookingLogic;
_clientLogic = clientLogic;
_bookLogic = bookLogic;
}
private void FormBooking_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
var bookings = _bookingLogic.GetAll();
dataGridView.Rows.Clear();
if (dataGridView.ColumnCount == 0)
{
dataGridView.Columns.Add("Id", "ID");
dataGridView.Columns.Add("Date", "Дата");
dataGridView.Columns.Add("Status", "Статус");
dataGridView.Columns.Add("Sum", "Сумма");
dataGridView.Columns.Add("ClientId", "ClientId");
dataGridView.Columns.Add("BookId", "BookId");
dataGridView.Columns["ClientId"].Visible = false;
dataGridView.Columns["BookId"].Visible = false;
dataGridView.Columns.Add("Client", "Клиент");
dataGridView.Columns.Add("Book", "Книга");
}
dataGridView.Columns["Date"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Status"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Sum"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Client"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Book"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
comboBoxClient.DataSource = _clientLogic.GetAll();
comboBoxClient.DisplayMember = "Client";
comboBoxClient.ValueMember = "Name";
comboBoxBook.DataSource = _bookLogic.GetAll();
comboBoxBook.DisplayMember = "Book";
comboBoxBook.ValueMember = "Name";
foreach (var booking in bookings)
{
dataGridView.Rows.Add(booking.Id, booking.Date, booking.Status, booking.Sum, booking.ClientId, booking.BookId, _clientLogic.Get(booking.ClientId)?.Name, _bookLogic.Get(booking.BookId)?.Name);
}
}
private void ButtonCreate_Click(object sender, EventArgs e)
{
Booking newBooking = new()
{
Date = dateTimePickerDate.Value,
Status = textBoxStatus.Text,
Sum = Convert.ToInt32(textBoxSum.Text),
ClientId = ((Client?)comboBoxClient.SelectedItem)?.Id ?? 0,
BookId = ((Book?)comboBoxBook.SelectedItem)?.Id ?? 0,
};
_bookingLogic.Create(newBooking);
LoadData();
}
private void ButtonUpdate_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
int bookingId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
Booking updatedBooking = new()
{
Id = bookingId,
Date = dateTimePickerDate.Value,
Status = textBoxStatus.Text,
Sum = Convert.ToInt32(textBoxSum.Text),
ClientId = ((Client?)comboBoxClient.SelectedItem)?.Id ?? 0,
BookId = ((Book?)comboBoxBook.SelectedItem)?.Id ?? 0,
};
_bookingLogic.Update(updatedBooking);
LoadData();
}
else
{
MessageBox.Show("Пожалуйста, выберите отзыв, которое необходимо обновить");
}
}
private void ButtonDelete_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
int bookingId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
_bookingLogic.Delete(bookingId);
LoadData();
}
else
{
MessageBox.Show("Пожалуйста, выберите отзыв, которое необходимо удалить");
}
}
private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = dataGridView.Rows[e.RowIndex];
dateTimePickerDate.Value = DateTime.Parse(row.Cells["Date"].Value.ToString());
textBoxStatus.Text = row.Cells["Status"].Value.ToString();
textBoxSum.Text = row.Cells["Sum"].Value.ToString();
comboBoxClient.SelectedValue = row.Cells["Client"].Value;
comboBoxBook.SelectedValue =row.Cells["Book"].Value;
}
}
}
}