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

201 lines
7.7 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 FormRoom : Form
{
private readonly IRoomLogic _roomLogic;
private readonly IHotelLogic _hotelLogic;
private readonly StorageModel _mongoLogic;
public FormRoom(IRoomLogic roomLogic, IHotelLogic hotelLogic, StorageModel mongoLogic)
{
InitializeComponent();
_roomLogic = roomLogic;
_hotelLogic = hotelLogic;
_mongoLogic = mongoLogic;
}
private void FormRoom_Load(object sender, EventArgs e)
{
LoadData();
}
private void ButtonCreate_Click(object sender, EventArgs e)
{
Room newRoom = new()
{
HotelId = ((Hotel?)comboBoxHotel.SelectedItem)?.Id ?? 0,
Category = textBoxCategory.Text,
CountPlace = (int)numericUpDownCountPlace.Value,
Flor = (int)numericUpFlor.Value,
Number = textBoxNumber.Text,
Price = (int)numericUpDownPrice.Value,
};
if (Program.isPostgreSQL)
_roomLogic.Create(newRoom);
else
_mongoLogic.AddRoom(newRoom);
LoadData();
}
private void LoadData()
{
dataGridView.Rows.Clear();
if (dataGridView.ColumnCount == 0)
{
dataGridView.Columns.Add("Id", "ID");
dataGridView.Columns.Add("HotelId", "HotelId");
dataGridView.Columns["HotelId"].Visible = false;
dataGridView.Columns.Add("Hotel", "Гостиница");
dataGridView.Columns.Add("Category", "Категория");
dataGridView.Columns.Add("CountPlace", "Количество мест");
dataGridView.Columns.Add("Flor", "Этаж");
dataGridView.Columns.Add("Number", "Номер");
dataGridView.Columns.Add("Price", "Цена");
}
dataGridView.Columns["Id"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView.Columns["Hotel"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Category"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView.Columns["CountPlace"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView.Columns["Flor"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView.Columns["Number"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView.Columns["Price"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
if (Program.isPostgreSQL)
{
var rooms = _roomLogic.GetAll();
foreach (var room in rooms)
{
dataGridView.Rows.Add(room.Id, room.HotelId, _hotelLogic.Get(room.HotelId)?.HotelName, room.Category, room.CountPlace,
room.Flor, room.Number, room.Price);
}
comboBoxHotel.DataSource = _hotelLogic.GetAll();
comboBoxHotel.DisplayMember = "Hotel";
comboBoxHotel.ValueMember = "HotelName";
}
else
{
var rooms = _mongoLogic.GetRooms();
foreach (var room in rooms)
{
dataGridView.Rows.Add(room.Id, room.HotelId, _mongoLogic.GetHotelById(room.HotelId)?.HotelName, room.Category, room.CountPlace,
room.Flor, room.Number, room.Price);
}
comboBoxHotel.DataSource = _mongoLogic.GetHotels();
comboBoxHotel.DisplayMember = "Hotel";
comboBoxHotel.ValueMember = "HotelName";
}
}
private void ButtonUpdate_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
if (Program.isPostgreSQL)
{
int roomId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
Room updatedRoom = new()
{
Id = roomId,
HotelId = ((Hotel?)comboBoxHotel.SelectedItem)?.Id ?? 0,
Category = textBoxCategory.Text,
CountPlace = (int)numericUpDownCountPlace.Value,
Flor = (int)numericUpFlor.Value,
Number = textBoxNumber.Text,
Price = (int)numericUpDownPrice.Value,
};
_roomLogic.Update(updatedRoom);
} else
{
string? roomId = selectedRow.Cells["Id"].Value.ToString();
MongoRoom updatedRoom = new()
{
Id = roomId,
HotelId = _mongoLogic.GetMongoId("Hotel", ((Hotel?)comboBoxHotel.SelectedItem)?.Id.ToString() ?? String.Empty),
Category = textBoxCategory.Text,
CountPlace = numericUpDownCountPlace.Value.ToString(),
Flor = numericUpFlor.Value.ToString(),
Number = textBoxNumber.Text,
Price = numericUpDownPrice.Value.ToString(),
};
_mongoLogic.UpdateRoom(updatedRoom);
}
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 roomId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
_roomLogic.Delete(roomId);
}
else
{
string? roomId = selectedRow.Cells["Id"].Value.ToString();
_mongoLogic.DeleteRoom(roomId);
}
LoadData();
}
else
{
MessageBox.Show("Пожалуйста, выберите статью, информацию о которой необходимо удалить");
}
}
private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = dataGridView.Rows[e.RowIndex];
comboBoxHotel.SelectedValue = row.Cells["Hotel"].Value ?? new Hotel { HotelName = "Неизвестнo" };
textBoxCategory.Text = row.Cells["Category"].Value.ToString();
numericUpDownCountPlace.Text = row.Cells["CountPlace"].Value.ToString();
numericUpFlor.Text = row.Cells["Flor"].Value.ToString();
textBoxNumber.Text = row.Cells["Number"].Value.ToString();
numericUpDownPrice.Text = row.Cells["Price"].Value.ToString();
}
}
}
}