LabSubd/Subd/Forms/FormHumans.cs

122 lines
3.9 KiB
C#
Raw Permalink Normal View History

2023-05-05 20:47:09 +03:00
using Microsoft.Extensions.Logging;
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 Contracts.BusinessLogic;
using Contracts.BindingModels;
namespace Forms
{
public partial class FormHumans : Form
{
private readonly ILogger _logger;
private readonly IHumanLogic _logic;
public FormHumans(ILogger<FormHumans> logger, IHumanLogic logic)
{
InitializeComponent();
_logger = logger;
_logic = logic;
}
private void FormHumans_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
try
{
var list = _logic.ReadList(null);
if (list != null)
{
DataGridView.DataSource = list;
DataGridView.Columns["Id"].Visible = false;
DataGridView.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
DataGridView.Columns["StatusId"].Visible = false;
}
_logger.LogInformation("Загрузка машин");
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки машин");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void AddButton_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormHuman));
if (service is FormHuman form)
{
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
}
private void ChangeButton_Click(object sender, EventArgs e)
{
if (DataGridView.SelectedRows.Count == 1)
{
var service = Program.ServiceProvider?.GetService(typeof(FormHuman));
if (service is FormHuman form)
{
form.Id = Convert.ToInt32(DataGridView.SelectedRows[0].Cells["Id"].Value);
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
}
}
private void DeleteButton_Click(object sender, EventArgs e)
{
if (DataGridView.SelectedRows.Count == 1)
{
if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
2023-05-06 01:31:48 +03:00
string id = DataGridView.SelectedRows[0].Cells["Phone"].Value.ToString();
2023-05-05 20:47:09 +03:00
_logger.LogInformation("Удаление изделия");
try
{
if (!_logic.Delete(new HumanBM
{
2023-05-06 01:31:48 +03:00
Phone = id
2023-05-05 20:47:09 +03:00
}))
{
throw new Exception("Ошибка при удалении. Дополнительная информация в логах.");
}
LoadData();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления изделия");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private void UpdateButton_Click(object sender, EventArgs e)
{
LoadData();
}
}
}