SUBD_Aleikin/Restaurant/RestaurantView/FormComponent.cs

115 lines
4.1 KiB
C#
Raw Normal View History

using RestaurantBusinessLogic.BusinessLogics;
using RestaurantContracts.BindingModels;
using RestaurantContracts.BusinessLogicsContracts;
using RestaurantContracts.SearchModels;
using RestaurantDataModels.Models;
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 FormComponent : Form
{
private readonly IComponentLogic _componentLogic;
private Dictionary<int, (IProviderModel, int, DateTime)> _componentProviders;
private int? _id;
public int Id { set { _id = value; } }
public FormComponent(IComponentLogic componentLogic)
{
InitializeComponent();
_componentLogic = componentLogic;
_componentProviders = new Dictionary<int, (IProviderModel, int, DateTime)>();
}
private void FormComponent_Load(object sender, EventArgs e)
{
if (_id.HasValue)
{
try
{
var view = _componentLogic.ReadElement(new ComponentSearchModel
{
Id = _id.Value
});
if (view != null)
{
textBoxName.Text = view.Name;
textBoxPrice.Text = view.Price.ToString();
textBoxCount.Text = view.Count.ToString();
_componentProviders = view.ComponentProviders ?? new Dictionary<int, (IProviderModel, int, DateTime)>();
LoadData();
}
}
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(textBoxPrice.Text) && string.IsNullOrEmpty(textBoxCount.Text))
{
MessageBox.Show("Введены не все данные.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
var model = new ComponentBindingModel
{
Id = _id ?? 0,
Name = textBoxName.Text,
Price = int.Parse(textBoxPrice.Text),
Count = int.Parse(textBoxCount.Text)
};
var operationResult = _id.HasValue ? _componentLogic.Update(model) : _componentLogic.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 buttonAdd_Click(object sender, EventArgs e)
{
}
private void LoadData()
{
try
{
if (_componentProviders != null)
{
dataGridView.Rows.Clear();
foreach (var pc in _componentProviders)
{
dataGridView.Rows.Add(new object[] { pc.Key, pc.Value.Item1.Name, pc.Value.Item2, pc.Value.Item3 });
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
}