89 lines
3.2 KiB
C#
89 lines
3.2 KiB
C#
using LawFirmContracts.BindingModels;
|
|
using LawFirmContracts.BusinessLogicsContracts;
|
|
using LawFirmContracts.SearchModels;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace LawFirmView
|
|
{
|
|
public partial class FormBlank : Form
|
|
{
|
|
private readonly ILogger _logger;
|
|
private readonly IBlankLogic _logic;
|
|
private int? _id;
|
|
public int Id { set { _id = value; } }
|
|
public FormBlank(ILogger<FormBlank> logger, IBlankLogic logic)
|
|
{
|
|
InitializeComponent();
|
|
_logger = logger;
|
|
_logic = logic;
|
|
}
|
|
private void FormBlank_Load(object sender, EventArgs e)
|
|
{
|
|
if (_id.HasValue)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("Получение бланка");
|
|
var view = _logic.ReadElement(new BlankSearchModel
|
|
{
|
|
Id = _id.Value
|
|
});
|
|
if (view != null)
|
|
{
|
|
textBoxName.Text = view.BlankName;
|
|
textBoxPrice.Text = view.Price.ToString();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка получения бланка");
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
|
|
MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
}
|
|
private void ButtonSave_Click(object sender, EventArgs e)
|
|
{
|
|
if (string.IsNullOrEmpty(textBoxName.Text))
|
|
{
|
|
MessageBox.Show("Заполните название", "Ошибка",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
_logger.LogInformation("Сохранение бланка");
|
|
try
|
|
{
|
|
var model = new BlankBindingModel
|
|
{
|
|
Id = _id ?? 0,
|
|
BlankName = textBoxName.Text,
|
|
Price = Convert.ToDouble(textBoxPrice.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);
|
|
}
|
|
}
|
|
private void ButtonCancel_Click(object sender, EventArgs e)
|
|
{
|
|
DialogResult = DialogResult.Cancel;
|
|
Close();
|
|
}
|
|
}
|
|
}
|