PIbd-31_Afanasev.S.S._COP_5/Component/Forms/FormBook.cs
2024-10-31 22:43:05 +04:00

161 lines
5.4 KiB
C#

using Components.VisualComponents;
using Contracts.BindingModels;
using Contracts.BusinessLogicsContracts;
using Contracts.SearchModels;
//using ControlsLibraryNet60.Input;
using DocumentFormat.OpenXml.Office2010.Word.DrawingShape;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinForms
{
public partial class FormBook : Form
{
private readonly IBookLogic _bookLogic;
private readonly IAuthorLogic _authorLogic;
private int? _id;
public int Id { set { _id = value; } }
public FormBook(IBookLogic bookLogic, IAuthorLogic authorLogic)
{
InitializeComponent();
_bookLogic = bookLogic;
_authorLogic = authorLogic;
customTextBox1.DatePattern = @"^(\d{2}.\d{2}.\d{4})$";
}
/// <summary>
/// Загрузка формы
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormBook_Load(object sender, EventArgs e)
{
var listAuthor = _authorLogic.ReadList(null);
if (listAuthor != null)
{
foreach (var type in listAuthor)
{
controlSelectedListBoxSingle.AddToList(type.Name);
}
}
if (!_id.HasValue)
{
return;
}
try
{
var book = _bookLogic.ReadElement(new BookSearchModel { Id = _id.Value });
if (book == null)
{
return;
}
textBoxTitleFullName.Text = book.TitleFullName;
controlSelectedListBoxSingle.SelectedElement = book.Author;
textBoxScan.Text = book.PicturePath;
customTextBox1.TextBoxValue = book.BookDate.ToString("dd MM yyyy", CultureInfo.CreateSpecificCulture("ru-RU"));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// Кнопка "Сохранить"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonSave_Click(object sender, EventArgs e)
{
customTextBox1.DatePattern = @"^(\d{2}.\d{2}.\d{4})$";
if (string.IsNullOrEmpty(textBoxTitleFullName.Text))
{
MessageBox.Show("Заполните ФИО официанта!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(textBoxScan.Text))
{
MessageBox.Show("Приложите скан чека!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(controlSelectedListBoxSingle.SelectedElement))
{
MessageBox.Show("Выберите блюдо!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(customTextBox1.TextBoxValue))
{
MessageBox.Show("Введите дату заказа!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
var model = new BookBindingModel
{
Id = _id ?? 0,
TitleFullName = textBoxTitleFullName.Text,
Author = controlSelectedListBoxSingle.SelectedElement,
PicturePath = textBoxScan.Text,
BookDate = DateTime.Parse(customTextBox1.TextBoxValue).ToUniversalTime(),
};
var operatingResult = _id.HasValue ? _bookLogic.Update(model) : _bookLogic.Create(model);
if (!operatingResult)
{
throw new Exception("Ошибка при создании сущности 'Счет'!");
}
MessageBox.Show("Создание сущности 'Счет' прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// Кнопка "Отмена"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void textBoxScan_Click(object sender, EventArgs e)
{
using (var dialog = new OpenFileDialog { Filter = "jpg|*.jpg" })
{
if (dialog.ShowDialog() == DialogResult.OK)
{
textBoxScan.Text = dialog.FileName.ToString();
}
}
}
}
}