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

201 lines
7.7 KiB
C#
Raw Permalink 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 FormRoom : Form
{
private readonly IRoomLogic _roomLogic;
private readonly IHotelLogic _hotelLogic;
2024-05-30 13:12:02 +04:00
private readonly StorageModel _mongoLogic;
public FormRoom(IRoomLogic roomLogic, IHotelLogic hotelLogic, StorageModel mongoLogic)
2024-05-29 14:28:53 +04:00
{
InitializeComponent();
_roomLogic = roomLogic;
_hotelLogic = hotelLogic;
2024-05-30 13:12:02 +04:00
_mongoLogic = mongoLogic;
2024-05-29 14:28:53 +04:00
}
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,
};
2024-05-30 13:12:02 +04:00
if (Program.isPostgreSQL)
_roomLogic.Create(newRoom);
else
_mongoLogic.AddRoom(newRoom);
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("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;
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 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";
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 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);
}
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 roomId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
_roomLogic.Delete(roomId);
}
else
{
string? roomId = selectedRow.Cells["Id"].Value.ToString();
2024-05-29 14:28:53 +04:00
2024-05-30 13:12:02 +04:00
_mongoLogic.DeleteRoom(roomId);
}
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];
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();
}
}
}
}