85 lines
2.8 KiB
C#

using ProjectOptika.Scripts.Entities;
using ProjectOptika.Scripts.Entities.Enums;
using ProjectOptika.Scripts.Repositories;
namespace ProjectOptika.Scripts.Forms
{
public partial class FormClient : Form
{
private readonly IClientRepositiory _clientRepositories;
private int? _clientID;
public int ID
{
set
{
try
{
var client = _clientRepositories.GetClientById(value);
if (client == null) throw new InvalidDataException(nameof(client));
_clientID = client.ID;
textBoxFirstName.Text = client.FirstName;
textBoxSecondName.Text = client.SecondName;
textBoxSurname.Text = client.Surname;
textBoxPhoneNumber.Text = client.PhoneNumber;
comboBoxClientType.SelectedIndex = (int)client.ClientType;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
}
public FormClient(IClientRepositiory clientRepositories)
{
InitializeComponent();
_clientRepositories = clientRepositories ??
throw new ArgumentNullException(nameof(clientRepositories));
comboBoxClientType.DataSource = Enum.GetValues(typeof(ClientType));
}
private void ButtonSave_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrEmpty(textBoxSecondName.Text) || string.IsNullOrEmpty(textBoxSurname.Text) || string.IsNullOrEmpty(textBoxFirstName.Text))
{
throw new Exception("Имеются незаполненные данные");
}
if (_clientID.HasValue)
{
_clientRepositories.UpdateClient(CreateClient(_clientID.Value));
}
else
{
_clientRepositories.CreateClient(CreateClient(0));
}
Close();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при получении сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
Close();
}
private Client CreateClient(int id)
{
return Client.CreateEntity(id, (ClientType)comboBoxClientType.SelectedIndex, textBoxFirstName.Text, textBoxSecondName.Text, textBoxSurname.Text, textBoxPhoneNumber.Text);
}
}
}