DB/TransportGuide/FormStop.cs

92 lines
2.0 KiB
C#

using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TransportGuideContracts.BusinessLogicsContracts;
using TransportGuideContracts.SearchModels;
using TransportGuideContracts.BindingModels;
namespace TransportGuide
{
public partial class FormStop : Form
{
private readonly ILogger _logger;
private readonly IStopLogic _logic;
private int? _id;
public int Id { set { _id = value; } }
public FormStop(ILogger<FormStop> logger, IStopLogic
logic)
{
InitializeComponent();
_logger = logger;
_logic = logic;
}
private void FormStop_Load(object sender, EventArgs e)
{
if (_id.HasValue)
{
try
{
var view = _logic.ReadElement(new StopSearchModel
{
Id =
_id.Value
});
if (view != null)
{
textBoxName.Text = view.Name;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error with loading", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
private void buttonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxName.Text))
{
MessageBox.Show("Name cant be empty", "error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
var model = new StopBindingModel
{
Id = _id ?? 0,
Name = textBoxName.Text,
};
var operationResult = _id.HasValue ? _logic.Update(model) :
_logic.Create(model);
if (!operationResult)
{
throw new Exception("Error with saving.");
}
MessageBox.Show("Stop was saved", "Saved",
MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}