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

145 lines
5.2 KiB
C#
Raw Permalink 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;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace HotelView
{
public partial class FormGuest : Form
{
private readonly IGuestLogic _guestLogic;
public FormGuest(IGuestLogic guestLogic)
{
InitializeComponent();
_guestLogic = guestLogic;
}
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())
};
_guestLogic.Create(newGuest);
LoadData();
}
private void LoadData()
{
comboBox1.DataSource = Enum.GetValues(typeof(GenderEnum));
var guests = _guestLogic.GetAll();
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;
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];
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);
LoadData();
}
else
{
MessageBox.Show("Пожалуйста, выберите гостинницу, информацию о которой необходимо обновить");
}
}
private void ButtonDelete_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
int guestId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
_guestLogic.Delete(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)
{
}
}
}