107 lines
4.0 KiB
C#
107 lines
4.0 KiB
C#
|
using Microsoft.Extensions.Logging;
|
|||
|
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;
|
|||
|
using TransportCompanyContracts.BindingModels;
|
|||
|
using TransportCompanyContracts.BusinessLogicsContracts;
|
|||
|
using TransportCompanyContracts.SearchModels;
|
|||
|
|
|||
|
namespace TransportCompanyView
|
|||
|
{
|
|||
|
public partial class FormTransport : Form
|
|||
|
{
|
|||
|
private readonly ILogger _logger;
|
|||
|
private readonly ITransportLogic _logic;
|
|||
|
private int? _id;
|
|||
|
public int Id { set { _id = value; } }
|
|||
|
|
|||
|
public FormTransport(ILogger<FormTransport> logger, ITransportLogic logic)
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
_logger = logger;
|
|||
|
_logic = logic;
|
|||
|
}
|
|||
|
|
|||
|
private void FormTransport_Load(object sender, EventArgs e)
|
|||
|
{
|
|||
|
if (_id.HasValue)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
_logger.LogInformation("Получение пункта");
|
|||
|
var view = _logic.ReadElement(new TransportSearchModel
|
|||
|
{
|
|||
|
Id = _id.Value
|
|||
|
});
|
|||
|
if (view != null)
|
|||
|
{
|
|||
|
textBoxName.Text = view.Model;
|
|||
|
textBoxLoadCapacity.Text = view.LoadCapacity.ToString();
|
|||
|
textBoxStateNumber.Text = view.StateNumber;
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
_logger.LogError(ex, "Ошибка получения транспорта");
|
|||
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void ButtonSave_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(textBoxName.Text))
|
|||
|
{
|
|||
|
MessageBox.Show("Заполните модель", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(textBoxLoadCapacity.Text))
|
|||
|
{
|
|||
|
MessageBox.Show("Заполните вместимость", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(textBoxStateNumber.Text))
|
|||
|
{
|
|||
|
MessageBox.Show("Заполните автомобильный номер", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
_logger.LogInformation("Сохранение пункта");
|
|||
|
try
|
|||
|
{
|
|||
|
var model = new TransportBindingModel
|
|||
|
{
|
|||
|
Id = _id ?? 0,
|
|||
|
Model = textBoxName.Text,
|
|||
|
LoadCapacity = Convert.ToInt32(textBoxLoadCapacity.Text),
|
|||
|
StateNumber = textBoxStateNumber.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();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|