SYBD_2024/viewmodel/FormConstructionMaterial.cs

89 lines
3.0 KiB
C#
Raw Normal View History

using Subd_4.BindingModels;
using Subd_4.BusinessLogicContracts;
using Subd_4.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 viewmodel
{
public partial class FormConstructionMaterial : Form
{
private readonly IConstructionMaterialLogic _logic;
private int? _id;
public int Id { set { _id = value; } }
public FormConstructionMaterial(IConstructionMaterialLogic logic)
{
InitializeComponent();
_logic = logic;
}
private void FormMenu_Load(object sender, EventArgs e)
{
if (_id.HasValue)
{
try
{
var view = _logic.ReadElement(new ConstructionMaterialSearchModel { Id = _id.Value });
if (view != null)
{
textBoxFoodName.Text = view.MaterialName;
textBoxDeskr.Text = view.Cost.ToString();
textBoxPrice.Text = view.Quantity.ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void buttonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxFoodName.Text) || string.IsNullOrEmpty(textBoxPrice.Text))
{
MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
var model = new ConstructionMaterialBindingModel
{
Id = _id ?? 0,
MaterialName = textBoxFoodName.Text,
Cost = Convert.ToInt32(textBoxPrice.Text),
Quantity = Convert.ToInt32(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)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}