84 lines
2.9 KiB
C#
Raw Normal View History

2024-12-23 03:32:54 +04:00
using IT_Company.Entities;
using IT_Company.Entities.Repositories;
using System.Data;
namespace IT_Company.Forms
{
public partial class FormContract : Form
{
private readonly IContractRepository _contractRepository;
private int? _contractId;
public int Id
{
set
{
try
{
var contract = _contractRepository.ReadContractById(value);
if (contract != null)
{
throw new InvalidDataException(nameof(contract));
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
}
public FormContract(IContractRepository contractRepository)
{
InitializeComponent();
_contractRepository = contractRepository ?? throw new ArgumentNullException(nameof(contractRepository));
}
private void buttonSave_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrEmpty(comboBoxExecutors.Text) || string.IsNullOrEmpty(comboBoxOrganizations.Text) || string.IsNullOrEmpty(dataGridViewServices.Text))
{
throw new DataException("Имеются незаполненные поля");
}
_contractRepository.CreateContract(Contract.CreateOperation(0,int.Parse(comboBoxExecutors.Text),int.Parse(comboBoxOrganizations.Text),0,CreateListServicesContractsFromDataGrid()));
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private List<ServicesContracts> CreateListServicesContractsFromDataGrid()
{
{
var list = new List<ServicesContracts>();
foreach (DataGridViewRow row in dataGridViewServices.Rows)
{
if (row.Cells["Service"].Value == null)
{
continue;
}
list.Add(ServicesContracts.CreateEntity(0,
Convert.ToInt32(row.Cells["Service"].Value),DateTime.Now));
}
return list;
}
}
private void buttonCancel_Click(object sender, EventArgs e)
{
Close();
}
private void price_KeyPress(object sender, KeyPressEventArgs e)
{
char c = e.KeyChar;
if (!Char.IsDigit(c) && c != 8)
e.Handled = true;
}
}
}