using StudentProgressRecord.Entity; using StudentProgressRecord.Repositories; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace StudentProgressRecord { public partial class FormFaculty : Form { private readonly IFacultyRepository _facultyRepository; private int? _facultyId; public int Id { set { try { var faculty = _facultyRepository.ReadFacultyById(value); if (faculty == null) { throw new InvalidDataException(nameof(faculty)); } textBoxFaculty.Text = faculty.FacultyName; _facultyId = value; } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при полученииданных", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } } public FormFaculty(IFacultyRepository facultyRepository) { InitializeComponent(); _facultyRepository = facultyRepository ?? throw new ArgumentNullException(nameof(facultyRepository)); } private void ButtonSave_Click(object sender, EventArgs e) { try { if (string.IsNullOrWhiteSpace(textBoxFaculty.Text)) { throw new Exception("Название факультета не может быть пустым"); } if (_facultyId.HasValue) { _facultyRepository.UpdateFaculty(CreateFaculty(_facultyId.Value)); } else { _facultyRepository.CreateFaculty(CreateFaculty(0)); } Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void ButtonCancel_Click(object sender, EventArgs e) => Close(); private Faculty CreateFaculty(int id) => Faculty.CreateEntity(id, textBoxFaculty.Text); } }