using DocumentFormat.OpenXml.Office2010.Excel; using Microsoft.Extensions.Logging; using NPOI.OpenXmlFormats.Spreadsheet; using System.ComponentModel; using UniversityBusinessLogic.BusinessLogics; using UniversityContracts.BindingModels; using UniversityContracts.BusinessLogicsContracts; using UniversityContracts.ViewModels; namespace UniversityView { public partial class FormStudent : Form { private readonly ILogger _logger; private readonly IStudentLogic _studentLogic; private readonly IDirectionLogic _directionLogic; private string imagePath; BindingList _list; private int? _id; public int Id { set { _id = value; } } private string? _item; private string itemChecked { set { _item = value; } } List directions = new(); public FormStudent(ILogger logger, IStudentLogic studentLogic, IDirectionLogic directionLogic) { InitializeComponent(); _logger = logger; _studentLogic = studentLogic; _directionLogic = directionLogic; imagePath = ""; _list = new BindingList(); 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 { _studentLogic.CreateOrUpdate(new StudentBindingModel { Id = _id, FIO = textBoxFIO.Text, PhotoFilePath = imagePath, Email = componenttBox1.TextBoxValue, DirectionName = userCheckedListBox1.SelectedValue }); ; 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(); 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); } } } private void LoadData() { _logger.LogInformation("Загрузка направлений"); try { var list = _directionLogic.Read(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); } } } }