SUBD_Aleikin/Restaurant/RestaurantView/FormComponent.cs

190 lines
6.8 KiB
C#

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 List<(IProviderModel, int, DateTime)> _componentProviders;
private int? _id;
public int Id { set { _id = value; } }
public FormComponent(IComponentLogic componentLogic)
{
InitializeComponent();
_componentLogic = componentLogic;
_componentProviders = new List<(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 List<(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)
{
var service = Program.ServiceProvider?.GetService(typeof(FormComponentProvider));
if (service is FormComponentProvider form)
{
if (form.ShowDialog() == DialogResult.OK)
{
if (form.ProviderModel == null)
{
return;
}
_componentProviders.Add((form.ProviderModel, form.Count, form.Date));
LoadData();
}
}
}
private void ButtonUpd_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
var service =
Program.ServiceProvider?.GetService(typeof(FormComponentProvider));
if (service is FormComponentProvider form)
{
int id =
Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value);
form.Id = id;
form.Count = _componentProviders[id].Item2;
form.Date = _componentProviders[id].Item3;
if (form.ShowDialog() == DialogResult.OK)
{
if (form.ProviderModel == null)
{
return;
}
_componentProviders[form.Id] = (form.ProviderModel, form.Count, form.Date);
LoadData();
}
}
}
}
private void ButtonDel_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
if (MessageBox.Show("Удалить запись?", "Вопрос",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
try
{
_componentProviders?.Remove(((IProviderModel, int, DateTime))dataGridView.SelectedRows[0].Cells[0].Value);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
LoadData();
}
}
}
private void ButtonRef_Click(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
try
{
if (_componentProviders != null)
{
dataGridView.Rows.Clear();
foreach (var pc in _componentProviders)
{
dataGridView.Rows.Add(new object[] { pc.Item1.Name, pc.Item2, pc.Item3 });
}
textBoxCount.Text = CalcCount().ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private double CalcCount()
{
double count = 0;
foreach (var elem in _componentProviders)
{
count += elem.Item2;
}
return count;
}
}
}