144 lines
4.7 KiB
C#
144 lines
4.7 KiB
C#
using GiftShopContracts.BindingModels;
|
|
using GiftShopContracts.BusinessLogicsContracts;
|
|
using GiftShopContracts.ViewModels;
|
|
using GiftShopDataModels.Models;
|
|
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 GiftShopView
|
|
{
|
|
public partial class FormShop : Form
|
|
{
|
|
private readonly List<ShopViewModel>? _listShops;
|
|
private readonly IShopLogic _logic;
|
|
private readonly ILogger _logger;
|
|
public int Id { get; set; }
|
|
|
|
public FormShop(ILogger<FormShop> logger, IShopLogic logic)
|
|
{
|
|
InitializeComponent();
|
|
_logger = logger;
|
|
_listShops = logic.ReadList(null);
|
|
_logic = logic;
|
|
}
|
|
|
|
private IShopModel? GetShop(int id)
|
|
{
|
|
if (_listShops == null)
|
|
{
|
|
return null;
|
|
}
|
|
foreach (var elem in _listShops)
|
|
{
|
|
if (elem.Id == id)
|
|
{
|
|
return elem;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private void SaveButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (string.IsNullOrEmpty(nameTextBox.Text))
|
|
{
|
|
MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(addressTextBox.Text))
|
|
{
|
|
MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
|
|
_logger.LogInformation("Сохранение изделия");
|
|
|
|
try
|
|
{
|
|
DateTime.TryParse(openingDatePicker.Text, out var dateTime);
|
|
ShopBindingModel model = new()
|
|
{
|
|
ShopName = nameTextBox.Text,
|
|
ShopAddress = addressTextBox.Text,
|
|
OpeningDate = dateTime
|
|
};
|
|
var vmodel = GetShop(Id);
|
|
bool operationResult = false;
|
|
|
|
if (vmodel != null)
|
|
{
|
|
model.Id = vmodel.Id;
|
|
model.Gifts = vmodel.Gifts;
|
|
operationResult = _logic.Update(model);
|
|
}
|
|
else
|
|
{
|
|
operationResult = _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();
|
|
}
|
|
|
|
private void FormShop_Load(object sender, EventArgs e)
|
|
{
|
|
LoadData();
|
|
}
|
|
private void LoadData(bool extendDate = true)
|
|
{
|
|
try
|
|
{
|
|
var model = GetShop(extendDate ? Id : Convert.ToInt32(nameTextBox.Text));
|
|
if (model != null)
|
|
{
|
|
nameTextBox.Text = model.ShopName;
|
|
addressTextBox.Text = model.ShopAddress;
|
|
openingDatePicker.Text = Convert.ToString(model.OpeningDate);
|
|
dataGridView.Rows.Clear();
|
|
// ne zahel
|
|
foreach (var el in model.Gifts.Values)
|
|
{
|
|
dataGridView.Rows.Add(new object[] { el.Item1.GiftName, el.Item1.Price, el.Item2 });
|
|
}
|
|
|
|
}
|
|
_logger.LogInformation("Загрузка магазинов");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка загрузки магазинов");
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
|
|
MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|