Правки

This commit is contained in:
safia 2024-12-21 14:15:19 +04:00
parent 472f599a62
commit 88a334708b
4 changed files with 142 additions and 47 deletions

View File

@ -1,4 +1,5 @@
using StudentProgress.Entities; using StudentProgress.Entities;
using StudentProgress.Entities.Enums;
using StudentProgress.Repositories; using StudentProgress.Repositories;
using System; using System;
using System.Windows.Forms; using System.Windows.Forms;
@ -8,11 +9,46 @@ namespace StudentPerformance.Forms
public partial class FormSubject : Form public partial class FormSubject : Form
{ {
private readonly ISubjectsRepository _subjectsRepository; 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) public FormSubject(ISubjectsRepository subjectsRepository)
{ {
InitializeComponent(); InitializeComponent();
_subjectsRepository = subjectsRepository ?? throw new ArgumentNullException(nameof(subjectsRepository)); _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) private void buttonSave_Click(object sender, EventArgs e)
@ -23,9 +59,14 @@ namespace StudentPerformance.Forms
{ {
throw new Exception("Имя предмета не может быть пустым"); throw new Exception("Имя предмета не может быть пустым");
} }
if (_subjectId.HasValue)
var subject = Subjects.CreateEntity_(0, textBoxName.Text); {
_subjectsRepository.CreateSubjects_(subject); _subjectsRepository.UpdateSubject(CreateEntity(_subjectId.Value));
}
else
{
_subjectsRepository.CreateSubjects_(CreateEntity(0));
}
Close(); Close();
} }
catch (Exception ex) catch (Exception ex)
@ -36,9 +77,15 @@ namespace StudentPerformance.Forms
private void buttonCancel_Click(object sender, EventArgs e) => Close(); 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);
} }
} }
} }

View File

@ -36,29 +36,29 @@ namespace StudentProgress.Forms
buttonDel = new Button(); buttonDel = new Button();
buttonAdd = new Button(); buttonAdd = new Button();
dataGridView = new DataGridView(); dataGridView = new DataGridView();
buttonUpd = new Button();
panel1.SuspendLayout(); panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit();
SuspendLayout(); SuspendLayout();
// //
// panel1 // panel1
// //
panel1.Controls.Add(buttonUpd);
panel1.Controls.Add(buttonDel); panel1.Controls.Add(buttonDel);
panel1.Controls.Add(buttonAdd); panel1.Controls.Add(buttonAdd);
panel1.Dock = DockStyle.Right; panel1.Dock = DockStyle.Right;
panel1.Location = new Point(1018, 0); panel1.Location = new Point(548, 0);
panel1.Margin = new Padding(6, 6, 6, 6);
panel1.Name = "panel1"; panel1.Name = "panel1";
panel1.Size = new Size(139, 578); panel1.Size = new Size(75, 271);
panel1.TabIndex = 0; panel1.TabIndex = 0;
// //
// buttonDel // buttonDel
// //
buttonDel.BackgroundImage = Properties.Resources.Del; buttonDel.BackgroundImage = Properties.Resources.Del;
buttonDel.BackgroundImageLayout = ImageLayout.Stretch; buttonDel.BackgroundImageLayout = ImageLayout.Stretch;
buttonDel.Location = new Point(13, 367); buttonDel.Location = new Point(7, 172);
buttonDel.Margin = new Padding(6, 6, 6, 6);
buttonDel.Name = "buttonDel"; buttonDel.Name = "buttonDel";
buttonDel.Size = new Size(104, 113); buttonDel.Size = new Size(56, 53);
buttonDel.TabIndex = 1; buttonDel.TabIndex = 1;
buttonDel.UseVisualStyleBackColor = true; buttonDel.UseVisualStyleBackColor = true;
buttonDel.Click += buttonDel_Click; buttonDel.Click += buttonDel_Click;
@ -67,10 +67,9 @@ namespace StudentProgress.Forms
// //
buttonAdd.BackgroundImage = Properties.Resources.Add; buttonAdd.BackgroundImage = Properties.Resources.Add;
buttonAdd.BackgroundImageLayout = ImageLayout.Stretch; buttonAdd.BackgroundImageLayout = ImageLayout.Stretch;
buttonAdd.Location = new Point(13, 73); buttonAdd.Location = new Point(7, 34);
buttonAdd.Margin = new Padding(6, 6, 6, 6);
buttonAdd.Name = "buttonAdd"; buttonAdd.Name = "buttonAdd";
buttonAdd.Size = new Size(104, 113); buttonAdd.Size = new Size(56, 53);
buttonAdd.TabIndex = 0; buttonAdd.TabIndex = 0;
buttonAdd.UseVisualStyleBackColor = true; buttonAdd.UseVisualStyleBackColor = true;
buttonAdd.Click += buttonAdd_Click; buttonAdd.Click += buttonAdd_Click;
@ -85,23 +84,32 @@ namespace StudentProgress.Forms
dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridView.Dock = DockStyle.Fill; dataGridView.Dock = DockStyle.Fill;
dataGridView.Location = new Point(0, 0); dataGridView.Location = new Point(0, 0);
dataGridView.Margin = new Padding(6, 6, 6, 6);
dataGridView.MultiSelect = false; dataGridView.MultiSelect = false;
dataGridView.Name = "dataGridView"; dataGridView.Name = "dataGridView";
dataGridView.ReadOnly = true; dataGridView.ReadOnly = true;
dataGridView.RowHeadersVisible = false; dataGridView.RowHeadersVisible = false;
dataGridView.RowHeadersWidth = 82; dataGridView.RowHeadersWidth = 82;
dataGridView.Size = new Size(1018, 578); dataGridView.Size = new Size(548, 271);
dataGridView.TabIndex = 1; 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 // FormSubjects
// //
AutoScaleDimensions = new SizeF(13F, 32F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1157, 578); ClientSize = new Size(623, 271);
Controls.Add(dataGridView); Controls.Add(dataGridView);
Controls.Add(panel1); Controls.Add(panel1);
Margin = new Padding(6, 6, 6, 6);
Name = "FormSubjects"; Name = "FormSubjects";
Text = "Предметы"; Text = "Предметы";
Load += FormSubjects_Load; Load += FormSubjects_Load;
@ -116,5 +124,6 @@ namespace StudentProgress.Forms
private DataGridView dataGridView; private DataGridView dataGridView;
private Button buttonDel; private Button buttonDel;
private Button buttonAdd; private Button buttonAdd;
private Button buttonUpd;
} }
} }

