Правки
This commit is contained in:
parent
bbeae41de5
commit
5b143a2cb8
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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<ISubjectsRepository>();
|
||||
_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<ISubjectsRepository>()))
|
||||
{
|
||||
if (form.ShowDialog() == DialogResult.OK)
|
||||
try
|
||||
{
|
||||
_container.Resolve<FormSubject>().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)
|
||||
return;
|
||||
}
|
||||
|
||||
if (MessageBox.Show("Удалить запись?", "Удаление", MessageBoxButtons.YesNo) != DialogResult.Yes)
|
||||
{
|
||||
_subjectsRepository.DeleteSubjects(selectedSubject.Id);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user