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

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