96 lines
3.6 KiB
C#
96 lines
3.6 KiB
C#
|
using ConstructionCompanyContracts.BindingModels;
|
|||
|
using ConstructionCompanyContracts.BusinessLogicContracts;
|
|||
|
using ConstructionCompanyContracts.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 ConstructionCompanyView
|
|||
|
{
|
|||
|
public partial class FormPosition : Form
|
|||
|
{
|
|||
|
private readonly ILogger _logger;
|
|||
|
private readonly IPositionLogic _logic;
|
|||
|
private int? _id;
|
|||
|
public int Id { set { _id = value; } }
|
|||
|
public FormPosition(ILogger<FormPosition> logger, IPositionLogic logic)
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
_logic = logic;
|
|||
|
_logger = logger;
|
|||
|
}
|
|||
|
|
|||
|
private void buttonCreate_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(textBoxName.Text))
|
|||
|
{
|
|||
|
MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(textBoxSalary.Text))
|
|||
|
{
|
|||
|
MessageBox.Show("Заполните зарплату", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
_logger.LogInformation("Сохранение материала");
|
|||
|
try
|
|||
|
{
|
|||
|
var model = new PositionBindingModel
|
|||
|
{
|
|||
|
Id = _id ?? 0,
|
|||
|
PositionName = textBoxName.Text,
|
|||
|
Salary = Convert.ToInt32(textBoxSalary.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 FormPosition_Load(object sender, EventArgs e)
|
|||
|
{
|
|||
|
if (_id.HasValue)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
_logger.LogInformation("Получение материала");
|
|||
|
var view = _logic.ReadElement(new PositionSearchModel { Id = _id.Value });
|
|||
|
if (view != null)
|
|||
|
{
|
|||
|
textBoxName.Text = view.PositionName;
|
|||
|
textBoxSalary.Text = view.Salary.ToString();
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
_logger.LogError(ex, "Ошибка получения материала");
|
|||
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|