SUBD_Aleikin/Restaurant/RestaurantView/FormProvider.cs

89 lines
3.0 KiB
C#

using RestaurantBusinessLogic.BusinessLogics;
using RestaurantContracts.BindingModels;
using RestaurantContracts.BusinessLogicsContracts;
using RestaurantContracts.SearchModels;
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 RestaurantView
{
public partial class FormProvider : Form
{
private readonly IProviderLogic _providerLogic;
private int? _id;
public int Id { set { _id = value; } }
public FormProvider(IProviderLogic providerLogic)
{
InitializeComponent();
_providerLogic = providerLogic;
}
private void FormProvider_Load(object sender, EventArgs e)
{
if (_id.HasValue)
{
try
{
var view = _providerLogic.ReadElement(new ProviderSearchModel
{
Id = _id.Value
});
if (view != null)
{
textBoxName.Text = view.Name;
textBoxAddress.Text = view.Address;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
private void buttonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxName.Text) && string.IsNullOrEmpty(textBoxAddress.Text))
{
MessageBox.Show("Введены не все данные.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
var model = new ProviderBindingModel
{
Id = _id ?? 0,
Name = textBoxName.Text,
Address = textBoxAddress.Text,
};
var operationResult = _id.HasValue ? _providerLogic.Update(model) : _providerLogic.Create(model);
if (!operationResult)
{
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
}
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}