100 lines
3.4 KiB
C#
100 lines
3.4 KiB
C#
using ProjectOptika.Scripts.Entities.Enums;
|
|
using ProjectOptika.Scripts.Entities;
|
|
using ProjectOptika.Scripts.Repositories;
|
|
|
|
namespace ProjectOptika.Scripts.Forms
|
|
{
|
|
public partial class FormEmployee : Form
|
|
{
|
|
private readonly IEmployeeRepository _employeeRepositories;
|
|
|
|
private int? _employeeID;
|
|
|
|
public int ID
|
|
{
|
|
set
|
|
{
|
|
try
|
|
{
|
|
var employee = _employeeRepositories.GetEmployeerByID(value);
|
|
|
|
if (employee == null) throw new InvalidDataException(nameof(employee));
|
|
|
|
_employeeID = employee.ID;
|
|
textBoxFirstName.Text = employee.FirstName;
|
|
textBoxSecondName.Text = employee.SecondName;
|
|
textBoxSurname.Text = employee.Surname;
|
|
|
|
foreach(PositionEmployee element in Enum.GetValues(typeof(PositionEmployee)))
|
|
{
|
|
if ((element & employee.PositionEmployee) != 0)
|
|
{
|
|
checkedListBoxPositionEmployee.SetItemChecked(checkedListBoxPositionEmployee.Items.IndexOf(element), true);
|
|
}
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
public FormEmployee(IEmployeeRepository employeeRepositories)
|
|
{
|
|
InitializeComponent();
|
|
|
|
_employeeRepositories = employeeRepositories ??
|
|
throw new ArgumentNullException(nameof(employeeRepositories));
|
|
|
|
foreach (var i in Enum.GetValues(typeof(PositionEmployee)))
|
|
{
|
|
checkedListBoxPositionEmployee.Items.Add(i);
|
|
}
|
|
}
|
|
|
|
private void ButtonSave_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(textBoxSecondName.Text) || string.IsNullOrEmpty(textBoxSurname.Text) || string.IsNullOrEmpty(textBoxFirstName.Text))
|
|
{
|
|
throw new Exception("Имеются незаполненные данные");
|
|
}
|
|
|
|
if (_employeeID.HasValue)
|
|
{
|
|
_employeeRepositories.UpdateEmployee(CreateEmployee(_employeeID.Value));
|
|
}
|
|
else
|
|
{
|
|
_employeeRepositories.CreateEmployee(CreateEmployee(0));
|
|
}
|
|
Close();
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при получении сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
private void ButtonCancel_Click(object sender, EventArgs e)
|
|
{
|
|
Close();
|
|
}
|
|
|
|
private Employee CreateEmployee(int id)
|
|
{
|
|
PositionEmployee positionEmployee = PositionEmployee.None;
|
|
foreach(var element in checkedListBoxPositionEmployee.CheckedItems)
|
|
{
|
|
positionEmployee |= (PositionEmployee)element;
|
|
}
|
|
|
|
return Employee.CreateEntity(id, positionEmployee, textBoxFirstName.Text, textBoxSecondName.Text, textBoxSurname.Text);
|
|
}
|
|
}
|
|
}
|