82 lines
2.2 KiB
C#
82 lines
2.2 KiB
C#
using ProjectTourismCompany.Entities;
|
|
using ProjectTourismCompany.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 ProjectTourismCompany.Forms;
|
|
|
|
public partial class FormCountry : Form
|
|
{
|
|
|
|
private readonly ICountryRepository _countryRepository;
|
|
private int? _countryId;
|
|
public int Id
|
|
{
|
|
set
|
|
{
|
|
try
|
|
{
|
|
var country = _countryRepository.ReadCountryById(value);
|
|
if (country == null)
|
|
{
|
|
throw new InvalidDataException(nameof(country));
|
|
}
|
|
|
|
textBoxName.Text = country.CountryName;
|
|
_countryId = value;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public FormCountry(ICountryRepository countryRepository)
|
|
{
|
|
InitializeComponent();
|
|
_countryRepository = countryRepository ?? throw new ArgumentNullException(nameof(countryRepository));
|
|
}
|
|
|
|
private void buttonSave_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrWhiteSpace(textBoxName.Text))
|
|
{
|
|
throw new Exception("Имеются незаполненные поля");
|
|
}
|
|
if (_countryId.HasValue)
|
|
{
|
|
_countryRepository.UpdateCountry(CreateCountry(_countryId.Value));
|
|
}
|
|
else
|
|
{
|
|
_countryRepository.CreateCountry(CreateCountry(0));
|
|
}
|
|
Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при сохранении",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
private void buttonCancel_Click(object sender, EventArgs e) => Close();
|
|
|
|
private Country CreateCountry(int id)
|
|
{
|
|
return Country.CreateCountry(id, textBoxName.Text);
|
|
}
|
|
}
|