80 lines
2.7 KiB
C#
80 lines
2.7 KiB
C#
using ProjectPolyclinic.Entities;
|
|
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 FormPatient : Form
|
|
{
|
|
private readonly IPatientRepository _patientRepository;
|
|
private int? _patientId;
|
|
public int Id
|
|
{
|
|
set
|
|
{
|
|
try
|
|
{
|
|
var patient = _patientRepository.ReadPatientById(value);
|
|
if (patient == null)
|
|
{
|
|
throw new InvalidDataException(nameof(patient));
|
|
|
|
}
|
|
textBoxNamePatient.Text = patient.First_Name;
|
|
textBoxName2Patient.Text = patient.Last_Name;
|
|
textBoxNumberPatient.Text = patient.ContactNumber;
|
|
_patientId = value;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при полученииданных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
|
|
}
|
|
}
|
|
public FormPatient(IPatientRepository patientRepository)
|
|
{
|
|
InitializeComponent();
|
|
_patientRepository = patientRepository ?? throw new ArgumentNullException(nameof(patientRepository));
|
|
}
|
|
|
|
private void ButtonSave_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrWhiteSpace(textBoxNamePatient.Text) || string.IsNullOrWhiteSpace(textBoxName2Patient.Text) || string.IsNullOrWhiteSpace(textBoxNumberPatient.Text))
|
|
{
|
|
throw new Exception("Имеются незаполненные поля");
|
|
}
|
|
if (_patientId.HasValue)
|
|
{
|
|
_patientRepository.UpdatePatient(CreatePatient(_patientId.Value));
|
|
|
|
}
|
|
else
|
|
{
|
|
_patientRepository.CreatePatient(CreatePatient(0));
|
|
}
|
|
Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
}
|
|
}
|
|
|
|
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
|
|
private Patient CreatePatient(int id) => Patient.CreateEntity(id, textBoxNamePatient.Text, textBoxName2Patient.Text, textBoxNumberPatient.Text);
|
|
}
|
|
}
|