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; using static System.Windows.Forms.VisualStyles.VisualStyleElement; namespace HotelView { public partial class FormGuest : Form { private readonly IGuestLogic _guestLogic; private readonly StorageModel _mongoLogic; public FormGuest(IGuestLogic guestLogic, StorageModel mongoLogic) { InitializeComponent(); _guestLogic = guestLogic; _mongoLogic = mongoLogic; } private void FormGuest_Load(object sender, EventArgs e) { LoadData(); } private void ButtonCreate_Click(object sender, EventArgs e) { Guest newGuest = new() { FIO = textBoxName.Text, PhoneNumber = textBoxPhone.Text, BirthDate = ToDateOnly(dateTimePicker1.Value), PassportId = textBoxPassport.Text, Gender = (GenderEnum)Enum.Parse(typeof(GenderEnum), comboBox1.SelectedItem.ToString()) }; if (Program.isPostgreSQL) _guestLogic.Create(newGuest); else _mongoLogic.AddGuest(newGuest); LoadData(); } private void LoadData() { comboBox1.DataSource = Enum.GetValues(typeof(GenderEnum)); dataGridView.Rows.Clear(); if (dataGridView.ColumnCount == 0) { dataGridView.Columns.Add("Id", "ID"); dataGridView.Columns.Add("FIO", "ФИО"); dataGridView.Columns.Add("PhoneNumber", "Номер телефона"); dataGridView.Columns.Add("BirthDate", "Дата рождения"); dataGridView.Columns.Add("PassportId", "Паспорт"); dataGridView.Columns.Add("Gender", "Пол"); } dataGridView.Columns["Id"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView.Columns["FIO"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView.Columns["PhoneNumber"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView.Columns["BirthDate"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView.Columns["PassportId"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView.Columns["Gender"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; if (Program.isPostgreSQL) { var guests = _guestLogic.GetAll(); foreach (var guest in guests) { dataGridView.Rows.Add(guest.Id, guest.FIO, guest.PhoneNumber, guest.BirthDate, guest.PassportId, guest.Gender); } } else { var guests = _mongoLogic.GetGuests(); foreach (var guest in guests) { dataGridView.Rows.Add(guest.Id, guest.FIO, guest.PhoneNumber, guest.BirthDate, guest.PassportId, guest.Gender); } } } private void ButtonUpdate_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count > 0) { DataGridViewRow selectedRow = dataGridView.SelectedRows[0]; if (Program.isPostgreSQL) { int guestId = Convert.ToInt32(selectedRow.Cells["Id"].Value); Guest updatedGuest = new() { Id = guestId, FIO = textBoxName.Text, PhoneNumber = textBoxPhone.Text, BirthDate = ToDateOnly(dateTimePicker1.Value), PassportId = textBoxPassport.Text, Gender = (GenderEnum)Enum.Parse(typeof(GenderEnum), comboBox1.SelectedItem.ToString()) }; _guestLogic.Update(updatedGuest); } else { string? guestId = selectedRow.Cells["Id"].Value.ToString(); MongoGuest updatedGuest = new() { Id = guestId, FIO = textBoxName.Text, PhoneNumber = textBoxPhone.Text, BirthDate = dateTimePicker1.Value.ToString("d"), PassportId = textBoxPassport.Text, Gender = comboBox1.SelectedItem.ToString() }; _mongoLogic.UpdateGuest(updatedGuest); } 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 guestId = Convert.ToInt32(selectedRow.Cells["Id"].Value); _guestLogic.Delete(guestId); } else { string? guestId = selectedRow?.Cells["Id"].Value.ToString(); _mongoLogic.DeleteGuest(guestId); } 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["FIO"].Value.ToString(); textBoxPhone.Text = row.Cells["PhoneNumber"].Value.ToString(); dateTimePicker1.Text = row.Cells["BirthDate"].Value.ToString(); textBoxPassport.Text = row.Cells["PassportId"].Value.ToString(); comboBox1.Text = row.Cells["Gender"].ToString(); } } public DateOnly ToDateOnly(DateTime dateTime) { var DateTime = dateTime; var Date = new DateOnly(DateTime.Year, DateTime.Month, DateTime.Day); return Date; } private void label3_Click(object sender, EventArgs e) { } } }