2024-11-09 03:06:08 +04:00
|
|
|
|
using Microsoft.VisualBasic.FileIO;
|
|
|
|
|
using ProjectGSM.Entities;
|
|
|
|
|
using ProjectGSM.Entities.Enums;
|
2024-11-05 03:59:13 +04:00
|
|
|
|
using ProjectGSM.Repositories;
|
|
|
|
|
|
|
|
|
|
namespace ProjectGSM.Forms
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public partial class FormAdvocate : Form
|
|
|
|
|
{
|
|
|
|
|
private readonly IAdvocateRepository _advocateRepository;
|
|
|
|
|
|
|
|
|
|
private int? _advocateId;
|
|
|
|
|
|
|
|
|
|
public int Id
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var advocate =
|
|
|
|
|
_advocateRepository.ReadAdvocateById(value);
|
|
|
|
|
if (advocate == null)
|
|
|
|
|
{
|
|
|
|
|
throw new
|
|
|
|
|
InvalidDataException(nameof(advocate));
|
|
|
|
|
}
|
2024-11-18 23:50:05 +04:00
|
|
|
|
|
|
|
|
|
_advocateId = value;
|
2024-11-09 03:06:08 +04:00
|
|
|
|
foreach (LicenseType elem in Enum.GetValues(typeof(LicenseType)))
|
|
|
|
|
{
|
|
|
|
|
if ((elem & advocate.LicenseType) != 0)
|
|
|
|
|
{
|
|
|
|
|
checkedListBox.SetItemChecked(checkedListBox.Items.IndexOf(
|
|
|
|
|
elem), true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-05 03:59:13 +04:00
|
|
|
|
nameTextBox.Text = advocate.Name;
|
|
|
|
|
sexCheckBox.Checked= advocate.Sex;
|
|
|
|
|
dateTimePicker.Value = new DateTime(advocate.DateOfBirth.Year, advocate.DateOfBirth.Month, advocate.DateOfBirth.Day);
|
|
|
|
|
expNumeric.Value = advocate.Experience;
|
|
|
|
|
tasksNumeric.Value = advocate.CompletedTasks;
|
|
|
|
|
ratingNumeric.Value = advocate.Rating;
|
|
|
|
|
emailTextBox.Text = advocate.Email;
|
|
|
|
|
phoneText.Text = advocate.PhoneNumber;
|
|
|
|
|
adressBox.Text = advocate.Address;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public FormAdvocate(IAdvocateRepository advocateRepository)
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
_advocateRepository = advocateRepository ??
|
|
|
|
|
throw new
|
|
|
|
|
ArgumentNullException(nameof(advocateRepository));
|
2024-11-09 03:06:08 +04:00
|
|
|
|
|
|
|
|
|
foreach (var elem in Enum.GetValues(typeof(LicenseType)))
|
|
|
|
|
{
|
|
|
|
|
checkedListBox.Items.Add(elem);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-05 03:59:13 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void saveButton_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(nameTextBox.Text) ||
|
|
|
|
|
string.IsNullOrWhiteSpace(emailTextBox.Text) ||
|
|
|
|
|
string.IsNullOrWhiteSpace(phoneText.Text) ||
|
2024-11-09 03:06:08 +04:00
|
|
|
|
string.IsNullOrWhiteSpace(adressBox.Text) || checkedListBox.CheckedItems.Count == 0)
|
2024-11-05 03:59:13 +04:00
|
|
|
|
{
|
|
|
|
|
throw new Exception("Имеются незаполненные поля");
|
|
|
|
|
}
|
|
|
|
|
if (_advocateId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
_advocateRepository.UpdateAdvocate(CreateAdvocate(_advocateId.Value));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-11-18 23:50:05 +04:00
|
|
|
|
_advocateRepository.CreateAdvocate(CreateAdvocate(0));
|
2024-11-05 03:59:13 +04:00
|
|
|
|
}
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка при сохранении",
|
|
|
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void cancelButton_Click(object sender, EventArgs e) => Close();
|
|
|
|
|
|
2024-11-09 03:06:08 +04:00
|
|
|
|
private Advocate CreateAdvocate(int id)
|
|
|
|
|
{
|
|
|
|
|
LicenseType licenseType = LicenseType.None;
|
|
|
|
|
foreach (var elem in checkedListBox.CheckedItems)
|
|
|
|
|
{
|
|
|
|
|
licenseType |= (LicenseType)elem;
|
|
|
|
|
}
|
|
|
|
|
return Advocate.CreateEntity(id,
|
2024-11-05 03:59:13 +04:00
|
|
|
|
nameTextBox.Text,
|
|
|
|
|
sexCheckBox.Checked,
|
|
|
|
|
dateTimePicker.Value,
|
|
|
|
|
Convert.ToInt32(expNumeric.Value),
|
|
|
|
|
Convert.ToInt32(tasksNumeric.Value),
|
|
|
|
|
Convert.ToInt32(ratingNumeric.Value),
|
|
|
|
|
emailTextBox.Text,
|
|
|
|
|
phoneText.Text,
|
2024-11-09 03:06:08 +04:00
|
|
|
|
adressBox.Text, licenseType);
|
|
|
|
|
}
|
2024-11-05 03:59:13 +04:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|