123 lines
4.3 KiB
C#
123 lines
4.3 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using SewingDressesContracts.BindingModels;
|
|
using SewingDressesContracts.BusinessLogicsContracts;
|
|
using SewingDressesContracts.SearchModels;
|
|
using SewingDressesDataModels.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 SewingDressesView
|
|
{
|
|
public partial class ShopForm : Form
|
|
{
|
|
private readonly ILogger _logger;
|
|
private readonly IShopLogic _logic;
|
|
public int? _id;
|
|
private Dictionary<int, (IDressModel, int)> _dresses;
|
|
public ShopForm(ILogger<ShopForm> logger, IShopLogic logic)
|
|
{
|
|
InitializeComponent();
|
|
_logger = logger;
|
|
_logic = logic;
|
|
}
|
|
|
|
private void ShopForm_Load(object sender, EventArgs e)
|
|
{
|
|
if (_id.HasValue)
|
|
{
|
|
_logger.LogInformation("Shop load");
|
|
try
|
|
{
|
|
var shop = _logic.ReadElement(new ShopSearchModel { Id = _id });
|
|
if (shop != null)
|
|
{
|
|
textBoxName.Text = shop.ShopName;
|
|
textBoxAdress.Text = shop.Adress;
|
|
dateTimePicker.Text = shop.DateOpen.ToString();
|
|
_dresses = shop.ShopDresses;
|
|
}
|
|
LoadData();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Load shop error");
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
|
|
MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void LoadData()
|
|
{
|
|
_logger.LogInformation("Load dresses");
|
|
try
|
|
{
|
|
if (_dresses != null)
|
|
{
|
|
foreach (var dress in _dresses)
|
|
{
|
|
dataGridView.Rows.Add(new object[] { dress.Key, dress.Value.Item1.DressName, dress.Value.Item1.Price, dress.Value.Item2 });
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Load dresses into shop error");
|
|
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;
|
|
}
|
|
if (string.IsNullOrEmpty(textBoxAdress.Text))
|
|
{
|
|
MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
_logger.LogInformation("Save shop");
|
|
try
|
|
{
|
|
var model = new ShopBindingModel
|
|
{
|
|
Id = _id ?? 0,
|
|
ShopName = textBoxName.Text,
|
|
Adress = textBoxAdress.Text,
|
|
DateOpen = dateTimePicker.Value.Date,
|
|
ShopDresses = _dresses
|
|
};
|
|
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, "Save shop error");
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
private void buttonCancel_Click(object sender, EventArgs e)
|
|
{
|
|
DialogResult = DialogResult.Cancel;
|
|
Close();
|
|
}
|
|
}
|
|
}
|