using ProjectGSM.Entities; using ProjectGSM.Repositories; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ProjectGSM.Forms { public partial class FormClient : Form { private readonly IClientRepository _clientRepository; private int? _clientId; public int Id { set { try { var client = _clientRepository.ReadClientById(value); if (client == null) { throw new InvalidDataException(nameof(client)); } nameTextBox.Text = client.Name; emailTextBox.Text = client.Email; sexCheckBox.Checked = client.Sex; dateTimePicker.Value = new DateTime(client.DateOfBirth.Year, client.DateOfBirth.Month, client.DateOfBirth.Day); phoneText.Text = client.PhoneNumber; adressBox.Text = client.Address; } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } } public FormClient(IClientRepository clientRepository) { InitializeComponent(); _clientRepository = clientRepository ?? throw new ArgumentNullException(nameof(clientRepository)); } private void saveButton_Click(object sender, EventArgs e) { try { if (string.IsNullOrWhiteSpace(nameTextBox.Text) || string.IsNullOrWhiteSpace(emailTextBox.Text) || string.IsNullOrWhiteSpace(phoneText.Text) || string.IsNullOrWhiteSpace(adressBox.Text)) { throw new Exception("Имеются незаполненные поля"); } if (_clientId.HasValue) { _clientRepository.UpdateClient(CreateClient(_clientId.Value)); } else { _clientRepository.UpdateClient(CreateClient(0)); } Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void cancelButton_Click(object sender, EventArgs e) => Close(); private Client CreateClient(int id) => Client.CreateEntity(id, nameTextBox.Text, sexCheckBox.Checked, dateTimePicker.Value, emailTextBox.Text, phoneText.Text, adressBox.Text); } }