121 lines
4.7 KiB
C#
Raw Normal View History

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)
{
2024-04-22 11:52:50 +04:00
InitializeComponent();
_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();
2024-04-22 11:52:50 +04:00
textBoxPassword.Text = view.Password.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),
2024-04-22 11:52:50 +04:00
Password = textBoxPassword.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 = "Заполните ФИО";
}
2024-04-22 11:52:50 +04:00
else if (string.IsNullOrEmpty(textBoxPassword.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;
}
}
}