127 lines
3.6 KiB
C#
127 lines
3.6 KiB
C#
|
using Contracts.BindingModels;
|
|||
|
using Contracts.BusinessLogicContracts;
|
|||
|
using Contracts.ViewModels;
|
|||
|
using KOP_Labs;
|
|||
|
using Microsoft.Extensions.Logging;
|
|||
|
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 Library
|
|||
|
{
|
|||
|
public partial class FormBook : Form
|
|||
|
{
|
|||
|
public int Id { set { id = value; } }
|
|||
|
|
|||
|
private readonly IBookLogic _logicB;
|
|||
|
private readonly IAuthorLogic _logicA;
|
|||
|
|
|||
|
private int? id;
|
|||
|
public FormBook(IBookLogic bookLogic, IAuthorLogic authorLogic)
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
_logicB = bookLogic;
|
|||
|
_logicA = authorLogic;
|
|||
|
}
|
|||
|
|
|||
|
private void buttonSave_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
dateTextBox.Pattern = @"^(([0-2][0-9])|([3][0-1]))\s(января|февраля|марта|апреля|мая|июня|июля|августа|сентября|октября|ноября|декабря)\s\d{4}$";
|
|||
|
if (string.IsNullOrEmpty(textBoxName.Text))
|
|||
|
{
|
|||
|
MessageBox.Show("Введите название книги", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(textBoxCover.Text))
|
|||
|
{
|
|||
|
MessageBox.Show("Выберите обложку", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(dateTextBox.TextBoxValue))
|
|||
|
{
|
|||
|
MessageBox.Show("Заполните дату публикации согласно шаблону", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(booksForm.SelectedValue))
|
|||
|
{
|
|||
|
MessageBox.Show("Выберите хотя бы одного автора", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
try
|
|||
|
{
|
|||
|
_logicB.CreateOrUpdate(new BookBindingModel
|
|||
|
{
|
|||
|
Id = id,
|
|||
|
Name = textBoxName.Text,
|
|||
|
PicturePath = textBoxCover.Text,
|
|||
|
Author = booksForm.SelectedValue,
|
|||
|
PublicationDate = DateTime.Parse(dateTextBox.TextBoxValue)
|
|||
|
}) ;
|
|||
|
|
|||
|
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|||
|
DialogResult = DialogResult.OK;
|
|||
|
Close();
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void FormBook_Load(object sender, EventArgs e)
|
|||
|
{
|
|||
|
List<AuthorViewModel> viewS = _logicA.Read(null);
|
|||
|
if (viewS != null)
|
|||
|
{
|
|||
|
booksForm.ClearList();
|
|||
|
foreach (AuthorViewModel s in viewS)
|
|||
|
{
|
|||
|
booksForm.FillValues(s.FIO);
|
|||
|
}
|
|||
|
}
|
|||
|
if (id.HasValue)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
BookViewModel view = _logicB.Read(new BookBindingModel { Id = id.Value })?[0];
|
|||
|
if (view != null)
|
|||
|
{
|
|||
|
textBoxName.Text = view.Name;
|
|||
|
textBoxCover.Text = view.PicturePath;
|
|||
|
booksForm.SelectedValue = view.Author;
|
|||
|
dateTextBox.TextBoxValue = view.PublicationDate.ToString("dd MMMM yyyy", CultureInfo.CreateSpecificCulture("ru-RU"));
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void buttonCancel_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
DialogResult = DialogResult.Cancel;
|
|||
|
Close();
|
|||
|
}
|
|||
|
|
|||
|
private void textBoxCover_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
using (var dialog = new OpenFileDialog { Filter = "jpg|*.jpg" })
|
|||
|
{
|
|||
|
if (dialog.ShowDialog() == DialogResult.OK)
|
|||
|
{
|
|||
|
textBoxCover.Text = dialog.FileName.ToString();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|