SUBD_Aleikin/Restaurant/RestaurantView/FormClient.cs

90 lines
3.1 KiB
C#

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 FormClient : Form
{
private readonly IClientLogic _clientLogic;
private int? _id;
public int Id { set { _id = value; } }
public FormClient(IClientLogic clientLogic)
{
InitializeComponent();
_clientLogic = clientLogic;
}
private void buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void FormClient_Load(object sender, EventArgs e)
{
if (_id.HasValue)
{
try
{
var view = _clientLogic.ReadElement(new ClientSearchModel
{
Id = _id.Value
});
if (view != null)
{
textBoxFirstName.Text = view.FirstName;
textBoxLastName.Text = view.LastName;
textBoxNumber.Text = view.Number;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
private void buttonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxFirstName.Text) && string.IsNullOrEmpty(textBoxLastName.Text) && string.IsNullOrEmpty(textBoxNumber.Text))
{
MessageBox.Show("Введены не все данные.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
var model = new ClientBindingModel
{
Id = _id ?? 0,
FirstName = textBoxFirstName.Text,
LastName = textBoxLastName.Text,
Number = textBoxNumber.Text
};
var operationResult = _id.HasValue ? _clientLogic.Update(model) : _clientLogic.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);
}
}
}
}