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

127 lines
4.4 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 FormHotel : Form
{
private readonly IHotelLogic _guestLogic;
public FormHotel(IHotelLogic guestLogic)
{
InitializeComponent();
_guestLogic = guestLogic;
}
private void FormHotel_Load(object sender, EventArgs e)
{
LoadData();
}
private void ButtonCreate_Click(object sender, EventArgs e)
{
Hotel newHotel = new()
{
HotelName = textBoxName.Text,
Address = textBoxAddress.Text,
CountStar = (int)numericUpDownCountStar.Value,
CountRoom = (int)numericUpDownCountRoom.Value,
};
_guestLogic.Create(newHotel);
LoadData();
}
private void LoadData()
{
var guests = _guestLogic.GetAll();
dataGridView.Rows.Clear();
if (dataGridView.ColumnCount == 0)
{
dataGridView.Columns.Add("Id", "ID");
dataGridView.Columns.Add("HotelName", "Название");
dataGridView.Columns.Add("Address", "Адрес");
dataGridView.Columns.Add("CountStar", "Количество звезд");
dataGridView.Columns.Add("CountRoom", "Количество комнат");
}
dataGridView.Columns["Id"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["HotelName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Address"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["CountStar"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["CountRoom"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
foreach (var guest in guests)
{
dataGridView.Rows.Add(guest.Id, guest.HotelName, guest.Address, guest.CountStar, guest.CountRoom);
}
}
private void ButtonUpdate_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
int guestId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
Hotel updatedHotel = new()
{
Id = guestId,
HotelName = textBoxName.Text,
Address = textBoxAddress.Text,
CountStar = (int)numericUpDownCountStar.Value,
CountRoom = (int)numericUpDownCountRoom.Value,
};
_guestLogic.Update(updatedHotel);
LoadData();
}
else
{
MessageBox.Show("Пожалуйста, выберите гостинницу, информацию о которой необходимо обновить");
}
}
private void ButtonDelete_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
int guestId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
_guestLogic.Delete(guestId);
LoadData();
}
else
{
MessageBox.Show("Пожалуйста, выберите гостинницу, информацию о которой необходимо удалить");
}
}
private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = dataGridView.Rows[e.RowIndex];
textBoxName.Text = row.Cells["HotelName"].Value.ToString();
textBoxAddress.Text = row.Cells["Address"].Value.ToString();
numericUpDownCountStar.Text = row.Cells["CountStar"].Value.ToString();
numericUpDownCountRoom.Text = row.Cells["CountRoom"].Value.ToString();
}
}
}
}