ISEbd-21_Agliullov.D.A._Con.../Confectionery/FormShop.cs
2023-02-06 00:44:42 +04:00

168 lines
5.5 KiB
C#

using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.BusinessLogicsContracts;
using ConfectioneryContracts.ViewModels;
using ConfectioneryDataModels;
using ConfectioneryDataModels.Models;
using Microsoft.Extensions.Logging;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ConfectioneryView
{
public partial class FormShop : Form
{
private readonly List<ShopViewModel>? _listShops;
private readonly IShopLogic _logic;
private readonly ILogger _logger;
private IShopModel? _currentShopModel;
public int Id
{
get
{
return Convert.ToInt32(comboBoxShop.SelectedValue);
}
set
{
comboBoxShop.SelectedValue = value;
}
}
public IShopModel? ShopModel
{
get => _currentShopModel;
}
private IShopModel? GetShop()
{
var list = _logic.ReadList(null);
if (list == null)
{
return null;
}
foreach (var elem in list)
{
if (elem.Id == this.Id)
{
return elem;
}
}
return null;
}
public FormShop(ILogger<FormShop> logger, IShopLogic logic)
{
InitializeComponent();
_logger = logger;
_listShops = logic.ReadList(null);
_logic = logic;
if (_listShops != null)
{
comboBoxShop.DisplayMember = "Name";
comboBoxShop.ValueMember = "Id";
comboBoxShop.DataSource = _listShops;
comboBoxShop.SelectedItem = null;
}
}
private void LoadData()
{
try
{
var currentShop = _logic.ReadElement(new() { Id = this.Id});
if (currentShop != null)
{
var vmodel = GetShop();
if (vmodel != null)
{
comboBoxShop.Text = vmodel.Name;
textBoxAddress.Text = vmodel.Address;
textBoxDateOpening.Text = Convert.ToString(vmodel.DateOpening);
}
dataGridView.Rows.Clear();
foreach (var el in currentShop.Pastries.Values)
{
dataGridView.Rows.Add(new object[]{el.Item1.PastryName, el.Item1.Price, el.Item2 });
}
}
_logger.LogInformation("Загрузка магазинов");
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки магазинов");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void ComboBoxShop_SelectedIndexChanged(object sender, EventArgs e)
{
LoadData();
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(comboBoxShop.Text))
{
MessageBox.Show("Заполните название", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(textBoxAddress.Text))
{
MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Сохранение изделия");
try
{
var vmodel = GetShop();
if (vmodel != null)
{
_currentShopModel = vmodel;
return;
}
// Создаем новый магазин если не нашли такого
DateTime.TryParse(textBoxDateOpening.Text, out var dateTime);
ShopBindingModel model = new()
{
Name = comboBoxShop.Text,
Address = textBoxAddress.Text,
DateOpening = dateTime
};
var operationResult = _logic.Create(model);
_currentShopModel = 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();
}
}
}