90 lines
3.2 KiB
C#
Raw Normal View History

2024-02-24 21:32:11 +04:00
using DinerContracts.BindingModels;
using DinerContracts.BusinessLogicsContacts;
using DinerContracts.SearchModels;
using Microsoft.Extensions.Logging;
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 DinerView
{
public partial class FormFood : Form
{
private readonly ILogger _logger;
private readonly IFoodLogic _logic;
private int? _ID;
public int ID { set { _ID = value; } }
public FormFood(ILogger<FormFood> logger, IFoodLogic logic)
{
InitializeComponent();
_logger = logger;
_logic = logic;
}
private void FormComponent_Load(object sender, EventArgs e)
{
if (_ID.HasValue)
{
try
{
_logger.LogInformation("Получение продукта");
var view = _logic.ReadElement(new FoodSearchModel { ID = _ID.Value });
if (view != null)
{
textBoxName.Text = view.ComponentName;
textBoxPrice.Text = view.Price.ToString();
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения продукта");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void buttonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxName.Text))
{
MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Сохранение продукта");
try
{
var model = new FoodBindingModel
{
ID = _ID ?? 0,
ComponentName = textBoxName.Text,
Price = Convert.ToDouble(textBoxPrice.Text)
};
var operationResult = _ID.HasValue ? _logic.Update(model) : _logic.Create(model);
if (!operationResult)
throw new Exception("Ошибка при сохранении. Дополнителльная информация в логах.");
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при сохронении продукта");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}