PIbd-22_Petrushin_E.A._LawFirm/LawFirm/LawFirmView/FormComponent.cs

101 lines
3.5 KiB
C#
Raw Normal View History

2024-02-27 19:44:44 +04:00
using AbstractLawFirmContracts.BindingModels.BindingModels;
using AbstractLawFirmContracts.BusinessLogicsContracts;
using AbstractLawFirmContracts.SearchModels;
2024-02-11 18:24:54 +04:00
using Microsoft.Extensions.Logging;
2024-02-27 19:44:44 +04:00
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;
2024-02-11 18:24:54 +04:00
2024-02-14 00:11:21 +04:00
namespace LawFirmView
2024-02-11 18:24:54 +04:00
{
public partial class FormComponent : Form
{
private readonly ILogger _logger;
private readonly IComponentLogic _logic;
private int? _id;
public int Id { set { _id = value; } }
public FormComponent(ILogger<FormComponent> logger, IComponentLogic logic)
{
InitializeComponent();
_logger = logger;
_logic = logic;
}
private void FormComponent_Load(object sender, EventArgs e)
{
if (_id.HasValue)
{
try
{
_logger.LogInformation("Получение компонента");
2024-02-27 19:44:44 +04:00
var view = _logic.ReadElement(new ComponentSearchModel
{
Id =
_id.Value
});
2024-02-11 18:24:54 +04:00
if (view != null)
{
textBoxName.Text = view.ComponentName;
textBoxCost.Text = view.Cost.ToString();
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения компонента");
2024-02-27 19:44:44 +04:00
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
2024-02-11 18:24:54 +04:00
}
}
}
2024-02-27 19:44:44 +04:00
private void buttonSave_Click(object sender, EventArgs e)
2024-02-11 18:24:54 +04:00
{
if (string.IsNullOrEmpty(textBoxName.Text))
{
2024-02-27 19:44:44 +04:00
MessageBox.Show("Заполните название", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
2024-02-11 18:24:54 +04:00
return;
}
_logger.LogInformation("Сохранение компонента");
try
{
var model = new ComponentBindingModel
{
Id = _id ?? 0,
ComponentName = textBoxName.Text,
Cost = Convert.ToDouble(textBoxCost.Text)
};
2024-02-27 19:44:44 +04:00
var operationResult = _id.HasValue ? _logic.Update(model) :
_logic.Create(model);
2024-02-11 18:24:54 +04:00
if (!operationResult)
{
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
}
2024-02-27 19:44:44 +04:00
MessageBox.Show("Сохранение прошло успешно", "Сообщение",
MessageBoxButtons.OK, MessageBoxIcon.Information);
2024-02-11 18:24:54 +04:00
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка сохранения компонента");
2024-02-27 19:44:44 +04:00
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
2024-02-11 18:24:54 +04:00
}
2024-02-27 19:44:44 +04:00
2024-02-11 18:24:54 +04:00
}
2024-02-27 19:44:44 +04:00
private void buttonCancel_Click(object sender, EventArgs e)
2024-02-11 18:24:54 +04:00
{
DialogResult = DialogResult.Cancel;
Close();
}
}
2024-02-27 19:44:44 +04:00
}