103 lines
3.4 KiB
C#
103 lines
3.4 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using BlogContracts.BusinessLogicContracts;
|
|
using BlogContracts.SearchModels;
|
|
using BlogContracts.BindingModel;
|
|
|
|
namespace BlogViewModel
|
|
{
|
|
public partial class FormUser : Form
|
|
{
|
|
private readonly ILogger _logger;
|
|
private readonly IUserLogic _logic;
|
|
private int? _id;
|
|
public int Id { set { _id = value; } }
|
|
|
|
public FormUser(ILogger<FormUser> logger, IUserLogic logic)
|
|
{
|
|
InitializeComponent();
|
|
_logger = logger;
|
|
_logic = logic;
|
|
}
|
|
|
|
private void FormUser_Load(object sender, EventArgs e)
|
|
{
|
|
if (_id.HasValue)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("Получение компонента");
|
|
|
|
var view = _logic.ReadElement(new UserSearchModel
|
|
{
|
|
Id = _id.Value
|
|
});
|
|
|
|
if (view != null)
|
|
{
|
|
UserNameTextBox.Text = view.Name;
|
|
DateCreateTextBox.Text = view.DateCreate;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка получения компонента");
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
|
|
MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SaveButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (string.IsNullOrEmpty(UserNameTextBox.Text))
|
|
{
|
|
MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
|
|
_logger.LogInformation("Сохранение компонента");
|
|
|
|
try
|
|
{
|
|
var model = new UserBindingModel
|
|
{
|
|
Id = _id ?? 0,
|
|
Name = UserNameTextBox.Text,
|
|
DateCreate = DateCreateTextBox.Text,
|
|
//Cost = Convert.ToDouble(CostTextBox.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();
|
|
}
|
|
}
|
|
}
|