diff --git a/StudentProgress/StudentProgress/Forms/FormSubject.cs b/StudentProgress/StudentProgress/Forms/FormSubject.cs index bcea12a..e6fb092 100644 --- a/StudentProgress/StudentProgress/Forms/FormSubject.cs +++ b/StudentProgress/StudentProgress/Forms/FormSubject.cs @@ -1,4 +1,5 @@ using StudentProgress.Entities; +using StudentProgress.Entities.Enums; using StudentProgress.Repositories; using System; using System.Windows.Forms; @@ -8,11 +9,46 @@ 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 +59,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 +77,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..db5647e 100644 --- a/StudentProgress/StudentProgress/Forms/FormSubjects.cs +++ b/StudentProgress/StudentProgress/Forms/FormSubjects.cs @@ -9,50 +9,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 @@