PIbd-32_Artamonova_T.V._COP_2/UniversityView/FormStudent.cs

160 lines
6.1 KiB
C#
Raw Normal View History

2023-11-09 04:07:06 +04:00
using DocumentFormat.OpenXml.Office2010.Excel;
using Microsoft.Extensions.Logging;
using NPOI.OpenXmlFormats.Spreadsheet;
2023-10-28 09:06:35 +04:00
using System.ComponentModel;
2023-11-09 04:07:06 +04:00
using UniversityBusinessLogic.BusinessLogics;
2023-10-28 09:06:35 +04:00
using UniversityContracts.BindingModels;
using UniversityContracts.BusinessLogicsContracts;
2023-11-09 04:07:06 +04:00
using UniversityContracts.ViewModels;
2023-10-28 09:06:35 +04:00
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; } }
2023-11-09 04:07:06 +04:00
private string? _item;
private string itemChecked { set { _item = value; } }
2023-10-28 09:06:35 +04:00
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
{
2023-11-09 04:07:06 +04:00
_studentLogic.CreateOrUpdate(new StudentBindingModel
2023-10-28 09:06:35 +04:00
{
2023-11-09 04:07:06 +04:00
Id = _id,
2023-10-28 09:06:35 +04:00
FIO = textBoxFIO.Text,
PhotoFilePath = imagePath,
2023-11-09 04:07:06 +04:00
Email = componenttBox1.TextBoxValue,
2023-10-28 09:06:35 +04:00
DirectionName = userCheckedListBox1.SelectedValue
2023-11-09 04:07:06 +04:00
}); ;
2023-10-28 09:06:35 +04:00
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();
2023-11-09 04:07:06 +04:00
if (_id.HasValue)
{
try
{
StudentViewModel? view = _studentLogic.Read(new StudentBindingModel { Id = _id.Value })?[0];
if (view != null)
{
textBoxFIO.Text = view.FIO;
componenttBox1.TextBoxValue = view.Email;
imagePath = view.PhotoFilePath;
string[] dirs = view.DirectionName.Split(";");
foreach (var dir in dirs)
{
userCheckedListBox1.SelectedValue = dir;
}
pictureBox.Image = Image.FromFile(imagePath);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
2023-10-28 09:06:35 +04:00
}
private void LoadData()
{
_logger.LogInformation("Загрузка направлений");
try
{
2023-11-09 04:07:06 +04:00
var list = _directionLogic.Read(null);
2023-10-28 09:06:35 +04:00
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);
}
}
}
}