SUBD/PersonnelDepartment/PersonnelDepartmentView/FormEmployee.cs

96 lines
2.6 KiB
C#

using PersonnelDepartmentBusinessLogic.BusinessLogics;
using PersonnelDepartmentContracts.BindingModels;
using PersonnelDepartmentContracts.BusinessLogicContracts;
using PersonnelDepartmentContracts.SearchModels;
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 PersonnelDepartmentView
{
public partial class FormEmployee : Form
{
private readonly IEmployeeLogic _employeeLogic;
private int? _id;
public int Id { set { _id = value; } }
public FormEmployee(IEmployeeLogic employeeLogic)
{
InitializeComponent();
_employeeLogic = employeeLogic;
}
private void FormEmployee_Load(object sender, EventArgs e)
{
if (_id.HasValue)
{
try
{
var view = _employeeLogic.ReadElement(new EmployeeSearchModel
{
Id = _id.Value
});
if (view != null)
{
textBoxFirstName.Text = view.FirstName;
textBoxLastName.Text = view.LastName;
textBoxPatronymic.Text = view.Patronymic;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxFirstName.Text))
{
MessageBox.Show("Введите название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(textBoxLastName.Text))
{
MessageBox.Show("Введите контактный номер", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
var model = new EmployeeBindingModel
{
Id = _id ?? 0,
FirstName = textBoxFirstName.Text,
LastName = textBoxLastName.Text,
Patronymic = textBoxPatronymic.Text
};
var operationResult = _id.HasValue ? _employeeLogic.Update(model) : _employeeLogic.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();
}
}
}