105 lines
3.6 KiB
C#
105 lines
3.6 KiB
C#
using BookShopContracts.BindingModels;
|
|
using BookShopContracts.BusinessLogicsContracts;
|
|
using BookShopContracts.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 BookShopView
|
|
{
|
|
public partial class FormAuthor : Form
|
|
{
|
|
private readonly ILogger _logger;
|
|
private readonly IAuthorLogic _logic;
|
|
private int? _id;
|
|
public int Id { set { _id = value; } }
|
|
|
|
public FormAuthor(ILogger<FormAuthor> logger, IAuthorLogic logic)
|
|
{
|
|
InitializeComponent();
|
|
_logger = logger;
|
|
_logic = logic;
|
|
}
|
|
|
|
private void ButtonSave_Click(object sender, EventArgs e)
|
|
{
|
|
if (string.IsNullOrEmpty(FTextBox.Text))
|
|
{
|
|
MessageBox.Show("Заполните фамилию", "Ошибка",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
if (string.IsNullOrEmpty(ITextBox.Text))
|
|
{
|
|
MessageBox.Show("Заполните имя", "Ошибка",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
_logger.LogInformation("Сохранение автора");
|
|
try
|
|
{
|
|
var model = new AuthorBindingModel
|
|
{
|
|
Id = _id ?? 0,
|
|
AuthorName = ITextBox.Text,
|
|
AuthorSurname = FTextBox.Text,
|
|
AuthorPatronymic = OTextBox.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();
|
|
}
|
|
|
|
private void FormAuthor_Load(object sender, EventArgs e)
|
|
{
|
|
if (_id.HasValue)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("Получение автора");
|
|
var view = _logic.ReadElement(new AuthorSearchModel
|
|
{
|
|
Id = _id.Value
|
|
});
|
|
if (view != null)
|
|
{
|
|
FTextBox.Text = view.AuthorSurname;
|
|
ITextBox.Text = view.AuthorName;
|
|
OTextBox.Text = view.AuthorPatronymic;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка получения автора");
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|