88 lines
2.8 KiB
C#
88 lines
2.8 KiB
C#
using Publication.Entites;
|
|
using Publication.Repositories;
|
|
using Publication.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 Publication.Forms;
|
|
|
|
public partial class FormPublishingHouse : Form
|
|
{
|
|
private readonly IPublisingHouseRepository publisingHouseRepository;
|
|
private int? publishingHouseId;
|
|
|
|
public int Id
|
|
{
|
|
set
|
|
{
|
|
try
|
|
{
|
|
var publishingHouse = publisingHouseRepository.ReadPublishingHouseById(value);
|
|
if (publishingHouse == null)
|
|
{
|
|
throw new InvalidDataException(nameof(publishingHouse));
|
|
}
|
|
textBoxTitle.Text = publishingHouse.Title;
|
|
textBoxAddress.Text = publishingHouse.Address;
|
|
textBoxWorkPhone.Text = publishingHouse.WorkPhone;
|
|
publishingHouseId = value;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
public FormPublishingHouse(IPublisingHouseRepository _publisingHouseRepository)
|
|
{
|
|
publisingHouseRepository = _publisingHouseRepository ?? throw new ArgumentNullException(nameof(_publisingHouseRepository));
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void FormPublishingHouse_Load(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void buttonBreak_Click(object sender, EventArgs e) => Close();
|
|
|
|
private void buttonSave_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrWhiteSpace(textBoxAddress.Text) ||
|
|
string.IsNullOrWhiteSpace(textBoxTitle.Text) ||
|
|
string.IsNullOrWhiteSpace(textBoxWorkPhone.Text))
|
|
{
|
|
throw new Exception("Имеются незаполненные поля");
|
|
}
|
|
if (publishingHouseId.HasValue)
|
|
{
|
|
|
|
publisingHouseRepository.UpdatePublishingHouse(CreatePublishingHouse(publishingHouseId.Value));
|
|
}
|
|
else
|
|
{
|
|
|
|
publisingHouseRepository.CreatePublishingHouse(CreatePublishingHouse(0));
|
|
}
|
|
|
|
Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
private PublishingHouse CreatePublishingHouse(int id)=>PublishingHouse.CreateEntity(id,textBoxTitle.Text,textBoxAddress.Text,textBoxWorkPhone.Text);
|
|
}
|