177 lines
6.5 KiB
C#
177 lines
6.5 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 FormSchedule : Form
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Логгер
|
|||
|
/// </summary>
|
|||
|
private readonly ILogger _logger;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Бизнес-логика для расписаний
|
|||
|
/// </summary>
|
|||
|
private readonly IScheduleLogic _scheduleLogic;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Бизнес-логика для маршрутов
|
|||
|
/// </summary>
|
|||
|
private readonly IRouteLogic _routeLogic;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Идентификатор
|
|||
|
/// </summary>
|
|||
|
private int? _id;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Идентификатор
|
|||
|
/// </summary>
|
|||
|
public int Id { set { _id = value; } }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Констурктор
|
|||
|
/// </summary>
|
|||
|
/// <param name="logger"></param>
|
|||
|
/// <param name="scheduleLogic"></param>
|
|||
|
/// <param name="routeLogic"></param>
|
|||
|
public FormSchedule(ILogger<FormSchedule> logger, IScheduleLogic scheduleLogic, IRouteLogic routeLogic)
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
_logger = logger;
|
|||
|
_scheduleLogic = scheduleLogic;
|
|||
|
_routeLogic = routeLogic;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Загрузка данных
|
|||
|
/// </summary>
|
|||
|
/// <param name="sender"></param>
|
|||
|
/// <param name="e"></param>
|
|||
|
private void FormSchedule_Load(object sender, EventArgs e)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
// Загрузка маршрутов для ComboBoxRoute
|
|||
|
_logger.LogInformation("Загрузка списка маршрутов");
|
|||
|
var list = _routeLogic.ReadList(null);
|
|||
|
if (list != null)
|
|||
|
{
|
|||
|
comboBoxRoute.DisplayMember = "Name";
|
|||
|
comboBoxRoute.ValueMember = "Id";
|
|||
|
comboBoxRoute.DataSource = list;
|
|||
|
comboBoxRoute.SelectedItem = null;
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
_logger.LogError(ex, "Ошибка загрузки списка маршрутов");
|
|||
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
}
|
|||
|
|
|||
|
if (!_id.HasValue)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
// Заполнение полей
|
|||
|
_logger.LogInformation("Получение сущности 'Расписание'");
|
|||
|
var view = _scheduleLogic.ReadElement(new ScheduleSearchModel
|
|||
|
{
|
|||
|
Id = _id.Value
|
|||
|
});
|
|||
|
if (view != null)
|
|||
|
{
|
|||
|
comboBoxRoute.SelectedItem = view.RouteId;
|
|||
|
dateTimePickerDate.Value = view.Date;
|
|||
|
dateTimePickerDepartureTime.Value = view.DepartureTime;
|
|||
|
dateTimePickerArrivalTime.Value = view.ArrivalTime;
|
|||
|
dateTimePickerFrequency.Value = view.Frequency;
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex1)
|
|||
|
{
|
|||
|
_logger.LogError(ex1, "Ошибка получения сущности 'Расписание'");
|
|||
|
MessageBox.Show(ex1.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Кнопка "Сохранить"
|
|||
|
/// </summary>
|
|||
|
/// <param name="sender"></param>
|
|||
|
/// <param name="e"></param>
|
|||
|
private void buttonSave_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
if (comboBoxRoute.SelectedValue == null)
|
|||
|
{
|
|||
|
MessageBox.Show("Выберите маршрут", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
if (dateTimePickerDepartureTime.Value >= dateTimePickerArrivalTime.Value)
|
|||
|
{
|
|||
|
MessageBox.Show("Время прибытия должно опережать время отправления", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
_logger.LogInformation("Сохранение сущности 'Расписание'");
|
|||
|
try
|
|||
|
{
|
|||
|
var model = new ScheduleBindingModel
|
|||
|
{
|
|||
|
Id = _id ?? 0,
|
|||
|
Date = dateTimePickerDate.Value,
|
|||
|
DepartureTime = dateTimePickerDepartureTime.Value,
|
|||
|
ArrivalTime = dateTimePickerArrivalTime.Value,
|
|||
|
Frequency = dateTimePickerFrequency.Value,
|
|||
|
RouteId = Convert.ToInt32(comboBoxRoute.SelectedValue)
|
|||
|
};
|
|||
|
|
|||
|
var operationResult = _id.HasValue ? _scheduleLogic.Update(model) : _scheduleLogic.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();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|