149 lines
5.1 KiB
C#
149 lines
5.1 KiB
C#
|
using Microsoft.Extensions.Logging;
|
|||
|
using RouteGuideBusinessLogics.BusinessLogics;
|
|||
|
using RouteGuideContracts.BindingModels;
|
|||
|
using RouteGuideContracts.BusinessLogicsContracts;
|
|||
|
using RouteGuideContracts.SearchModels;
|
|||
|
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 RouteGuideView
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Форма для создания/редактирования остановок
|
|||
|
/// </summary>
|
|||
|
public partial class FormStop : Form
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Логгер
|
|||
|
/// </summary>
|
|||
|
private readonly ILogger _logger;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Бизнес-логика
|
|||
|
/// </summary>
|
|||
|
private readonly IStopLogic _stopLogic;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Идентификатор
|
|||
|
/// </summary>
|
|||
|
private int? _id;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Идентификатор
|
|||
|
/// </summary>
|
|||
|
public int Id { set { _id = value; } }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Конструктор
|
|||
|
/// </summary>
|
|||
|
public FormStop(ILogger<FormStop> logger, IStopLogic stopLogic)
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
_logger = logger;
|
|||
|
_stopLogic = stopLogic;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Загрузка информации о сущности
|
|||
|
/// </summary>
|
|||
|
/// <param name="sender"></param>
|
|||
|
/// <param name="e"></param>
|
|||
|
private void FormStop_Load(object sender, EventArgs e)
|
|||
|
{
|
|||
|
if (!_id.HasValue)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
_logger.LogInformation("Получение сущности 'Остановка'");
|
|||
|
var view = _stopLogic.ReadElement(new StopSearchModel
|
|||
|
{
|
|||
|
Id = _id.Value
|
|||
|
});
|
|||
|
if (view != null)
|
|||
|
{
|
|||
|
textBoxName.Text = view.Name;
|
|||
|
textBoxStreet.Text = view.Street;
|
|||
|
textBoxNumber.Text = view.Number.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(textBoxName.Text))
|
|||
|
{
|
|||
|
MessageBox.Show("Заполните название остановки", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(textBoxStreet.Text))
|
|||
|
{
|
|||
|
MessageBox.Show("Заполните название улицы", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(textBoxNumber.Text))
|
|||
|
{
|
|||
|
MessageBox.Show("Заполните номер дома", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
_logger.LogInformation("Сохранение сущности 'Остановка'");
|
|||
|
try
|
|||
|
{
|
|||
|
var model = new StopBindingModel
|
|||
|
{
|
|||
|
Id = _id ?? 0,
|
|||
|
Name = textBoxName.Text,
|
|||
|
Street = textBoxStreet.Text,
|
|||
|
Number = int.TryParse(textBoxNumber.Text, out int number) ? number : 1
|
|||
|
};
|
|||
|
|
|||
|
var operationResult = _id.HasValue ? _stopLogic.Update(model) : _stopLogic.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);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Кнопка "Отмена"
|
|||
|
/// </summary>
|
|||
|
/// <param name="sender"></param>
|
|||
|
/// <param name="e"></param>
|
|||
|
private void buttonCancel_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
DialogResult = DialogResult.Cancel;
|
|||
|
Close();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|