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 FormHotel : Form { private readonly IHotelLogic _hotelLogic; private readonly StorageModel _mongoLogic; public FormHotel(IHotelLogic hotelLogic, StorageModel mongoLogic) { InitializeComponent(); _hotelLogic = hotelLogic; _mongoLogic = mongoLogic; } 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, }; if (Program.isPostgreSQL) _hotelLogic.Create(newHotel); else _mongoLogic.AddHotel(newHotel); 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; 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 { var hotels = _mongoLogic.GetHotels(); foreach (var hotel in hotels) { dataGridView.Rows.Add(hotel.Id, hotel.HotelName, hotel.Address, hotel.CountStar, hotel.CountRoom); } } } private void ButtonUpdate_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count > 0) { DataGridViewRow selectedRow = dataGridView.SelectedRows[0]; if (Program.isPostgreSQL) { 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(), }; _mongoLogic.UpdateHotel(updatedHotel); } LoadData(); } else { MessageBox.Show("Пожалуйста, выберите hotel, информацию о котором необходимо обновить"); } } private void ButtonDelete_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count > 0) { DataGridViewRow selectedRow = dataGridView.SelectedRows[0]; if (Program.isPostgreSQL) { int hotelId = Convert.ToInt32(selectedRow.Cells["Id"].Value); _hotelLogic.Delete(hotelId); } else { string? hotelId = selectedRow.Cells["Id"].Value.ToString(); _mongoLogic.DeleteHotel(hotelId); } LoadData(); } else { MessageBox.Show("Пожалуйста, выберите hotel, информацию о котором необходимо удалить"); } } 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(); } } } }