View File

@ -9,50 +9,89 @@ namespace StudentProgress.Forms
public partial class FormSubjects : Form public partial class FormSubjects : Form
{ {
private readonly ISubjectsRepository _subjectsRepository; private readonly ISubjectsRepository _subjectsRepository;
private readonly IUnityContainer _container;
public FormSubjects() public FormSubjects(IUnityContainer container, ISubjectsRepository subjectsRepository)
{ {
InitializeComponent(); InitializeComponent();
var container = Program.CreateContainer(); // Получаем контейнер Unity _container = container ?? throw new ArgumentNullException(nameof(container));
_subjectsRepository = container.Resolve<ISubjectsRepository>(); _subjectsRepository = subjectsRepository ?? throw new ArgumentNullException(nameof(subjectsRepository));
} }
private void FormSubjects_Load(object sender, EventArgs e) private void FormSubjects_Load(object sender, EventArgs e) => LoadData();
{
// Загрузка данных в DataGridView
LoadData();
}
private void LoadData() private void LoadData()
{ {
// Пример загрузки данных из репозитория dataGridView.DataSource = _subjectsRepository.ReadSubjects();
var subjects = _subjectsRepository.ReadSubjects(); dataGridView.Columns["Id"].Visible = false;
dataGridView.DataSource = subjects;
} }
private void buttonAdd_Click(object sender, EventArgs e) private void buttonAdd_Click(object sender, EventArgs e)
{ {
// Логика добавления нового предмета try
using (var form = new FormSubject(Program.CreateContainer().Resolve<ISubjectsRepository>()))
{ {
if (form.ShowDialog() == DialogResult.OK) _container.Resolve<FormSubject>().ShowDialog();
{ LoadData();
LoadData(); }
} catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при добавлении", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
private void buttonDel_Click(object sender, EventArgs e) private void buttonDel_Click(object sender, EventArgs e)
{ {
// Логика удаления выбранного предмета if (!TryGetIdentifierFromSelectedRow(out var findId))
if (dataGridView.SelectedRows.Count > 0)
{ {
var selectedSubject = dataGridView.SelectedRows[0].DataBoundItem as Entities.Subjects; return;
if (selectedSubject != null) }
{
_subjectsRepository.DeleteSubjects(selectedSubject.Id); if (MessageBox.Show("Удалить запись?", "Удаление", MessageBoxButtons.YesNo) != DialogResult.Yes)
LoadData(); {
} 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<FormSubject>();
form.Id = findId;
form.ShowDialog();
LoadData();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при изменении", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
} }