124 lines
3.5 KiB
C#
124 lines
3.5 KiB
C#
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;
|
|
using Unity;
|
|
|
|
namespace ProjectGSM.Forms
|
|
{
|
|
public partial class FormCourts : Form
|
|
{
|
|
private readonly IUnityContainer _container;
|
|
|
|
private readonly ICourtRepository _courtRepository;
|
|
public FormCourts(IUnityContainer container, ICourtRepository courtRepository)
|
|
{
|
|
InitializeComponent();
|
|
_container = container ??
|
|
throw new ArgumentNullException(nameof(container));
|
|
_courtRepository = courtRepository ??
|
|
throw new
|
|
ArgumentNullException(nameof(courtRepository));
|
|
|
|
}
|
|
|
|
private void deleteButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (!TryGetIdentifierFromSelectedRow(out var findId))
|
|
{
|
|
return;
|
|
}
|
|
if (MessageBox.Show("Удалить запись?", "Удаление",
|
|
MessageBoxButtons.YesNo) != DialogResult.Yes)
|
|
{
|
|
return;
|
|
}
|
|
try
|
|
{
|
|
_courtRepository.DeleteCourt(findId);
|
|
LoadList();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при удалении",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
|
|
}
|
|
|
|
private void editButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (!TryGetIdentifierFromSelectedRow(out var findId))
|
|
{
|
|
return;
|
|
}
|
|
try
|
|
{
|
|
var form = _container.Resolve<FormCourt>();
|
|
form.Id = findId;
|
|
form.ShowDialog();
|
|
LoadList();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при изменении",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
|
|
}
|
|
|
|
private void addButton_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
_container.Resolve<FormCourt>().ShowDialog();
|
|
LoadList();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при добавлении",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
|
|
}
|
|
|
|
private void FormClients_Load(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
LoadList();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при загрузке",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
|
|
}
|
|
|
|
private void LoadList() => dataGridView1.DataSource =
|
|
_courtRepository.ReadCourts();
|
|
|
|
private bool TryGetIdentifierFromSelectedRow(out int id)
|
|
{
|
|
id = 0;
|
|
if (dataGridView1.SelectedRows.Count < 1)
|
|
{
|
|
MessageBox.Show("Нет выбранной записи", "Ошибка",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return false;
|
|
}
|
|
id =
|
|
Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["Id"].Value);
|
|
return true;
|
|
}
|
|
|
|
}
|
|
}
|