PIbd-22_Shabunov_O.A._SushiBar/SushiBarView/Forms/FormDish.cs
2024-05-24 01:56:34 +04:00

206 lines
6.9 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using SushiBarBusinessLogic;
using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
using SushiBarDataModels.Models;
namespace SushiBarView.Forms
{
public partial class FormDish : Form
{
private readonly DishLogic _dishLogic;
private int? _id;
private Dictionary<int, (IIngredientModel, int)> _dishIngredients;
public int Id { set { _id = value; } }
public FormDish(DishLogic DishLogic)
{
InitializeComponent();
_dishLogic = DishLogic;
_dishIngredients = new Dictionary<int, (IIngredientModel, int)>();
}
private void FormDish_Load(object sender, EventArgs e)
{
if (_id.HasValue)
{
try
{
var View = _dishLogic.ReadElement(new DishSearchModel
{
Id = _id.Value
});
if (View != null)
{
NameTextBox.Text = View.DishName;
CategoryTextBox.Text = View.Category;
PriceTextBox.Text = View.Price.ToString();
_dishIngredients = View.DishIngredients ?? new Dictionary<int, (IIngredientModel, int)>();
LoadData();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void LoadData()
{
try
{
if (_dishIngredients != null)
{
DataGridView.Rows.Clear();
foreach (var Ingredient in _dishIngredients)
{
DataGridView.Rows.Add(new object[] { Ingredient.Key, Ingredient.Value.Item1.IngredientName, Ingredient.Value.Item2 });
}
PriceTextBox.Text = CalcPrice().ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void AddButton_Click(object sender, EventArgs e)
{
var Form = new FormDishIngredient(new IngredientLogic(new SushiBarDatabaseImplement.Storages.IngredientStorage()));
if (Form.ShowDialog() == DialogResult.OK)
{
if (Form.IngredientModel == null)
{
return;
}
if (_dishIngredients.ContainsKey(Form.Id))
{
_dishIngredients[Form.Id] = (Form.IngredientModel, Form.Count);
}
else
{
_dishIngredients.Add(Form.Id, (Form.IngredientModel, Form.Count));
}
LoadData();
}
}
private void UpdateButton_Click(object sender, EventArgs e)
{
if (DataGridView.SelectedRows.Count == 1)
{
var Form = new FormDishIngredient(new IngredientLogic(new SushiBarDatabaseImplement.Storages.IngredientStorage()));
int id = Convert.ToInt32(DataGridView.SelectedRows[0].Cells[0].Value);
Form.Id = id;
Form.Count = _dishIngredients[id].Item2;
if (Form.ShowDialog() == DialogResult.OK)
{
if (Form.IngredientModel == null)
{
return;
}
_dishIngredients[Form.Id] = (Form.IngredientModel, Form.Count);
LoadData();
}
}
}
private void DeleteButton_Click(object sender, EventArgs e)
{
if (DataGridView.SelectedRows.Count == 1)
{
if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
try
{
_dishIngredients?.Remove(Convert.ToInt32(DataGridView.SelectedRows[0].Cells[0].Value));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
LoadData();
}
}
}
private void RefreshButton_Click(object sender, EventArgs e)
{
LoadData();
}
private void SaveButton_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(NameTextBox.Text))
{
MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(PriceTextBox.Text))
{
MessageBox.Show("Заполните цену", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (_dishIngredients == null || _dishIngredients.Count == 0)
{
MessageBox.Show("Заполните компоненты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
var Мodel = new DishBindingModel
{
Id = _id ?? 0,
DishName = NameTextBox.Text,
Category = CategoryTextBox.Text,
Price = Convert.ToDouble(PriceTextBox.Text),
DishIngredients = _dishIngredients,
};
var OperationResult = _id.HasValue ? _dishLogic.Update(Мodel) : _dishLogic.Create(Мodel);
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 double CalcPrice()
{
double Price = 0;
foreach (var Elem in _dishIngredients)
{
Price += ((Elem.Value.Item1?.Cost ?? 0) * Elem.Value.Item2);
}
return Math.Round(Price * 1.1, 2);
}
}
}