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

192 lines
6.9 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;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace HotelView
{
public partial class FormGuest : Form
{
private readonly IGuestLogic _guestLogic;
2024-05-30 13:12:02 +04:00
private readonly StorageModel _mongoLogic;
public FormGuest(IGuestLogic guestLogic, StorageModel mongoLogic)
2024-05-29 14:28:53 +04:00
{
InitializeComponent();
_guestLogic = guestLogic;
2024-05-30 13:12:02 +04:00
_mongoLogic = mongoLogic;
2024-05-29 14:28:53 +04:00
}
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())
};
2024-05-30 13:12:02 +04:00
if (Program.isPostgreSQL)
_guestLogic.Create(newGuest);
else
_mongoLogic.AddGuest(newGuest);
2024-05-29 14:28:53 +04:00
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;
2024-05-30 13:12:02 +04:00
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
2024-05-29 14:28:53 +04:00
{
2024-05-30 13:12:02 +04:00
var guests = _mongoLogic.GetGuests();
foreach (var guest in guests)
{
dataGridView.Rows.Add(guest.Id, guest.FIO, guest.PhoneNumber, guest.BirthDate, guest.PassportId, guest.Gender);
}
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 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);
}
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 guestId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
_guestLogic.Delete(guestId);
} else
{
string? guestId = selectedRow?.Cells["Id"].Value.ToString();
2024-05-29 14:28:53 +04:00
2024-05-30 13:12:02 +04:00
_mongoLogic.DeleteGuest(guestId);
}
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["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)
{
}
}
}