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 FormServiceCheck : Form { private readonly IServiceCheckLogic _serviceCheckLogic; private readonly IReservationLogic _reservationLogic; private readonly IServiceLogic _serviceLogic; private readonly StorageModel _mongoLogic; public FormServiceCheck(IServiceCheckLogic serviceCheckLogic, IReservationLogic reservationLogic, IServiceLogic serviceLogic, StorageModel mongoLogic) { InitializeComponent(); _serviceCheckLogic = serviceCheckLogic; _reservationLogic = reservationLogic; _serviceLogic = serviceLogic; _mongoLogic = mongoLogic; } 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"; if (Program.isPostgreSQL) { 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"; } } private void ButtonUpdate_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count > 0) { DataGridViewRow selectedRow = dataGridView.SelectedRows[0]; if (Program.isPostgreSQL) { 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(), }; _mongoLogic.UpdateServiceCheck(updatedServiceCheck); } 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 serviceCheckId = Convert.ToInt32(selectedRow.Cells["Id"].Value); _serviceCheckLogic.Delete(serviceCheckId); } else { string? serviceCheckId = selectedRow.Cells["Id"].Value.ToString(); _mongoLogic.DeleteServiceCheck(serviceCheckId); } 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(); } } }