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

172 lines
6.0 KiB
C#
Raw 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 FormHotel : Form
{
2024-05-30 13:12:02 +04:00
private readonly IHotelLogic _hotelLogic;
private readonly StorageModel _mongoLogic;
public FormHotel(IHotelLogic hotelLogic, StorageModel mongoLogic)
2024-05-29 14:28:53 +04:00
{
InitializeComponent();
2024-05-30 13:12:02 +04:00
_hotelLogic = hotelLogic;
_mongoLogic = mongoLogic;
2024-05-29 14:28:53 +04:00
}
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,
};
2024-05-30 13:12:02 +04:00
if (Program.isPostgreSQL)
_hotelLogic.Create(newHotel);
else
_mongoLogic.AddHotel(newHotel);
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("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;
2024-05-30 13:12:02 +04:00
if (Program.isPostgreSQL)
{
var hotels = _hotelLogic.GetAll();
foreach (var hotel in hotels)
{
dataGridView.Rows.Add(hotel.Id, hotel.HotelName, hotel.Address, hotel.CountStar, hotel.CountRoom);
}
}
else
2024-05-29 14:28:53 +04:00
{
2024-05-30 13:12:02 +04:00
var hotels = _mongoLogic.GetHotels();
foreach (var hotel in hotels)
{
dataGridView.Rows.Add(hotel.Id, hotel.HotelName, hotel.Address, hotel.CountStar, hotel.CountRoom);
}
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 hotelId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
Hotel updatedHotel = new()
{
Id = hotelId,
HotelName = textBoxName.Text,
Address = textBoxAddress.Text,
CountStar = (int)numericUpDownCountStar.Value,
CountRoom = (int)numericUpDownCountRoom.Value,
};
_hotelLogic.Update(updatedHotel);
}
else
{
string? hotelId = selectedRow.Cells["Id"].Value.ToString();
MongoHotel updatedHotel = new()
{
Id = hotelId,
HotelName = textBoxName.Text,
Address = textBoxAddress.Text,
CountStar = numericUpDownCountStar.Value.ToString(),
CountRoom = numericUpDownCountRoom.Value.ToString(),
};
2024-05-29 14:28:53 +04:00
2024-05-30 13:12:02 +04:00
_mongoLogic.UpdateHotel(updatedHotel);
}
2024-05-29 14:28:53 +04:00
LoadData();
}
else
{
2024-05-30 13:12:02 +04:00
MessageBox.Show("Пожалуйста, выберите hotel, информацию о котором необходимо обновить");
2024-05-29 14:28:53 +04:00
}
}
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 hotelId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
_hotelLogic.Delete(hotelId);
}
else
{
string? hotelId = selectedRow.Cells["Id"].Value.ToString();
2024-05-29 14:28:53 +04:00
2024-05-30 13:12:02 +04:00
_mongoLogic.DeleteHotel(hotelId);
}
2024-05-29 14:28:53 +04:00
LoadData();
}
else
{
2024-05-30 13:12:02 +04:00
MessageBox.Show("Пожалуйста, выберите hotel, информацию о котором необходимо удалить");
2024-05-29 14:28:53 +04:00
}
}
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();
}
}
}
}