2024-11-17 05:41:07 +04:00

84 lines
2.9 KiB
C#

using ProjectPolyclinic.Entities;
using ProjectPolyclinic.Entities.Enums;
using ProjectPolyclinic.Repositories;
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 ProjectPolyclinic.Forms
{
public partial class FormDoctor : Form
{
private readonly IDoctorRepository _doctorRepository;
private int? _doctorId;
public int Id
{
set
{
try
{
var doctor = _doctorRepository.ReadDoctorById(value);
if (doctor == null)
{
throw new InvalidDataException(nameof(doctor));
}
textBoxNameDoctor.Text = doctor.First_Name;
textBoxName2Doctor.Text = doctor.Last_Name;
comboBoxSpec.SelectedItem = doctor.Specialization;
_doctorId = value;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при полученииданных", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
}
public FormDoctor(IDoctorRepository doctorRepository)
{
InitializeComponent();
_doctorRepository = doctorRepository ?? throw new ArgumentNullException(nameof(doctorRepository));
comboBoxSpec.DataSource = Enum.GetValues(typeof(Specialization));
}
private void ButtonSave_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrWhiteSpace(textBoxNameDoctor.Text) ||
string.IsNullOrWhiteSpace(textBoxName2Doctor.Text)
||
comboBoxSpec.SelectedIndex < 1)
{
throw new Exception("Имеются незаполненныеполя");
}
if (_doctorId.HasValue)
{
_doctorRepository.UpdateDoctor(CreateDoctor(_doctorId.Value));
}
else
{
_doctorRepository.CreateDoctor(CreateDoctor(0));
}
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при сохранении",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
private Doctor CreateDoctor(int id) => Doctor.CreateEntity(id, textBoxNameDoctor.Text, textBoxName2Doctor.Text,
(Specialization)comboBoxSpec.SelectedItem!);
}
}