111 lines
3.1 KiB
C#
111 lines
3.1 KiB
C#
using Publication.Repositories;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Net.Mail;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using Unity;
|
|
|
|
namespace Publication.Forms;
|
|
|
|
public partial class FormCustomers : Form
|
|
{
|
|
private readonly IUnityContainer container;
|
|
private readonly ICustomerRepository customerRepository;
|
|
public FormCustomers(IUnityContainer _unityContainer, ICustomerRepository _customerRepository)
|
|
{
|
|
container = _unityContainer ?? throw new ArgumentNullException(nameof(_unityContainer));
|
|
customerRepository = _customerRepository ?? throw new ArgumentNullException(nameof(_customerRepository));
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void buttonAdd_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
container.Resolve<FormCustomer>().ShowDialog();
|
|
LoadList();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при добавлении", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
private void buttonEdit_Click(object sender, EventArgs e)
|
|
{
|
|
if (!TryGetIdentifierFromSelectedRow(out var findId))
|
|
{
|
|
return;
|
|
}
|
|
try
|
|
{
|
|
var form = container.Resolve<FormCustomer>();
|
|
form.Id = findId;
|
|
form.ShowDialog();
|
|
LoadList();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при изменении", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
private void buttonDelete_Click(object sender, EventArgs e)
|
|
{
|
|
if (!TryGetIdentifierFromSelectedRow(out var findId))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (MessageBox.Show("Удалить запись?", "Удаление", MessageBoxButtons.YesNo) != DialogResult.Yes)
|
|
{
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
customerRepository.DeleteCustomer(findId);
|
|
LoadList();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при удалении", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
private void FormCustomers_Load(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
LoadList();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при загрузке",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
private bool TryGetIdentifierFromSelectedRow(out int id)
|
|
{
|
|
id = 0;
|
|
if (dataGridView.SelectedRows.Count < 1)
|
|
{
|
|
MessageBox.Show("Нет выбранной записи", "Ошибка",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return false;
|
|
}
|
|
|
|
id =
|
|
Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
|
|
return true;
|
|
}
|
|
|
|
private void LoadList() => dataGridView.DataSource = customerRepository.ReadCustomers();
|
|
}
|