PIbd-23_Firsov_KA_SUBD/Hotel/HotelView/FormReservation.cs
2024-05-30 13:12:02 +04:00

230 lines
9.1 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using HotelAbstractions.Logic;
using HotelAbstractions.Models;
using HotelMongoDB.Models;
using HotelMongoDB.StorageContracts;
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;
private readonly StorageModel _mongoLogic;
public FormReservation(IReservationLogic reservationLogic, IGuestLogic guestLogic, IRoomLogic roomLogic, StorageModel mongoLogic)
{
InitializeComponent();
_reservationLogic = reservationLogic;
_guestLogic = guestLogic;
_roomLogic = roomLogic;
_mongoLogic = mongoLogic;
}
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,
};
if (Program.isPostgreSQL)
_reservationLogic.Create(newReservation);
else
_mongoLogic.AddReservation(newReservation);
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;
if (Program.isPostgreSQL)
{
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";
}
}
private void ButtonUpdate_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
if (Program.isPostgreSQL)
{
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(),
};
_mongoLogic.UpdateReservation(updatedReservation);
}
LoadData();
}
else
{
MessageBox.Show("Пожалуйста, выберите статью, информацию о которой необходимо обновить");
}
}
private void ButtonDelete_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
if (Program.isPostgreSQL)
{
int reservationId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
_reservationLogic.Delete(reservationId);
}
else
{
string? reservationId = selectedRow.Cells["Id"].Value.ToString();
_mongoLogic.DeleteRoom(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;
}
}
}