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

117 lines
3.6 KiB
C#
Raw 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 FormService : Form
{
private readonly IServiceLogic _serviceLogic;
public FormService(IServiceLogic serviceLogic)
{
InitializeComponent();
_serviceLogic = serviceLogic;
}
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,
};
_serviceLogic.Create(newService);
LoadData();
}
private void LoadData()
{
var services = _serviceLogic.GetAll();
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;
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];
int serviceId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
Service updatedService = new()
{
Id = serviceId,
Name = textBoxName.Text,
Price = (double)numericUpDownPrice.Value
};
_serviceLogic.Update(updatedService);
LoadData();
}
else
{
MessageBox.Show("Пожалуйста, выберите гостинницу, информацию о которой необходимо обновить");
}
}
private void ButtonDelete_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
int serviceId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
_serviceLogic.Delete(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();
}
}
}
}