forked from DavidMakarov/StudentEnrollment
80 lines
2.3 KiB
C#
80 lines
2.3 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using StudentEnrollmentContracts.BindingModels;
|
|
using StudentEnrollmentContracts.BusinessLogicContracts;
|
|
using StudentEnrollmentContracts.SearchModels;
|
|
|
|
namespace StudentEnrollmentView
|
|
{
|
|
public partial class FormFaculty : Form
|
|
{
|
|
private readonly ILogger _logger;
|
|
private readonly IFacultyLogic _logic;
|
|
private int? _id;
|
|
public int Id { set { _id = value; } }
|
|
public FormFaculty(ILogger<FormFaculty> logger, IFacultyLogic logic)
|
|
{
|
|
InitializeComponent();
|
|
_logger = logger;
|
|
_logic = logic;
|
|
}
|
|
private void FormFaculty_Load(object sender, EventArgs e)
|
|
{
|
|
if (_id.HasValue)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("Получение факультета");
|
|
var view = _logic.ReadElement(new FacultySearchModel
|
|
{
|
|
faculty_id = _id.Value
|
|
});
|
|
if (view != null)
|
|
{
|
|
textBoxName.Text = view.FacultyName;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(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;
|
|
}
|
|
_logger.LogInformation("Сохранение факультета");
|
|
try
|
|
{
|
|
var model = new FacultyBindingModel
|
|
{
|
|
Id = _id ?? 0,
|
|
FacultyName = textBoxName.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();
|
|
}
|
|
}
|
|
}
|