diff --git a/StudentProgress/StudentProgress/Entities/Grades.cs b/StudentProgress/StudentProgress/Entities/Grades.cs index 7b89070..3938d73 100644 --- a/StudentProgress/StudentProgress/Entities/Grades.cs +++ b/StudentProgress/StudentProgress/Entities/Grades.cs @@ -1,11 +1,32 @@ -namespace StudentProgress.Entities; +using System.ComponentModel; + +namespace StudentProgress.Entities; public class Grades { public int Id { get; private set; } + + [Browsable(false)] public int SubjectsId { get; private set; } + + [Browsable(false)] public int ProfessorsId { get; private set; } + + [DisplayName("Предмет")] + public string SubjectsName { get; private set; } = string.Empty; + + [DisplayName("Преподаватель")] + public string ProfessorsName { get; private set; } = string.Empty; + + [DisplayName("Дата")] public DateTime Date { get; private set; } + + [DisplayName("Оценки")] + public string GradeStudent => StudentGrade != null ? + string.Join(", ", StudentGrade.Select(x => $"{x.StudentName} {x.Grade}")) : + string.Empty; + + [Browsable(false)] public IEnumerable StudentGrade { get; private set; } = []; public static Grades CreateEntity(int id, int subjectsId, int professorsId, DateTime date, IEnumerable studentGrades) { @@ -19,15 +40,11 @@ public class Grades }; } - public static Grades CreateEntity(TempStudentGrades tempStudentGrades, IEnumerable studentGrades) + public void SetStudentGrade(IEnumerable studentGrade) { - return new Grades + if (studentGrade != null && studentGrade.Any()) { - Id = tempStudentGrades.Id, - SubjectsId = tempStudentGrades.SubjectsId, - ProfessorsId = tempStudentGrades.ProfessorsId, - Date = tempStudentGrades.Date, - StudentGrade = studentGrades - }; + StudentGrade = studentGrade; + } } } diff --git a/StudentProgress/StudentProgress/Entities/Group.cs b/StudentProgress/StudentProgress/Entities/Group.cs index 91ef307..ac82255 100644 --- a/StudentProgress/StudentProgress/Entities/Group.cs +++ b/StudentProgress/StudentProgress/Entities/Group.cs @@ -1,8 +1,12 @@ -namespace StudentProgress.Entities; +using System.ComponentModel; + +namespace StudentProgress.Entities; public class Group { public int Id { get; private set; } + + [DisplayName("Название")] public string NameGroup { get; set; } = string.Empty; public static Group CreateEntity(int id, string nameGroup) diff --git a/StudentProgress/StudentProgress/Entities/Lectures.cs b/StudentProgress/StudentProgress/Entities/Lectures.cs index f52bd7f..d8b386c 100644 --- a/StudentProgress/StudentProgress/Entities/Lectures.cs +++ b/StudentProgress/StudentProgress/Entities/Lectures.cs @@ -1,11 +1,23 @@ -namespace StudentProgress.Entities; +using System.ComponentModel; + +namespace StudentProgress.Entities; public class Lectures { public int Id { get; private set; } + + [Browsable(false)] public int ProfessorsId { get; private set; } + + [DisplayName("Преподаватель")] + public string ProfessorsName { get; private set; } = string.Empty; + + [DisplayName("Дата")] public DateTime Date { get; private set; } + + [DisplayName("Аудитория")] public string Auditorium { get; private set; } = string.Empty; + public static Lectures CreateElement(int id, int professorsId, DateTime date, string auditorium) { return new Lectures diff --git a/StudentProgress/StudentProgress/Entities/Professors.cs b/StudentProgress/StudentProgress/Entities/Professors.cs index fb964ba..0a1a9d5 100644 --- a/StudentProgress/StudentProgress/Entities/Professors.cs +++ b/StudentProgress/StudentProgress/Entities/Professors.cs @@ -1,9 +1,15 @@ -namespace StudentProgress.Entities; +using System.ComponentModel; + +namespace StudentProgress.Entities; public class Professors { public int Id { get; set; } + + [DisplayName("Имя")] public string FirstName { get; set; } = string.Empty; + + [DisplayName("Фамилия")] public string Surname { get; set; } = string.Empty; public static Professors CreateEntity(int id, string firstName, string surname) { diff --git a/StudentProgress/StudentProgress/Entities/Student.cs b/StudentProgress/StudentProgress/Entities/Student.cs index 000b6a6..7369a99 100644 --- a/StudentProgress/StudentProgress/Entities/Student.cs +++ b/StudentProgress/StudentProgress/Entities/Student.cs @@ -1,12 +1,23 @@ -namespace StudentProgress.Entities; +using System.ComponentModel; + +namespace StudentProgress.Entities; public class Student { public int Id { get; private set; } + + [DisplayName("Имя")] public string Name { get; set; } = string.Empty; + + [DisplayName("Фамилия")] public string Surname { get; set; } = string.Empty; + + [Browsable(false)] public int GroupId { get; set; } + [DisplayName("Группа")] + public string GroupName { get; set; } = string.Empty; + public static Student CreateEntity(int id, string name, string surname, int groupId) { return new Student diff --git a/StudentProgress/StudentProgress/Entities/StudentGrades.cs b/StudentProgress/StudentProgress/Entities/StudentGrades.cs index fd5c062..e010129 100644 --- a/StudentProgress/StudentProgress/Entities/StudentGrades.cs +++ b/StudentProgress/StudentProgress/Entities/StudentGrades.cs @@ -6,6 +6,7 @@ public class StudentGrades { public int Id { get; private set; } public int StudentID { get; private set; } + public string StudentName { get; private set; } = string.Empty; public Grade Grade { get; private set; } public static StudentGrades CreateEntity(int id, int studentID, Grade grade) { diff --git a/StudentProgress/StudentProgress/Entities/Subjects.cs b/StudentProgress/StudentProgress/Entities/Subjects.cs index 30bb035..7bd431b 100644 --- a/StudentProgress/StudentProgress/Entities/Subjects.cs +++ b/StudentProgress/StudentProgress/Entities/Subjects.cs @@ -1,12 +1,16 @@ -using Microsoft.VisualBasic.Devices; -using StudentProgress.Entities.Enums; +using StudentProgress.Entities.Enums; +using System.ComponentModel; namespace StudentProgress.Entities; public class Subjects { public int Id { get; private set; } + + [DisplayName("Название")] public string NameSubject { get; private set; } = string.Empty; + + [DisplayName("Курсы")] public Course Course { get; private set; } public static Subjects CreateEntity_(int id, string nameSubject, Course course) { diff --git a/StudentProgress/StudentProgress/Entities/TempStudentGrades.cs b/StudentProgress/StudentProgress/Entities/TempStudentGrades.cs deleted file mode 100644 index c6d6b18..0000000 --- a/StudentProgress/StudentProgress/Entities/TempStudentGrades.cs +++ /dev/null @@ -1,18 +0,0 @@ -using StudentProgress.Entities.Enums; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace StudentProgress.Entities; - -public class TempStudentGrades -{ - public int Id { get; private set; } - public int SubjectsId { get; private set; } - public int ProfessorsId { get; private set; } - public DateTime Date { get; private set; } - public int StudentID { get; private set; } - public Grade Grade { get; private set; } -} diff --git a/StudentProgress/StudentProgress/FormStudentProgress.cs b/StudentProgress/StudentProgress/FormStudentProgress.cs index 27b7e61..6859e73 100644 --- a/StudentProgress/StudentProgress/FormStudentProgress.cs +++ b/StudentProgress/StudentProgress/FormStudentProgress.cs @@ -97,6 +97,41 @@ namespace StudentProgress MessageBox.Show(ex.Message, " ", MessageBoxButtons.OK, MessageBoxIcon.Error); } } + + private void toolStripMenuItemDocReport_Click(object sender, EventArgs e) + { + try + { + _container.Resolve().ShowDialog(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, " ", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void toolStripMenuItemStudentGradeReport_Click(object sender, EventArgs e) + { + try + { + _container.Resolve().ShowDialog(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, " ", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void toolStripMenuItemGradeDistribution_Click(object sender, EventArgs e) + { + try + { + _container.Resolve().ShowDialog(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, " ", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } } } - \ No newline at end of file diff --git a/StudentProgress/StudentProgress/Forms/FormLecturesCount.cs b/StudentProgress/StudentProgress/Forms/FormLecturesCount.cs index f677e16..5032a86 100644 --- a/StudentProgress/StudentProgress/Forms/FormLecturesCount.cs +++ b/StudentProgress/StudentProgress/Forms/FormLecturesCount.cs @@ -1,21 +1,18 @@ -using StudentProgress.Entities; -using StudentProgress.Repositories; -using System; -using System.Collections.Generic; -using System.Linq; +using StudentProgress.Repositories; using System.Windows.Forms; +using Unity; namespace StudentProgress.Forms { public partial class FormLecturesCount : Form { - private readonly IProfessorsRepository _professorsRepository; + private readonly IUnityContainer _container; private readonly ILecturesRepository _lecturesRepository; - public FormLecturesCount(IProfessorsRepository professorsRepository, ILecturesRepository lecturesRepository) + public FormLecturesCount(IUnityContainer container, ILecturesRepository lecturesRepository) { InitializeComponent(); - _professorsRepository = professorsRepository; + _container = container ?? throw new ArgumentNullException(nameof(container)); _lecturesRepository = lecturesRepository; } @@ -26,71 +23,56 @@ namespace StudentProgress.Forms private void LoadLectures() { - var lectures = _lecturesRepository.ReadLectures(); - var lectureViewModels = new List(); - - foreach (var lecture in lectures) - { - var professor = _professorsRepository.ReadProfessorsNameById(lecture.ProfessorsId); - lectureViewModels.Add(new LectureViewModel - { - LectureId = lecture.LectureId, - ProfessorName = $"{professor.FirstNameProfessor} {professor.SurnameProfessor}", - Auditorium = lecture.Auditorium, - Date = lecture.Date - }); - } - - LecturesDataGridView.DataSource = lectureViewModels; + LecturesDataGridView.DataSource = _lecturesRepository.ReadLectures(); + LecturesDataGridView.Columns["Id"].Visible = false; } private void buttonAdd_Click(object sender, EventArgs e) { - using (var formRecordLecture = new FormRecordLecture(_professorsRepository, _lecturesRepository)) + try { - formRecordLecture.ShowDialog(); - LoadLectures(); // Обновляем данные после закрытия формы + _container.Resolve().ShowDialog(); + LoadLectures(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка при добавлении", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void buttonDel_Click(object sender, EventArgs e) { - if (LecturesDataGridView.SelectedRows.Count > 0) + if (!TryGetIdentifierFromSelectedRow(out var findId)) { - var selectedRow = LecturesDataGridView.SelectedRows[0]; - var lectureViewModel = selectedRow.DataBoundItem as LectureViewModel; - - if (lectureViewModel != null) - { - // Логика удаления лекции - _lecturesRepository.DeleteLecture(lectureViewModel.LectureId); - - MessageBox.Show("Лекция успешно удалена!", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information); - LoadLectures(); // Обновляем данные после удаления - } + return; } - else + + if (MessageBox.Show("Удалить запись?", "Удаление", MessageBoxButtons.YesNo) != DialogResult.Yes) { - MessageBox.Show("Выберите лекцию для удаления.", "Информация", MessageBoxButtons.OK, MessageBoxIcon.Information); + return; + } + + try + { + _lecturesRepository.DeleteLecture(findId); + LoadLectures(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка при удалении", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - private void LecturesDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e) + private bool TryGetIdentifierFromSelectedRow(out int id) { + id = 0; + if (LecturesDataGridView.SelectedRows.Count < 1) + { + MessageBox.Show("Нет выбранной записи", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return false; + } - } - } - - public class LectureViewModel - { - public int LectureId { get; set; } - public string ProfessorName { get; set; } - public string Auditorium { get; set; } - public DateTime Date { get; set; } - - public LectureViewModel() - { - ProfessorName = string.Empty; // Инициализация свойства ProfessorName - Auditorium = string.Empty; // Инициализация свойства Auditorium + id = Convert.ToInt32(LecturesDataGridView.SelectedRows[0].Cells["Id"].Value); + return true; } } } \ No newline at end of file diff --git a/StudentProgress/StudentProgress/Forms/FormStudent.cs b/StudentProgress/StudentProgress/Forms/FormStudent.cs index bb96eb9..c40e1fb 100644 --- a/StudentProgress/StudentProgress/Forms/FormStudent.cs +++ b/StudentProgress/StudentProgress/Forms/FormStudent.cs @@ -1,52 +1,102 @@ using System; using System.Windows.Forms; +using Unity; using StudentProgress.Repositories; -using StudentProgress.Entities; namespace StudentProgress.Forms { - public partial class FormStudent : Form + public partial class FormStudents : Form { + private readonly IUnityContainer _container; private readonly IStudentRepository _studentRepository; - private readonly IGroupRepository _groupRepository; - public FormStudent(IStudentRepository studentRepository, IGroupRepository groupRepository) + public FormStudents(IUnityContainer container, IStudentRepository studentRepository) { InitializeComponent(); + _container = container ?? throw new ArgumentNullException(nameof(container)); _studentRepository = studentRepository ?? throw new ArgumentNullException(nameof(studentRepository)); - _groupRepository = groupRepository ?? throw new ArgumentNullException(nameof(groupRepository)); - - LoadGroups(); } - private void LoadGroups() - { - var groups = _groupRepository.ReadGroup(); - comboBoxGroup.DataSource = groups; - comboBoxGroup.DisplayMember = "NameGroup"; - comboBoxGroup.ValueMember = "Id"; - } - - private void buttonSave_Click(object sender, EventArgs e) + private void FormStudents_Load(object sender, EventArgs e) { try { - if (string.IsNullOrWhiteSpace(textBoxName.Text) || string.IsNullOrWhiteSpace(textBoxSurname.Text) || comboBoxGroup.SelectedIndex < 0) - { - throw new Exception("Имеются незаполненные поля"); - } - - var student = Student.CreateEntity(0, textBoxName.Text, textBoxSurname.Text, (int)comboBoxGroup.SelectedValue); - _studentRepository.CreateStudent(student); - MessageBox.Show("Студент успешно добавлен", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information); - Close(); + LoadList(); } catch (Exception ex) { - MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show(ex.Message, "Ошибка при загрузке", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - private void buttonCancel_Click(object sender, EventArgs e) => Close(); + private void buttonAdd_Click(object sender, EventArgs e) + { + try + { + _container.Resolve().ShowDialog(); + LoadList(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка при добавлении", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonPencil_Click(object sender, EventArgs e) + { + if (!TryGetIdentifierFromSelectedRow(out var findId)) + { + return; + } + try + { + var form = _container.Resolve(); + form.Id = findId; + form.ShowDialog(); + LoadList(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка при изменении", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonDel_Click(object sender, EventArgs e) + { + if (!TryGetIdentifierFromSelectedRow(out var findId)) + { + return; + } + if (MessageBox.Show("Удалить запись?", "Удаление", MessageBoxButtons.YesNo) != DialogResult.Yes) + { + return; + } + try + { + _studentRepository.DeleteStudent(findId); + LoadList(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка при удалении", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void LoadList() + { + dataGridView.DataSource = _studentRepository.ReadStudents(); + dataGridView.Columns["Id"].Visible = false; + } + private bool TryGetIdentifierFromSelectedRow(out int id) + { + id = 0; + if (dataGridView.SelectedRows.Count < 1) + { + MessageBox.Show("Нет выбранной записи", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return false; + } + id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + return true; + } } } \ No newline at end of file diff --git a/StudentProgress/StudentProgress/Forms/FormStudents.cs b/StudentProgress/StudentProgress/Forms/FormStudents.cs index 9b1fe2d..c40e1fb 100644 --- a/StudentProgress/StudentProgress/Forms/FormStudents.cs +++ b/StudentProgress/StudentProgress/Forms/FormStudents.cs @@ -51,7 +51,7 @@ namespace StudentProgress.Forms try { var form = _container.Resolve(); - // Здесь нужно добавить логику для загрузки данных в форму редактирования + form.Id = findId; form.ShowDialog(); LoadList(); } @@ -82,8 +82,11 @@ namespace StudentProgress.Forms } } - private void LoadList() => dataGridView.DataSource = _studentRepository.ReadStudents(); - + private void LoadList() + { + dataGridView.DataSource = _studentRepository.ReadStudents(); + dataGridView.Columns["Id"].Visible = false; + } private bool TryGetIdentifierFromSelectedRow(out int id) { id = 0; diff --git a/StudentProgress/StudentProgress/Forms/FormSubject.cs b/StudentProgress/StudentProgress/Forms/FormSubject.cs index bcea12a..0a11279 100644 --- a/StudentProgress/StudentProgress/Forms/FormSubject.cs +++ b/StudentProgress/StudentProgress/Forms/FormSubject.cs @@ -1,18 +1,52 @@ using StudentProgress.Entities; +using StudentProgress.Entities.Enums; using StudentProgress.Repositories; -using System; -using System.Windows.Forms; namespace StudentPerformance.Forms { public partial class FormSubject : Form { private readonly ISubjectsRepository _subjectsRepository; + private int? _subjectId; + public int Id + { + set + { + try + { + var subject = _subjectsRepository.ReadSubjectById(value); + if (subject == null) + { + throw new InvalidDataException(nameof(subject)); + } + + foreach (Course elem in Enum.GetValues(typeof(Course))) + { + if ((elem & subject.Course) != 0) + { + checkedListBoxCourses.SetItemChecked(checkedListBoxCourses.Items.IndexOf(elem), true); + } + } + textBoxName.Text = subject.NameSubject; + _subjectId = subject.Id; + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + } + } public FormSubject(ISubjectsRepository subjectsRepository) { InitializeComponent(); _subjectsRepository = subjectsRepository ?? throw new ArgumentNullException(nameof(subjectsRepository)); + + foreach (var elem in Enum.GetValues(typeof(Course))) + { + checkedListBoxCourses.Items.Add(elem); + } } private void buttonSave_Click(object sender, EventArgs e) @@ -23,9 +57,14 @@ namespace StudentPerformance.Forms { throw new Exception("Имя предмета не может быть пустым"); } - - var subject = Subjects.CreateEntity_(0, textBoxName.Text); - _subjectsRepository.CreateSubjects_(subject); + if (_subjectId.HasValue) + { + _subjectsRepository.UpdateSubject(CreateEntity(_subjectId.Value)); + } + else + { + _subjectsRepository.CreateSubjects_(CreateEntity(0)); + } Close(); } catch (Exception ex) @@ -36,9 +75,15 @@ namespace StudentPerformance.Forms private void buttonCancel_Click(object sender, EventArgs e) => Close(); - private void FormSubject_Load(object sender, EventArgs e) + private Subjects CreateEntity(int id) { - // Инициализация формы, если необходимо + Course course = Course.None; + + foreach (var elem in checkedListBoxCourses.CheckedItems) + { + course |= (Course)elem; + } + return Subjects.CreateEntity_(id, textBoxName.Text, course); } } } \ No newline at end of file diff --git a/StudentProgress/StudentProgress/Forms/FormSubjects.Designer.cs b/StudentProgress/StudentProgress/Forms/FormSubjects.Designer.cs index ef5306e..cde700d 100644 --- a/StudentProgress/StudentProgress/Forms/FormSubjects.Designer.cs +++ b/StudentProgress/StudentProgress/Forms/FormSubjects.Designer.cs @@ -36,29 +36,29 @@ namespace StudentProgress.Forms buttonDel = new Button(); buttonAdd = new Button(); dataGridView = new DataGridView(); + buttonUpd = new Button(); panel1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); // // panel1 // + panel1.Controls.Add(buttonUpd); panel1.Controls.Add(buttonDel); panel1.Controls.Add(buttonAdd); panel1.Dock = DockStyle.Right; - panel1.Location = new Point(1018, 0); - panel1.Margin = new Padding(6, 6, 6, 6); + panel1.Location = new Point(548, 0); panel1.Name = "panel1"; - panel1.Size = new Size(139, 578); + panel1.Size = new Size(75, 271); panel1.TabIndex = 0; // // buttonDel // buttonDel.BackgroundImage = Properties.Resources.Del; buttonDel.BackgroundImageLayout = ImageLayout.Stretch; - buttonDel.Location = new Point(13, 367); - buttonDel.Margin = new Padding(6, 6, 6, 6); + buttonDel.Location = new Point(7, 172); buttonDel.Name = "buttonDel"; - buttonDel.Size = new Size(104, 113); + buttonDel.Size = new Size(56, 53); buttonDel.TabIndex = 1; buttonDel.UseVisualStyleBackColor = true; buttonDel.Click += buttonDel_Click; @@ -67,10 +67,9 @@ namespace StudentProgress.Forms // buttonAdd.BackgroundImage = Properties.Resources.Add; buttonAdd.BackgroundImageLayout = ImageLayout.Stretch; - buttonAdd.Location = new Point(13, 73); - buttonAdd.Margin = new Padding(6, 6, 6, 6); + buttonAdd.Location = new Point(7, 34); buttonAdd.Name = "buttonAdd"; - buttonAdd.Size = new Size(104, 113); + buttonAdd.Size = new Size(56, 53); buttonAdd.TabIndex = 0; buttonAdd.UseVisualStyleBackColor = true; buttonAdd.Click += buttonAdd_Click; @@ -85,23 +84,32 @@ namespace StudentProgress.Forms dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; dataGridView.Dock = DockStyle.Fill; dataGridView.Location = new Point(0, 0); - dataGridView.Margin = new Padding(6, 6, 6, 6); dataGridView.MultiSelect = false; dataGridView.Name = "dataGridView"; dataGridView.ReadOnly = true; dataGridView.RowHeadersVisible = false; dataGridView.RowHeadersWidth = 82; - dataGridView.Size = new Size(1018, 578); + dataGridView.Size = new Size(548, 271); dataGridView.TabIndex = 1; // + // buttonUpd + // + buttonUpd.BackgroundImage = Properties.Resources.Pencil; + buttonUpd.BackgroundImageLayout = ImageLayout.Stretch; + buttonUpd.Location = new Point(7, 93); + buttonUpd.Name = "buttonUpd"; + buttonUpd.Size = new Size(56, 53); + buttonUpd.TabIndex = 2; + buttonUpd.UseVisualStyleBackColor = true; + buttonUpd.Click += buttonUpd_Click; + // // FormSubjects // - AutoScaleDimensions = new SizeF(13F, 32F); + AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1157, 578); + ClientSize = new Size(623, 271); Controls.Add(dataGridView); Controls.Add(panel1); - Margin = new Padding(6, 6, 6, 6); Name = "FormSubjects"; Text = "Предметы"; Load += FormSubjects_Load; @@ -116,5 +124,6 @@ namespace StudentProgress.Forms private DataGridView dataGridView; private Button buttonDel; private Button buttonAdd; + private Button buttonUpd; } } \ No newline at end of file diff --git a/StudentProgress/StudentProgress/Forms/FormSubjects.cs b/StudentProgress/StudentProgress/Forms/FormSubjects.cs index 6c07d9c..9c20306 100644 --- a/StudentProgress/StudentProgress/Forms/FormSubjects.cs +++ b/StudentProgress/StudentProgress/Forms/FormSubjects.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel; using System.Windows.Forms; using StudentPerformance.Forms; using StudentProgress.Repositories; @@ -9,50 +10,89 @@ namespace StudentProgress.Forms public partial class FormSubjects : Form { private readonly ISubjectsRepository _subjectsRepository; + private readonly IUnityContainer _container; - public FormSubjects() + public FormSubjects(IUnityContainer container, ISubjectsRepository subjectsRepository) { InitializeComponent(); - var container = Program.CreateContainer(); // Получаем контейнер Unity - _subjectsRepository = container.Resolve(); + _container = container ?? throw new ArgumentNullException(nameof(container)); + _subjectsRepository = subjectsRepository ?? throw new ArgumentNullException(nameof(subjectsRepository)); } - private void FormSubjects_Load(object sender, EventArgs e) - { - // Загрузка данных в DataGridView - LoadData(); - } + private void FormSubjects_Load(object sender, EventArgs e) => LoadData(); private void LoadData() { - // Пример загрузки данных из репозитория - var subjects = _subjectsRepository.ReadSubjects(); - dataGridView.DataSource = subjects; + dataGridView.DataSource = _subjectsRepository.ReadSubjects(); + dataGridView.Columns["Id"].Visible = false; } - private void buttonAdd_Click(object sender, EventArgs e) { - // Логика добавления нового предмета - using (var form = new FormSubject(Program.CreateContainer().Resolve())) + try { - if (form.ShowDialog() == DialogResult.OK) - { - LoadData(); - } + _container.Resolve().ShowDialog(); + LoadData(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка при добавлении", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void buttonDel_Click(object sender, EventArgs e) { - // Логика удаления выбранного предмета - if (dataGridView.SelectedRows.Count > 0) + if (!TryGetIdentifierFromSelectedRow(out var findId)) { - var selectedSubject = dataGridView.SelectedRows[0].DataBoundItem as Entities.Subjects; - if (selectedSubject != null) - { - _subjectsRepository.DeleteSubjects(selectedSubject.Id); - LoadData(); - } + return; + } + + if (MessageBox.Show("Удалить запись?", "Удаление", MessageBoxButtons.YesNo) != DialogResult.Yes) + { + return; + } + + try + { + _subjectsRepository.DeleteSubjects(findId); + LoadData(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка при удалении", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private bool TryGetIdentifierFromSelectedRow(out int id) + { + id = 0; + if (dataGridView.SelectedRows.Count < 1) + { + MessageBox.Show("Нет выбранной записи", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return false; + } + + id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + return true; + + } + + private void buttonUpd_Click(object sender, EventArgs e) + { + if (!TryGetIdentifierFromSelectedRow(out var findId)) + { + return; + } + + try + { + var form = _container.Resolve(); + form.Id = findId; + form.ShowDialog(); + LoadData(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка при изменении", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } diff --git a/StudentProgress/StudentProgress/Forms/FormSubjects.resx b/StudentProgress/StudentProgress/Forms/FormSubjects.resx index 8b2ff64..af32865 100644 --- a/StudentProgress/StudentProgress/Forms/FormSubjects.resx +++ b/StudentProgress/StudentProgress/Forms/FormSubjects.resx @@ -1,7 +1,7 @@