115 lines
4.4 KiB
C#
115 lines
4.4 KiB
C#
using Microsoft.Extensions.Logging;
|
||
using Microsoft.IdentityModel.Tokens;
|
||
using SecuritySystemContracts.BindingModels;
|
||
using SecuritySystemContracts.BusinessLogicsContracts;
|
||
using SecuritySystemContracts.SearchModels;
|
||
|
||
namespace SecuritySystemView.Implementer
|
||
{
|
||
public partial class FormImplementer : Form
|
||
{
|
||
private readonly ILogger _logger;
|
||
private readonly IImplementerLogic _logic;
|
||
private int? _id;
|
||
public int Id { set { _id = value; } }
|
||
public FormImplementer(ILogger<FormImplementer> logger, IImplementerLogic logic)
|
||
{
|
||
InitializeImplementer();
|
||
_logger = logger;
|
||
_logic = logic;
|
||
}
|
||
private void Form_Load(object sender, EventArgs e)
|
||
{
|
||
if (_id.HasValue)
|
||
{
|
||
try
|
||
{
|
||
_logger.LogInformation("Получение исполнителя");
|
||
var view = _logic.ReadElement(new ImplementerSearchModel { Id = _id.Value });
|
||
if (view != null)
|
||
{
|
||
textBoxFIO.Text = view.ImplementerFIO;
|
||
textBoxQualification.Text = view.Qualification.ToString();
|
||
textBoxWorkExp.Text = view.WorkExperience.ToString();
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "Ошибка получения исполнителя");
|
||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
|
||
MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
private void ButtonSave_Click(object sender, EventArgs e)
|
||
{
|
||
if (!CheckFieldsIsValid())
|
||
{
|
||
return;
|
||
}
|
||
_logger.LogInformation("Сохранение исполнителя");
|
||
try
|
||
{
|
||
var model = new ImplementerBindingModel
|
||
{
|
||
Id = _id ?? 0,
|
||
ImplementerFIO = textBoxFIO.Text,
|
||
WorkExperience = Convert.ToInt32(textBoxWorkExp.Text),
|
||
Qualification = Convert.ToInt32(textBoxQualification.Text),
|
||
};
|
||
var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model);
|
||
if (!operationResult)
|
||
{
|
||
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
|
||
}
|
||
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
DialogResult = DialogResult.OK;
|
||
Close();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "Ошибка сохранения исполнителя");
|
||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
private void ButtonCancel_Click(object sender, EventArgs e)
|
||
{
|
||
DialogResult = DialogResult.Cancel;
|
||
Close();
|
||
}
|
||
|
||
private bool CheckFieldsIsValid()
|
||
{
|
||
string errMsg = string.Empty;
|
||
if (string.IsNullOrEmpty(textBoxFIO.Text))
|
||
{
|
||
errMsg = "Заполните ФИО";
|
||
}
|
||
else if (string.IsNullOrEmpty(textBoxQualification.Text))
|
||
{
|
||
errMsg = "Заполните квалификацию";
|
||
}
|
||
else if (!int.TryParse(textBoxQualification.Text, out _))
|
||
{
|
||
errMsg = "В поле квалификации должны быть только цифры";
|
||
}
|
||
else if (!int.TryParse(textBoxWorkExp.Text, out _))
|
||
{
|
||
errMsg = "Заполните квалификацию";
|
||
}
|
||
else if (string.IsNullOrEmpty(textBoxWorkExp.Text))
|
||
{
|
||
errMsg = "В поле опыта работы должны быть только цифры";
|
||
}
|
||
|
||
if (!errMsg.IsNullOrEmpty())
|
||
{
|
||
MessageBox.Show(errMsg, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
}
|
||
}
|