using ProjectFuel.Entities; using ProjectFuel.Repositories; 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 ProjectFuel.Forms_ { public partial class FormRoute : Form { private readonly IRouteRepository _routeRepository; private int? _routeId; public int Id { set { try { var route = _routeRepository.ReadRouteByID(value); if (route == null) throw new InvalidOperationException(nameof(route)); textBoxStartPoint.Text = route.Start_Point; textBoxEndPoint.Text = route.End_Point; numericUpDownLength.Value = (decimal)route.Route_Length; _routeId = value; } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } } public FormRoute(IRouteRepository routeRepository) { InitializeComponent(); _routeRepository = routeRepository ?? throw new ArgumentNullException(nameof(routeRepository)); } private void ButtonRouteSave_Click(object sender, EventArgs e) { try { if (string.IsNullOrWhiteSpace(textBoxStartPoint.Text) || string.IsNullOrWhiteSpace(textBoxEndPoint.Text)) throw new Exception("Имеются незаполненные поля"); if (_routeId.HasValue) _routeRepository.UpdateRoute(CreateRoute(_routeId.Value)); else _routeRepository.CreateRoute(CreateRoute(0)); Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void ButtonCancel_Click(object sender, EventArgs e) => Close(); private Route CreateRoute(int id) { return Route.CreateEntity(id, textBoxStartPoint.Text, textBoxEndPoint.Text, (float)numericUpDownLength.Value); } } }