using ProjectGarage.Entities; using ProjectGarage.Entities.Enums; using ProjectGarage.Repositories; using ProjectGarage.Repositories.Implementations; 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 ProjectGarage.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 InvalidDataException(nameof(route)); } textBoxRouteName.Text = route.RouteName; textBoxRouteStart.Text = route.StartP; textBoxRouteFinal.Text = route.FinalP; numericUpDownRouteLen.Value = 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(textBoxRouteStart.Text) || string.IsNullOrWhiteSpace(textBoxRouteFinal.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 ButtonRouteFinal_Click(object sender, EventArgs e) => Close(); private Route CreateRoute(int id) => Route.CreateRoute(id, textBoxRouteName.Text, textBoxRouteStart.Text, textBoxRouteFinal.Text, Convert.ToInt32(numericUpDownRouteLen.Value)); } }