LabSubd/Subd/Forms/FormRoute.cs

144 lines
4.8 KiB
C#

using Contracts.BindingModels;
using Contracts.BusinessLogic;
using Contracts.SearchModel;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Diagnostics;
namespace Forms
{
public partial class FormRoute : Form
{
private readonly ILogger _logger;
private readonly IRouteLogic _logic;
private readonly IPlaceLogic _placeLogic;
private int? _id;
public int Id { set { _id = value; } }
Random rndModel = new Random();
public FormRoute(ILogger<FormRoute> logger, IRouteLogic logic, IPlaceLogic placeLogic)
{
InitializeComponent();
_logger = logger;
_logic = logic;
_placeLogic = placeLogic;
}
private void FormComponent_Load(object sender, EventArgs e)
{
try
{
var places = _placeLogic.ReadList(null);
var places2 = _placeLogic.ReadList(null);
if (places != null && places2 !=null)
{
StartcomboBox.DisplayMember = "Title";
StartcomboBox.ValueMember = "Id";
StartcomboBox.DataSource = places;
StartcomboBox.SelectedItem = null;
EndcomboBox.DisplayMember = "Title";
EndcomboBox.ValueMember = "Id";
EndcomboBox.DataSource = places2;
EndcomboBox.SelectedItem = null;
if (_id.HasValue)
{
_logger.LogInformation("Получение route");
var view = _logic.ReadElement(new RouteSM
{
Id = _id.Value
});
if (view != null)
{
LengthTextBox.Text = view.Length.ToString();
}
}
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения route");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void SaveButton_Click(object sender, EventArgs e)
{
_logger.LogInformation("Сохранение компонента");
try
{
var model = new RouteBM
{
Id = _id ?? 0,
Length = Convert.ToInt32(LengthTextBox.Text),
PlaceStart = Convert.ToInt32(StartcomboBox.SelectedValue),
PlaceStartName = StartcomboBox.SelectedValue.ToString(),
PlaceEnd = Convert.ToInt32(EndcomboBox.SelectedValue),
PlaceEndName = EndcomboBox.SelectedValue.ToString(),
Title = StartcomboBox.Text + " - " + EndcomboBox.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 PlacecomboBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void AddTenbutton_Click(object sender, EventArgs e)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
for (int i = 0; i < 10; i++)
{
var model = new RouteBM
{
Id = _id ?? 0,
Length = rndModel.Next(10,200),
PlaceStart = rndModel.Next(1, _placeLogic.ReadList(null).Count),
PlaceEnd = rndModel.Next(1, _placeLogic.ReadList(null).Count),
};
var operationResult = _logic.Create(model);
}
stopwatch.Stop();
var time = stopwatch.ElapsedMilliseconds;
Timelabel.Text = time.ToString();
}
}
}