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

230 lines
9.1 KiB
C#
Raw Normal View History

2024-05-29 14:28:53 +04:00
using HotelAbstractions.Logic;
using HotelAbstractions.Models;
2024-05-30 13:12:02 +04:00
using HotelMongoDB.Models;
using HotelMongoDB.StorageContracts;
2024-05-29 14:28:53 +04:00
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;
2024-05-30 13:12:02 +04:00
private readonly StorageModel _mongoLogic;
public FormReservation(IReservationLogic reservationLogic, IGuestLogic guestLogic, IRoomLogic roomLogic, StorageModel mongoLogic)
2024-05-29 14:28:53 +04:00
{
InitializeComponent();
_reservationLogic = reservationLogic;
_guestLogic = guestLogic;
_roomLogic = roomLogic;
2024-05-30 13:12:02 +04:00
_mongoLogic = mongoLogic;
2024-05-29 14:28:53 +04:00
}
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,
};
2024-05-30 13:12:02 +04:00
if (Program.isPostgreSQL)
_reservationLogic.Create(newReservation);
else
_mongoLogic.AddReservation(newReservation);
2024-05-29 14:28:53 +04:00
LoadData();
}
private void LoadData()
{
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;
2024-05-30 13:12:02 +04:00
2024-05-29 14:28:53 +04:00
2024-05-30 13:12:02 +04:00
if (Program.isPostgreSQL)
2024-05-29 14:28:53 +04:00
{
2024-05-30 13:12:02 +04:00
var reservations = _reservationLogic.GetAll();
foreach (var reservation in reservations)
{
dataGridView.Rows.Add(reservation.Id,
reservation.GuestId,
_guestLogic.Get(reservation.GuestId)?.FIO,
reservation.RoomId,
_roomLogic.Get(reservation.RoomId)?.Id,
reservation.ArrivalDate,
reservation.DepartureDate,
reservation.Price);
}
comboBoxGuest.DataSource = _guestLogic.GetAll();
comboBoxGuest.DisplayMember = "Guest";
comboBoxGuest.ValueMember = "FIO";
comboBoxRoom.DataSource = _roomLogic.GetAll();
comboBoxRoom.DisplayMember = "Room";
comboBoxRoom.ValueMember = "Id";
}
else
{
var reservations = _mongoLogic.GetReservations();
foreach (var reservation in reservations)
{
dataGridView.Rows.Add(
reservation.Id,
reservation.GuestId,
_mongoLogic.GetGuestById(reservation.GuestId)?.FIO,
reservation.RoomId,
_mongoLogic.GetRoomById(reservation.RoomId)?.Id,
reservation.ArrivalDate,
reservation.DepartureDate,
reservation.Price);
}
comboBoxGuest.DataSource = _mongoLogic.GetGuests();
comboBoxGuest.DisplayMember = "Guest";
comboBoxGuest.ValueMember = "FIO";
comboBoxRoom.DataSource = _mongoLogic.GetRooms();
comboBoxRoom.DisplayMember = "Room";
comboBoxRoom.ValueMember = "Id";
2024-05-29 14:28:53 +04:00
}
}
private void ButtonUpdate_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
2024-05-30 13:12:02 +04:00
if (Program.isPostgreSQL)
2024-05-29 14:28:53 +04:00
{
2024-05-30 13:12:02 +04:00
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 = (double)numericUpDownPrice.Value,
};
_reservationLogic.Update(updatedReservation);
}
else
{
string? reservationId = selectedRow.Cells["Id"].Value.ToString();
MongoReservation updatedReservation = new()
{
Id = reservationId,
GuestId = _mongoLogic.GetMongoId("Guest", ((Guest?)comboBoxGuest.SelectedItem)?.Id.ToString() ?? String.Empty),
RoomId = _mongoLogic.GetMongoId("Room", ((Room?)comboBoxRoom.SelectedItem)?.Id.ToString() ?? String.Empty),
ArrivalDate = ToDateOnly(dateTimePickerArrival.Value).ToString(),
DepartureDate = ToDateOnly(dateTimePickerDeparture.Value).ToString(),
Price = numericUpDownPrice.Value.ToString(),
};
2024-05-29 14:28:53 +04:00
2024-05-30 13:12:02 +04:00
_mongoLogic.UpdateReservation(updatedReservation);
}
2024-05-29 14:28:53 +04:00
LoadData();
}
else
{
MessageBox.Show("Пожалуйста, выберите статью, информацию о которой необходимо обновить");
}
}
private void ButtonDelete_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
2024-05-30 13:12:02 +04:00
if (Program.isPostgreSQL)
{
int reservationId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
_reservationLogic.Delete(reservationId);
}
else
{
string? reservationId = selectedRow.Cells["Id"].Value.ToString();
2024-05-29 14:28:53 +04:00
2024-05-30 13:12:02 +04:00
_mongoLogic.DeleteRoom(reservationId);
}
2024-05-29 14:28:53 +04:00
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;
}
}
}