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 FormService : Form { private readonly IServiceLogic _serviceLogic; private readonly StorageModel _mongoLogic; public FormService(IServiceLogic serviceLogic, StorageModel mongoLogic) { InitializeComponent(); _serviceLogic = serviceLogic; _mongoLogic = mongoLogic; } private void FormService_Load(object sender, EventArgs e) { LoadData(); } private void ButtonCreate_Click(object sender, EventArgs e) { Service newService = new() { Name = textBoxName.Text, Price = (double)numericUpDownPrice.Value, }; if (Program.isPostgreSQL) _serviceLogic.Create(newService); else _mongoLogic.AddService(newService); LoadData(); } private void LoadData() { dataGridView.Rows.Clear(); if (dataGridView.ColumnCount == 0) { dataGridView.Columns.Add("Id", "ID"); dataGridView.Columns.Add("Name", "Название"); dataGridView.Columns.Add("Price", "Цена"); } dataGridView.Columns["Id"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView.Columns["Price"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; if (Program.isPostgreSQL) { var services = _serviceLogic.GetAll(); foreach (var service in services) { dataGridView.Rows.Add(service.Id, service.Name, service.Price); } } else { var services = _mongoLogic.GetServices(); foreach (var service in services) { dataGridView.Rows.Add(service.Id, service.Name, service.Price); } } } private void ButtonUpdate_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count > 0) { DataGridViewRow selectedRow = dataGridView.SelectedRows[0]; if (!Program.isPostgreSQL) { int serviceId = Convert.ToInt32(selectedRow.Cells["Id"].Value); Service updatedService = new() { Id = serviceId, Name = textBoxName.Text, Price = (double)numericUpDownPrice.Value }; _serviceLogic.Update(updatedService); } else { string? serviceId = selectedRow.Cells["Id"].Value.ToString(); MongoService mongoService = new() { Id = serviceId, Name = textBoxName.Text, Price = numericUpDownPrice.Value.ToString() }; _mongoLogic.UpdateService(mongoService); } LoadData(); } else { MessageBox.Show("Пожалуйста, выберите гостинницу, информацию о которой необходимо обновить"); } } private void ButtonDelete_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count > 0) { DataGridViewRow selectedRow = dataGridView.SelectedRows[0]; if (!Program.isPostgreSQL) { int serviceId = Convert.ToInt32(selectedRow.Cells["Id"].Value); _serviceLogic.Delete(serviceId); } else { string? serviceId = selectedRow.Cells["Id"].Value.ToString(); _mongoLogic.DeleteService(serviceId); } 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["Name"].Value.ToString(); numericUpDownPrice.Text = row.Cells["Price"].Value.ToString(); } } } }