PIBD14-Boyko-M.S.-GSM-Autot.../ProjectGSM/Forms/FormCase.cs
2024-12-17 01:21:56 +04:00

90 lines
3.0 KiB
C#

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