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

147 lines
5.6 KiB
C#
Raw Permalink 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 FormRoom : Form
{
private readonly IRoomLogic _roomLogic;
private readonly IHotelLogic _hotelLogic;
public FormRoom(IRoomLogic roomLogic, IHotelLogic hotelLogic)
{
InitializeComponent();
_roomLogic = roomLogic;
_hotelLogic = hotelLogic;
}
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,
};
_roomLogic.Create(newRoom);
LoadData();
}
private void LoadData()
{
var rooms = _roomLogic.GetAll();
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;
comboBoxHotel.DataSource = _hotelLogic.GetAll();
comboBoxHotel.DisplayMember = "Hotel";
comboBoxHotel.ValueMember = "HotelName";
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);
}
}
private void ButtonUpdate_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
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);
LoadData();
}
else
{
MessageBox.Show("Пожалуйста, выберите статью, информацию о которой необходимо обновить");
}
}
private void ButtonDelete_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
int roomId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
_roomLogic.Delete(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();
}
}
}
}