Esempio n. 1
0
        private void InitBook()
        {
            _dtpPublishDay.Format       = DateTimePickerFormat.Custom;
            _dtpPublishDay.CustomFormat = "yyyy";
            _olvColumnDatePublisher.AspectToStringConverter = (o) =>
            {
                var date = Convert.ToDateTime(o);
                return(date.ToString("yyyy"));
            };

            if (_selectedBook != null)
            {
                _txtName.Text        = _selectedBook.Name;
                _txtAuthor.Text      = _selectedBook.Author;
                _dtpPublishDay.Value = _selectedBook.DatePublisher;
                _txtCountPage.Text   = _selectedBook.CountPage.ToString();
                _txtTotalCount.Text  = _selectedBook.TotalCount.ToString();
            }

            _btnAddBook.Click += (sender, args) =>
            {
                if (string.IsNullOrEmpty(_txtName.Text) || string.IsNullOrEmpty(_txtAuthor.Text) || string.IsNullOrEmpty(_txtCountPage.Text) || string.IsNullOrEmpty(_txtTotalCount.Text))
                {
                    MessageBox.Show("Заполните все поля", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                try
                {
                    using (var context = new BookContext())
                    {
                        _selectedBook = new Core.Book(_txtName.Text, _txtAuthor.Text, Convert.ToInt32(_txtCountPage.Text), _dtpPublishDay.Value, Convert.ToInt32(_txtTotalCount.Text));
                        context.Books.AddOrUpdate(_selectedBook);
                        context.SaveChanges();
                        _objectListViewBook.SetObjects(context.Books);
                        ResetBook();
                    };
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ошибка: " + ex.Message);
                }
            };
            _btnUpdateBook.Click += (sender, args) =>
            {
                try
                {
                    using (var context = new BookContext())
                    {
                        var bookForUpdate = _objectListViewBook.SelectedObject as Core.Book;
                        if (bookForUpdate == null)
                        {
                            MessageBox.Show("Укажите книгу для редактирования", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                        bookForUpdate.Name          = _txtName.Text;
                        bookForUpdate.Author        = _txtAuthor.Text;
                        bookForUpdate.CountPage     = Convert.ToInt32(_txtCountPage.Text);
                        bookForUpdate.DatePublisher = _dtpPublishDay.Value;
                        bookForUpdate.TotalCount    = Convert.ToInt32(_txtTotalCount.Text);
                        context.Books.AddOrUpdate(bookForUpdate);
                        context.SaveChanges();
                        _objectListViewBook.SetObjects(context.Books);
                        ResetBook();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ошибка: " + ex.Message);
                }
            };
            _btnDeleteBook.Click += (sender, args) =>
            {
                try
                {
                    using (var context = new BookContext())
                    {
                        var deleteBook = _objectListViewBook.SelectedObject as Core.Book;
                        if (deleteBook == null)
                        {
                            MessageBox.Show("Укажите книгу", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                        if (MessageBox.Show($"Удалить книгу '{deleteBook.Name}' ?",
                                            "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                        {
                            return;
                        }
                        deleteBook = context.Books.First(w => w.Id == deleteBook.Id);
                        context.Books.Remove(deleteBook);
                        context.SaveChanges();
                        _objectListViewBook.SetObjects(context.Books);
                        ResetBook();
                    };
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ошибка: " + ex.Message);
                }
            };

            _objectListViewBook.SelectedIndexChanged += (sender, args) =>
            {
                Core.Book selectedBook = _objectListViewBook.SelectedObject as Core.Book;
                if (selectedBook == null)
                {
                    return;
                }
                _txtName.Text        = selectedBook.Name;
                _txtAuthor.Text      = selectedBook.Author;
                _txtCountPage.Text   = selectedBook.CountPage.ToString();
                _dtpPublishDay.Value = selectedBook.DatePublisher;
                _txtTotalCount.Text  = selectedBook.TotalCount.ToString();
            };
        }
Esempio n. 2
0
        public void InitGiveBook()
        {
            _olvColumnDatePublisher.AspectToStringConverter = (o) =>
            {
                var date = Convert.ToDateTime(o);
                return(date.ToString("yyyy"));
            };
            try
            {
                using (var context = new BookContext())
                {
                    _objectListViewBooks.SetObjects(context.Books);
                }
                _txtBookSearch.TextChanged += (sender, args) =>
                {
                    using (var context = new BookContext())
                    {
                        if (string.IsNullOrEmpty(_txtBookSearch.Text))
                        {
                            _objectListViewBooks.SetObjects(context.Books);
                            return;
                        }
                        var books = context.Books.Where(d => d.Name.ToLower().StartsWith(_txtBookSearch.Text.ToLower()) ||
                                                        d.Author.ToLower().StartsWith(_txtBookSearch.Text.ToLower())).Take(100);
                        _objectListViewBooks.SetObjects(books);
                    }
                };
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ошибка: " + ex.Message);
            }

            _objectListViewBooks.SelectedIndexChanged += (sender, args) =>
            {
                Core.Book selectedBook = _objectListViewBooks.SelectedObject as Core.Book;
                if (selectedBook == null)
                {
                    return;
                }
            };

            _btnGiveBook.Click += (sender, args) =>
            {
                try
                {
                    using (var context = new BookContext())
                    {
                        var       reader       = context.Readers.Include("Books").First(f => f.Id == _selectedReader.Id);
                        Core.Book selectedBook = _objectListViewBooks.SelectedObject as Core.Book;
                        if (selectedBook == null)
                        {
                            MessageBox.Show("Укажите книгу для выдачи", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                        if (MessageBox.Show($"Выдать книгу '{selectedBook.Name}', '{selectedBook.Author}' ?",
                                            "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                        {
                            return;
                        }
                        selectedBook = context.Books.First(b => b.Id == selectedBook.Id);

                        if (reader.Books.Any(book => book.Id == selectedBook.Id))
                        {
                            MessageBox.Show("У читателя есть такая книга", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                        if (selectedBook.StayCount < 1)
                        {
                            MessageBox.Show("Все экземпляры книг выданы", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }

                        reader.AddBook(selectedBook);
                        context.SaveChanges();
                        _objectListViewBooks.SetObjects(context.Books);
                        MessageBox.Show("Книга выдана, спасибо!", "Внимание");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ошибка: " + ex.Message);
                }
                Close();
            };
        }