136 lines
5.0 KiB
C#
136 lines
5.0 KiB
C#
using BusinessLogic.BusinessLogic;
|
||
using Contracts.BindingModels;
|
||
using Contracts.BusinessLogicContracts;
|
||
using Contracts.SearchModels;
|
||
using Microsoft.Extensions.Logging;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Data;
|
||
using System.Diagnostics;
|
||
using System.Drawing;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Forms;
|
||
|
||
namespace WinFormsApp
|
||
{
|
||
public partial class FormProducts : Form
|
||
{
|
||
private Guid? _id;
|
||
private readonly ILogger _logger;
|
||
private readonly IProductLogic _productLogic;
|
||
public FormProducts(ILogger<FormMain> logger, IProductLogic productLogic)
|
||
{
|
||
InitializeComponent();
|
||
_productLogic = productLogic;
|
||
_logger = logger;
|
||
}
|
||
|
||
private void FormProducts_Load(object sender, EventArgs e)
|
||
{
|
||
LoadData();
|
||
groupBoxCreateProduct.Hide();
|
||
//groupBoxCreateProduct.Enabled = false;
|
||
}
|
||
|
||
private void LoadData()
|
||
{
|
||
_logger.LogInformation("Загрузка товаров");
|
||
try
|
||
{
|
||
var list = _productLogic.ReadList(null);
|
||
if (list != null)
|
||
{
|
||
dataGridView.DataSource = list;
|
||
}
|
||
_logger.LogInformation("Загрузка товаров");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "Ошибка загрузки товаров");
|
||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
|
||
private void buttonCreateProduct_Click(object sender, EventArgs e)
|
||
{
|
||
//надо сделать че нибудь с фронтом а то всё грустно
|
||
groupBoxControls.Hide();
|
||
//groupBoxControls.Enabled = false;
|
||
groupBoxCreateProduct.Show();
|
||
//groupBoxCreateProduct.Enabled = true;
|
||
}
|
||
|
||
private void buttonSaveProduct_Click(object sender, EventArgs e)
|
||
{
|
||
if (string.IsNullOrEmpty(textBoxName.Text))
|
||
{
|
||
MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return;
|
||
}
|
||
if (string.IsNullOrEmpty(numericUpDownPrice.Value.ToString()))
|
||
{
|
||
MessageBox.Show("Укажите цену", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return;
|
||
}
|
||
if (string.IsNullOrEmpty(numericUpDownAmount.Value.ToString()))
|
||
{
|
||
MessageBox.Show("Укажите кол-во", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return;
|
||
}
|
||
_logger.LogInformation("Сохранение товара");
|
||
try
|
||
{
|
||
var model = new ProductBindingModel
|
||
{
|
||
Name = textBoxName.Text,
|
||
Price = Convert.ToDouble(numericUpDownPrice.Value),
|
||
Amount = Convert.ToInt16(numericUpDownAmount.Value),
|
||
Rate = 0.0,
|
||
IsBeingSold = checkBoxIsSold.Checked,
|
||
};
|
||
var operationResult = _productLogic.Create(model);
|
||
if (!operationResult)
|
||
{
|
||
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
|
||
}
|
||
MessageBox.Show("Сохранение прошло успешно", "Сообщение",
|
||
MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
groupBoxControls.Enabled = true;
|
||
groupBoxControls.Show();
|
||
groupBoxCreateProduct.Enabled = false;
|
||
groupBoxCreateProduct.Hide();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "Ошибка сохранения товара");
|
||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
|
||
MessageBoxIcon.Error);
|
||
}
|
||
finally
|
||
{
|
||
LoadData();
|
||
}
|
||
}
|
||
|
||
private void buttonUpdateProduct_Click(object sender, EventArgs e)
|
||
{
|
||
if (dataGridView.SelectedRows.Count <= 0) return;
|
||
_id = (Guid)dataGridView.SelectedRows[0].Cells["Id"].Value;
|
||
groupBoxControls.Hide();
|
||
groupBoxCreateProduct.Show();
|
||
_logger.LogInformation("Получение товара");
|
||
var view = _productLogic.ReadElement(new ProductSearchModel
|
||
{
|
||
Id = _id.Value
|
||
});
|
||
textBoxName.Text = view.Name;
|
||
numericUpDownPrice.Value = Convert.ToDecimal(view.Price);
|
||
numericUpDownAmount.Value = view.Amount;
|
||
checkBoxIsSold.Checked = view.IsBeingSold;
|
||
}
|
||
}
|
||
}
|