PIbd-23_Firsov_KA_SUBD/Hotel/HotelView/FormReservation.cs

157 lines
6.2 KiB
C#
Raw Normal View History

2024-05-29 14:28:53 +04:00
using HotelAbstractions.Logic;
using HotelAbstractions.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HotelView
{
public partial class FormReservation : Form
{
private readonly IReservationLogic _reservationLogic;
private readonly IGuestLogic _guestLogic;
private readonly IRoomLogic _roomLogic;
public FormReservation(IReservationLogic reservationLogic, IGuestLogic guestLogic, IRoomLogic roomLogic)
{
InitializeComponent();
_reservationLogic = reservationLogic;
_guestLogic = guestLogic;
_roomLogic = roomLogic;
}
private void FormReservation_Load(object sender, EventArgs e)
{
LoadData();
}
private void ButtonCreate_Click(object sender, EventArgs e)
{
Reservation newReservation = new()
{
GuestId = ((Guest?)comboBoxGuest.SelectedItem)?.Id ?? 0,
RoomId = ((Room?)comboBoxRoom.SelectedItem)?.Id ?? 0,
ArrivalDate = ToDateOnly(dateTimePickerArrival.Value),
DepartureDate = ToDateOnly(dateTimePickerDeparture.Value),
Price = (int)numericUpDownPrice.Value,
};
_reservationLogic.Create(newReservation);
LoadData();
}
private void LoadData()
{
var reservations = _reservationLogic.GetAll();
dataGridView.Rows.Clear();
if (dataGridView.ColumnCount == 0)
{
dataGridView.Columns.Add("Id", "ID");
dataGridView.Columns.Add("GuestId", "GuestId");
dataGridView.Columns["GuestId"].Visible = false;
dataGridView.Columns.Add("Guest", "Гость");
dataGridView.Columns.Add("RoomId", "RoomId");
dataGridView.Columns["RoomId"].Visible = false;
dataGridView.Columns.Add("Room", "Комната");
dataGridView.Columns.Add("ArrivalDate", "Дата заезда");
dataGridView.Columns.Add("DepartureDate", "Дата выезда");
dataGridView.Columns.Add("Price", "Цена");
}
dataGridView.Columns["Id"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView.Columns["Guest"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Room"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView.Columns["ArrivalDate"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView.Columns["DepartureDate"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView.Columns["Price"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
comboBoxGuest.DataSource = _guestLogic.GetAll();
comboBoxGuest.DisplayMember = "Guest";
comboBoxGuest.ValueMember = "FIO";
comboBoxRoom.DataSource = _roomLogic.GetAll();
comboBoxRoom.DisplayMember = "Room";
comboBoxRoom.ValueMember = "Id";
foreach (var reservation in reservations)
{
dataGridView.Rows.Add(reservation.Id, reservation.GuestId, _guestLogic.Get(reservation.GuestId)?.FIO, reservation.RoomId, reservation.ArrivalDate,
reservation.DepartureDate, reservation.Price);
}
}
private void ButtonUpdate_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
int reservationId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
Reservation updatedReservation = new()
{
Id = reservationId,
GuestId = ((Guest?)comboBoxGuest.SelectedItem)?.Id ?? 0,
RoomId = ((Room?)comboBoxRoom.SelectedItem)?.Id ?? 0,
ArrivalDate = ToDateOnly(dateTimePickerArrival.Value),
DepartureDate = ToDateOnly(dateTimePickerDeparture.Value),
Price = (int)numericUpDownPrice.Value,
};
_reservationLogic.Update(updatedReservation);
LoadData();
}
else
{
MessageBox.Show("Пожалуйста, выберите статью, информацию о которой необходимо обновить");
}
}
private void ButtonDelete_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
int reservationId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
_reservationLogic.Delete(reservationId);
LoadData();
}
else
{
MessageBox.Show("Пожалуйста, выберите статью, информацию о которой необходимо удалить");
}
}
private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = dataGridView.Rows[e.RowIndex];
comboBoxGuest.SelectedValue = row.Cells["Guest"].Value ?? new Guest { FIO = "Неизвестнo" };
comboBoxRoom.SelectedValue = row.Cells["Room"].Value ?? new Room { Id = 0 };
dateTimePickerArrival.Text = row.Cells["ArrivalDate"].Value.ToString();
dateTimePickerDeparture.Text = row.Cells["DepartureDate"].Value.ToString();
numericUpDownPrice.Text = row.Cells["Price"].Value.ToString();
}
}
public DateOnly ToDateOnly(DateTime dateTime)
{
var DateTime = dateTime;
var Date = new DateOnly(DateTime.Year, DateTime.Month, DateTime.Day);
return Date;
}
}
}