135 lines
5.2 KiB
C#
135 lines
5.2 KiB
C#
|
using Microsoft.Extensions.Logging;
|
|||
|
using System.ComponentModel;
|
|||
|
using UniversityContracts.BindingModels;
|
|||
|
using UniversityContracts.BusinessLogicsContracts;
|
|||
|
|
|||
|
namespace UniversityView
|
|||
|
{
|
|||
|
public partial class FormStudent : Form
|
|||
|
{
|
|||
|
private readonly ILogger _logger;
|
|||
|
private readonly IStudentLogic _studentLogic;
|
|||
|
private readonly IDirectionLogic _directionLogic;
|
|||
|
private string imagePath;
|
|||
|
BindingList<DirectionBindingModel> _list;
|
|||
|
private int? _id;
|
|||
|
public int Id { set { _id = value; } }
|
|||
|
List<string> directions = new();
|
|||
|
public FormStudent(ILogger<FormStudent> logger, IStudentLogic studentLogic, IDirectionLogic directionLogic)
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
_logger = logger;
|
|||
|
_studentLogic = studentLogic;
|
|||
|
_directionLogic = directionLogic;
|
|||
|
imagePath = "";
|
|||
|
_list = new BindingList<DirectionBindingModel>();
|
|||
|
componenttBox1.Pattern = @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$";
|
|||
|
componenttBox1.setExample("example@mail.ru");
|
|||
|
}
|
|||
|
|
|||
|
private void buttonSave_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(textBoxFIO.Text))
|
|||
|
{
|
|||
|
MessageBox.Show("Заполните ФИО", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(componenttBox1.TextBoxValue))
|
|||
|
{
|
|||
|
MessageBox.Show("Заполните электронную почту", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
if (userCheckedListBox1.SelectedValue == null)
|
|||
|
{
|
|||
|
MessageBox.Show("Заполните направление", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
if (pictureBox.Image == null)
|
|||
|
{
|
|||
|
MessageBox.Show("Загрузите фото", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
_logger.LogInformation("Сохранение студента");
|
|||
|
try
|
|||
|
{
|
|||
|
var model = new StudentBindingModel
|
|||
|
{
|
|||
|
Id = _id ?? 0,
|
|||
|
FIO = textBoxFIO.Text,
|
|||
|
Email = componenttBox1.TextBoxValue,
|
|||
|
PhotoFilePath = imagePath,
|
|||
|
DirectionName = userCheckedListBox1.SelectedValue
|
|||
|
};
|
|||
|
var operationResult = _id.HasValue ? _studentLogic.Update(model) : _studentLogic.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 void FormStudent_Load(object sender, EventArgs e)
|
|||
|
{
|
|||
|
LoadData();
|
|||
|
}
|
|||
|
|
|||
|
private void LoadData()
|
|||
|
{
|
|||
|
_logger.LogInformation("Загрузка направлений");
|
|||
|
try
|
|||
|
{
|
|||
|
var list = _directionLogic.ReadList(null);
|
|||
|
userCheckedListBox1.ClearList();
|
|||
|
directions.Clear();
|
|||
|
_list.Clear();
|
|||
|
if (list != null)
|
|||
|
{
|
|||
|
foreach (var item in list)
|
|||
|
{
|
|||
|
directions.Add(item.Name);
|
|||
|
_list.Add(new DirectionBindingModel
|
|||
|
{
|
|||
|
Id = item.Id,
|
|||
|
Name = item.Name,
|
|||
|
});
|
|||
|
}
|
|||
|
}
|
|||
|
userCheckedListBox1.FillList(directions);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
_logger.LogError(ex, "Ошибка загрузки направлений");
|
|||
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void buttonAddPhoto_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
OpenFileDialog openFileDialog = new()
|
|||
|
{
|
|||
|
Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG|All files (*.*)|*.*"
|
|||
|
};
|
|||
|
|
|||
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
|||
|
{
|
|||
|
imagePath = openFileDialog.FileName;
|
|||
|
pictureBox.Image = Image.FromFile(imagePath);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|