PIBD14-Boyko-M.S.-GSM-Autot.../ProjectGSM/Forms/FormCase.cs

90 lines
3.0 KiB
C#
Raw Normal View History

2024-11-05 03:59:13 +04:00
using ProjectGSM.Entities;
2024-11-09 03:06:08 +04:00
using ProjectGSM.Entities.Enums;
2024-11-05 03:59:13 +04:00
using ProjectGSM.Repositories;
namespace ProjectGSM.Forms
{
public partial class FormCase : Form
{
private readonly ICaseRepository _caseRepository;
2024-11-09 03:06:08 +04:00
2024-11-18 23:50:05 +04:00
public FormCase(ICaseRepository caseRepository,IClientRepository clientRepository, ICourtRepository courtRepository, IAdvocateRepository advocateRepository)
2024-11-05 03:59:13 +04:00
{
InitializeComponent();
_caseRepository = caseRepository ??
throw new
ArgumentNullException(nameof(caseRepository));
2024-11-09 03:06:08 +04:00
typeApellBox.DataSource = Enum.GetValues(typeof(TypeAppeal));
2024-11-05 03:59:13 +04:00
2024-11-09 03:06:08 +04:00
ColumnAdvocate.DataSource = advocateRepository.ReadAdvocates();
ColumnAdvocate.DisplayMember = "Name";
ColumnAdvocate.ValueMember = "Id";
2024-11-05 03:59:13 +04:00
clientBox.DataSource = clientRepository.ReadClients();
clientBox.DisplayMember = "Name";
clientBox.ValueMember = "Id";
courtBox.DataSource = courtRepository.ReadCourts();
courtBox.DisplayMember = "Name";
courtBox.ValueMember = "Id";
}
private void saveButton_Click(object sender, EventArgs e)
{
try
2024-11-09 03:06:08 +04:00
{if(dataGridView1.RowCount < 1 || typeApellBox.SelectedIndex < 0 || clientBox.SelectedIndex < 0 || courtBox.SelectedIndex < 0
2024-11-05 03:59:13 +04:00
)
{
throw new Exception("Имеются незаполненные поля");
}
2024-11-18 23:50:05 +04:00
Case caseE = CreateCase(0);
caseE.Advocates = CreateListCaseAdvocateFromDataGrid();
_caseRepository.CreateCase(caseE);
2024-11-05 03:59:13 +04:00
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при сохранении",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void cancelButton_Click(object sender, EventArgs e) => Close();
2024-11-09 03:06:08 +04:00
private List<CaseAdvocate> CreateListCaseAdvocateFromDataGrid()
{
var list = new List<CaseAdvocate>();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
2024-11-18 23:50:05 +04:00
if (row.Cells["ColumnAdvocate"].Value == null ||
2024-11-09 03:06:08 +04:00
row.Cells["ColumnPost"].Value == null)
{
continue;
}
list.Add(CaseAdvocate.CreateEntity(0,
2024-11-18 23:50:05 +04:00
Convert.ToInt32(row.Cells["ColumnAdvocate"].Value),
2024-11-09 03:06:08 +04:00
row.Cells["ColumnPost"].Value.ToString()??string.Empty));
}
return list;
}
2024-11-05 03:59:13 +04:00
private Case CreateCase(int id) => Case.CreateEntity(id,
2024-11-09 03:06:08 +04:00
(TypeAppeal)typeApellBox.SelectedItem!,
2024-11-05 03:59:13 +04:00
paymentCheckBox.Checked,
priceNumeric.Value,
winPriceNumeric.Value,
verdictCheckBox.Checked,
courtBox.SelectedIndex,
clientBox.SelectedIndex,
textBox1.Text);
}
}