PIBD14-Boyko-M.S.-GSM-Autot.../ProjectGSM/Forms/FormClient.cs
2024-11-18 23:50:05 +04:00

99 lines
3.0 KiB
C#

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));
}
_clientId = value;
nameTextBox.Text = client.Name;
emailTextBox.Text = client.Email;
sexCheckBox.Checked = client.Sex;
dateTimePicker.Value = client.DateOfBirth;
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.CreateClient(CreateClient(0));
}
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, $"Ошибка при сохранении {_clientId}",
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);
}
}