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

220 lines
8.9 KiB
C#
Raw Permalink 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 FormServiceCheck : Form
{
private readonly IServiceCheckLogic _serviceCheckLogic;
private readonly IReservationLogic _reservationLogic;
private readonly IServiceLogic _serviceLogic;
2024-05-30 13:12:02 +04:00
private readonly StorageModel _mongoLogic;
public FormServiceCheck(IServiceCheckLogic serviceCheckLogic, IReservationLogic reservationLogic, IServiceLogic serviceLogic, StorageModel mongoLogic)
2024-05-29 14:28:53 +04:00
{
InitializeComponent();
_serviceCheckLogic = serviceCheckLogic;
_reservationLogic = reservationLogic;
_serviceLogic = serviceLogic;
2024-05-30 13:12:02 +04:00
_mongoLogic = mongoLogic;
2024-05-29 14:28:53 +04:00
}
private void FormServiceCheck_Load(object sender, EventArgs e)
{
LoadData();
}
private void ButtonCreate_Click(object sender, EventArgs e)
{
ServiceCheck newServiceCheck = new()
{
ReservationId = ((Reservation?)comboBoxReservation.SelectedItem)?.Id ?? 0,
ServiceId = ((Service?)comboBoxService.SelectedItem)?.Id ?? 0,
Count = (int)numericUpDownCount.Value,
DateTime = dateTimePicker.Value,
};
_serviceCheckLogic.Create(newServiceCheck);
LoadData();
}
private void LoadData()
{
dataGridView.Rows.Clear();
if (dataGridView.ColumnCount == 0)
{
dataGridView.Columns.Add("Id", "ID");
dataGridView.Columns.Add("ReservationId", "ReservationId");
dataGridView.Columns["ReservationId"].Visible = false;
dataGridView.Columns.Add("Reservation", "Бронирование");
dataGridView.Columns.Add("ServiceId", "ServiceId");
dataGridView.Columns["ServiceId"].Visible = false;
dataGridView.Columns.Add("Service", "Услуга");
dataGridView.Columns.Add("Count", "Количество");
dataGridView.Columns.Add("DateTime", "Дата время");
}
dataGridView.Columns["Id"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Reservation"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Service"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Count"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["DateTime"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
comboBoxReservation.DataSource = _reservationLogic.GetAll();
comboBoxReservation.DisplayMember = "Reservation";
comboBoxReservation.ValueMember = "Id";
comboBoxService.DataSource = _serviceLogic.GetAll();
comboBoxService.DisplayMember = "Service";
comboBoxService.ValueMember = "Name";
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
var serviceChecks = _serviceCheckLogic.GetAll();
foreach (var serviceCheck in serviceChecks)
{
dataGridView.Rows.Add(serviceCheck.Id,
serviceCheck.ReservationId,
_reservationLogic.Get(serviceCheck.ReservationId)?.Id,
serviceCheck.ServiceId,
_serviceLogic.Get(serviceCheck.ServiceId)?.Name,
serviceCheck.Count,
serviceCheck.DateTime);
}
comboBoxReservation.DataSource = _reservationLogic.GetAll();
comboBoxReservation.DisplayMember = "Reservation";
comboBoxReservation.ValueMember = "Id";
comboBoxService.DataSource = _serviceLogic.GetAll();
comboBoxService.DisplayMember = "Service";
comboBoxService.ValueMember = "Name";
}
else
{
var serviceChecks = _mongoLogic.GetServiceChecks();
foreach (var serviceCheck in serviceChecks)
{
dataGridView.Rows.Add(
serviceCheck.Id,
serviceCheck.ReservationId,
_mongoLogic.GetReservationById(serviceCheck.ReservationId)?.Id,
serviceCheck.ServiceId,
_mongoLogic.GetServiceById(serviceCheck.ServiceId)?.Name,
serviceCheck.Count,
serviceCheck.DateTime);
}
comboBoxReservation.DataSource = _mongoLogic.GetReservations();
comboBoxReservation.DisplayMember = "Reservation";
comboBoxReservation.ValueMember = "Id";
comboBoxService.DataSource = _mongoLogic.GetServices();
comboBoxService.DisplayMember = "Service";
comboBoxService.ValueMember = "Name";
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 serviceCheckId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
ServiceCheck updatedServiceCheck = new()
{
Id = serviceCheckId,
ReservationId = ((Reservation?)comboBoxReservation.SelectedItem)?.Id ?? 0,
ServiceId = ((Service?)comboBoxService.SelectedItem)?.Id ?? 0,
Count = (int)numericUpDownCount.Value,
DateTime = dateTimePicker.Value,
};
_serviceCheckLogic.Update(updatedServiceCheck);
}
else
{
string? serviceCheckId = selectedRow.Cells["Id"].Value.ToString();
MongoServiceCheck updatedServiceCheck = new()
{
Id = serviceCheckId,
ReservationId = _mongoLogic.GetMongoId("Reservation", ((Reservation?)comboBoxReservation.SelectedItem)?.Id.ToString() ?? String.Empty),
ServiceId = _mongoLogic.GetMongoId("Service", ((Service?)comboBoxService.SelectedItem)?.Id.ToString() ?? String.Empty),
Count = numericUpDownCount.Value.ToString(),
DateTime = dateTimePicker.Value.ToString(),
};
2024-05-29 14:28:53 +04:00
2024-05-30 13:12:02 +04:00
_mongoLogic.UpdateServiceCheck(updatedServiceCheck);
}
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 serviceCheckId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
_serviceCheckLogic.Delete(serviceCheckId);
}
else
{
string? serviceCheckId = selectedRow.Cells["Id"].Value.ToString();
2024-05-29 14:28:53 +04:00
2024-05-30 13:12:02 +04:00
_mongoLogic.DeleteServiceCheck(serviceCheckId);
}
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];
comboBoxReservation.SelectedValue = row.Cells["Reservation"].Value ?? new Reservation { Id = 0 };
comboBoxService.SelectedValue = row.Cells["Service"].Value ?? new Service { Name = "Неизвестно" };
numericUpDownCount.Text = row.Cells["Count"].Value.ToString();
dateTimePicker.Text = row.Cells["DateTime"].Value.ToString();
}
}
public FormServiceCheck()
{
InitializeComponent();
}
}
}