using BeautySaloonContracts.BindingModels; using BeautySaloonContracts.BusinessLogicsContracts; using BeautySaloonContracts.SearchModels; using Microsoft.IdentityModel.Tokens; namespace BeautySaloonView { public partial class FormEmployee : Form { private readonly IEmployeeLogic _logicE; private readonly IPositionLogic _logicP; private int? _id; public int Id { set { _id = value; } } public FormEmployee(IEmployeeLogic logicE, IPositionLogic logicP) { InitializeComponent(); _logicE = logicE; _logicP = logicP; } private void FormEmployee_Load(object sender, EventArgs e) { if (_id.HasValue) { try { var view = _logicE.ReadElement(new EmployeeSearchModel { Id = _id.Value }); var list = _logicP.ReadList(null); if (list != null) { comboBox.DisplayMember = "Name"; comboBox.ValueMember = "Id"; comboBox.DataSource = list; comboBox.SelectedItem = null; } if (view != null) { textBoxName.Text = view.Name; textBoxSurname.Text = view.Surname; textBoxPatronymic.Text = view.Patronymic; maskedTextBox.Text = view.Phone; comboBox.SelectedValue = view.PositionId; } } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { try { var list = _logicP.ReadList(null); if (list != null) { comboBox.DisplayMember = "Name"; comboBox.ValueMember = "Id"; comboBox.DataSource = list; comboBox.SelectedItem = null; } } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void ButtonSave_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBoxName.Text)) { MessageBox.Show("Заполните имя", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrEmpty(textBoxSurname.Text)) { MessageBox.Show("Заполните фамилию", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (!maskedTextBox.MaskFull) { MessageBox.Show("Заполните номер телефона", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (comboBox.SelectedValue == null) { MessageBox.Show("Выберите должность", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { var model = new EmployeeBindingModel { Id = _id ?? 0, Name = textBoxName.Text, Surname = textBoxSurname.Text, Patronymic = textBoxPatronymic.Text, Phone = maskedTextBox.Text, PositionId = Convert.ToInt32(comboBox.SelectedValue) }; var operationResult = _id.HasValue ? _logicE.Update(model) : _logicE.Create(model); if (!operationResult) { throw new Exception("Ошибка при сохранении."); } MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); DialogResult = DialogResult.OK; Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void ButtonCancel_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; Close(); } } }