2024-04-05 02:48:52 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using RouteGuideContracts.BindingModels;
|
|
|
|
|
using RouteGuideContracts.BusinessLogicsContracts;
|
|
|
|
|
using RouteGuideContracts.SearchModels;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
namespace RouteGuideView
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Форма для создания/редактирования водителей
|
|
|
|
|
/// </summary>
|
|
|
|
|
public partial class FormDriver : Form
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Логгер
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Бизнес-логика
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IDriverLogic _driverLogic;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Идентификатор
|
|
|
|
|
/// </summary>
|
2024-04-23 19:38:25 +04:00
|
|
|
|
private object? _id;
|
2024-04-05 02:48:52 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Идентификатор
|
|
|
|
|
/// </summary>
|
2024-04-23 19:38:25 +04:00
|
|
|
|
public object Id { set { _id = value; } }
|
2024-04-05 02:48:52 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
public FormDriver(ILogger<FormDriver> logger, IDriverLogic driverLogic)
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_driverLogic = driverLogic;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Загрузка информации о сущности
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void FormDriver_Load(object sender, EventArgs e)
|
|
|
|
|
{
|
2024-04-23 19:38:25 +04:00
|
|
|
|
if (_id == null)
|
2024-04-05 02:48:52 +04:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Получение сущности 'Водитель'");
|
|
|
|
|
var view = _driverLogic.ReadElement(new DriverSearchModel
|
|
|
|
|
{
|
2024-04-23 19:38:25 +04:00
|
|
|
|
Id = _id
|
2024-04-05 02:48:52 +04:00
|
|
|
|
});
|
|
|
|
|
if (view != null)
|
|
|
|
|
{
|
|
|
|
|
textBoxFullName.Text = view.FullName;
|
|
|
|
|
textBoxPhone.Text = view.Phone;
|
|
|
|
|
textBoxExperience.Text = view.Experience.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка получения сущности 'Водитель'");
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Кнопка "Сохранить"
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void buttonSave_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(textBoxFullName.Text))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Заполните ФИО", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(textBoxPhone.Text))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Заполните номер телефона", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Сохранение сущности 'Водитель'");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var model = new DriverBindingModel
|
|
|
|
|
{
|
2024-04-23 19:38:25 +04:00
|
|
|
|
Id = _id,
|
2024-04-05 02:48:52 +04:00
|
|
|
|
FullName = textBoxFullName.Text,
|
|
|
|
|
Phone = textBoxPhone.Text,
|
|
|
|
|
Experience = int.TryParse(textBoxExperience.Text, out int experience) ? experience : 0
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-23 19:38:25 +04:00
|
|
|
|
var operationResult = _id != null ? _driverLogic.Update(model) : _driverLogic.Create(model);
|
2024-04-05 02:48:52 +04:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Кнопка "Отмена"
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void buttonCancel_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
DialogResult = DialogResult.Cancel;
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|