PIbd-23_Abazov_A.A._Constru.../ConstructionCompany/ConstructionCompanyView/FormEmployee.cs

111 lines
4.3 KiB
C#
Raw Permalink Normal View History

using ConstructionCompanyContracts.BindingModels;
using ConstructionCompanyContracts.BusinessLogicContracts;
using ConstructionCompanyContracts.SearchModels;
using ConstructionCompanyContracts.ViewModels;
using ConstructionCompanyPsqlImplement.Models;
using Microsoft.Extensions.Logging;
using System;
using System.Collections;
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 FormEmployee : Form
{
private readonly ILogger _logger;
private readonly IPositionLogic _position;
private readonly IEmployeeLogic _employee;
private List<PositionViewModel>? _list;
private int? _id;
public int Id { set { _id = value; } }
public FormEmployee(ILogger<FormEmployee> logger, IPositionLogic position, IEmployeeLogic employee)
{
InitializeComponent();
_logger = logger;
_position = position;
_employee = employee;
}
private void FormEmployee_Load(object sender, EventArgs e)
{
_logger.LogInformation("Загрузка материалов для заказа");
_list = _position.ReadList(null);
if (_list != null)
{
comboBoxPosition.DisplayMember = "PositionName";
comboBoxPosition.ValueMember = "Id";
comboBoxPosition.DataSource = _list;
comboBoxPosition.SelectedItem = null;
}
if (_id.HasValue)
{
try
{
_logger.LogInformation("Получение материала");
var view = _employee.ReadElement(new EmployeeSearchModel { Id = _id.Value });
if (view != null)
{
textBoxName.Text = view.EmployeeName;
comboBoxPosition.SelectedValue = view.PositionID;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения материала");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void buttonCreate_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxName.Text))
{
MessageBox.Show("Заполните ФИО", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxPosition.SelectedValue == null)
{
MessageBox.Show("Выбирете должность", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Сохранение материала");
try
{
var model = new EmployeeBindingModel
{
Id = _id ?? 0,
EmployeeName = textBoxName.Text,
PositionID = Convert.ToInt32(comboBoxPosition.SelectedValue)
};
var operationResult = _id.HasValue ? _employee.Update(model) : _employee.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();
}
}
}