137 lines
4.7 KiB
C#
137 lines
4.7 KiB
C#
using AircraftPlantContracts.BindingModels;
|
||
using AircraftPlantContracts.BusinessLogicsContracts;
|
||
using AircraftPlantContracts.SearchModels;
|
||
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;
|
||
|
||
namespace AircraftPlantView
|
||
{
|
||
/// <summary>
|
||
/// Форма для работы с компонентами
|
||
/// </summary>
|
||
public partial class FormComponent : Form
|
||
{
|
||
/// <summary>
|
||
/// Логгер
|
||
/// </summary>
|
||
private readonly ILogger _logger;
|
||
|
||
/// <summary>
|
||
/// Бизнес-логика для компонента
|
||
/// </summary>
|
||
private readonly IComponentLogic _logic;
|
||
|
||
/// <summary>
|
||
/// Идентификатор
|
||
/// </summary>
|
||
private int? _id;
|
||
public int Id { set { _id = value; } }
|
||
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="logger"></param>
|
||
/// <param name="logic"></param>
|
||
public FormComponent(ILogger<FormComponent> logger, IComponentLogic logic)
|
||
{
|
||
InitializeComponent();
|
||
_logger = logger;
|
||
_logic = logic;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Загрузка информации о компоненте
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void FormComponent_Load(object sender, EventArgs e)
|
||
{
|
||
if (_id.HasValue)
|
||
{
|
||
try
|
||
{
|
||
_logger.LogInformation("Получение компонента");
|
||
var view = _logic.ReadElement(new ComponentSearchModel
|
||
{
|
||
Id = _id.Value
|
||
});
|
||
if (view != null)
|
||
{
|
||
textBoxComponentName.Text = view.ComponentName;
|
||
textBoxComponentCost.Text = view.Cost.ToString();
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "Ошибка получения компонента");
|
||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Кнопка "Сохранить"
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void buttonSave_Click(object sender, EventArgs e)
|
||
{
|
||
if (string.IsNullOrEmpty(textBoxComponentName.Text))
|
||
{
|
||
MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return;
|
||
}
|
||
if (string.IsNullOrEmpty(textBoxComponentCost.Text))
|
||
{
|
||
MessageBox.Show("Заполните стоимость", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return;
|
||
}
|
||
|
||
_logger.LogInformation("Сохранение компонента");
|
||
try
|
||
{
|
||
var model = new ComponentBindingModel
|
||
{
|
||
Id = _id ?? 0,
|
||
ComponentName = textBoxComponentName.Text,
|
||
Cost = Convert.ToDouble(textBoxComponentCost.Text)
|
||
};
|
||
|
||
var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model);
|
||
if (!operationResult)
|
||
{
|
||
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
|
||
}
|
||
|
||
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
DialogResult = DialogResult.OK;
|
||
Close();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "Ошибка сохранения компонента");
|
||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Кнопка "Отмена"
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void buttonCancel_Click(object sender, EventArgs e)
|
||
{
|
||
DialogResult = DialogResult.Cancel;
|
||
Close();
|
||
}
|
||
}
|
||
}
|