Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Library.Models;
- using Library.Repositories;
- using Library.Services;
- 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 Library
- {
- public partial class AddBook : Form
- {
- BookService _bookService;
- AuthorService _authorService;
- BookCopyService _bookCopyService;
- public AddBook()
- {
- InitializeComponent();
- RepositoryFactory repoFactory = new RepositoryFactory();
- _bookService = new BookService(repoFactory);
- _authorService = new AuthorService(repoFactory);
- _bookCopyService = new BookCopyService(repoFactory);
- _bookService.Updated += ListAllBooks;
- FillAuthors();
- foreach (Book b in _bookService.All())
- {
- bookList.Items.Add(b);
- }
- }
- public void ListAllBooks(object sender, EventArgs e)
- {
- bookList.Items.Clear();
- foreach (Book b in _bookService.All())
- {
- bookList.Items.Add(b);
- }
- }
- public void FillAuthors()
- {
- authorBox.Items.Clear();
- foreach (Author a in _authorService.All())
- {
- authorBox.Items.Add(a);
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Author selectedAuthor = authorBox.SelectedItem as Author;
- Book newEntry = new Book
- {
- Title = titleBox.Text,
- ISBN = Int32.Parse(isbnBox.Text),
- Description = descriptionBox.Text,
- Author = selectedAuthor
- };
- BookCopy newCopy = new BookCopy
- {
- Book = newEntry
- };
- titleBox.Clear();
- isbnBox.Clear();
- descriptionBox.Clear();
- authorBox.SelectedIndex = -1;
- _bookService.Add(newEntry);
- for (int i = 0; i < numCopies.Value; i++)
- {
- _bookCopyService.Add(newCopy);
- newEntry.NumCopies.Add(newCopy);
- }
- }
- private void homeBtn_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- private void editBtn_Click(object sender, EventArgs e)
- {
- Book selectedBook = bookList.SelectedItem as Book;
- Book editBook = new Book
- {
- Id = selectedBook.Id,
- Title = titleBox.Text,
- Description = descriptionBox.Text,
- ISBN = Convert.ToInt32(isbnBox.Text),
- NumCopies = selectedBook.NumCopies,
- Author = authorBox.SelectedItem as Author
- };
- titleBox.Clear();
- isbnBox.Clear();
- descriptionBox.Clear();
- authorBox.SelectedIndex = -1;
- _bookService.Edit(editBook);
- }
- private void removeBtn_Click(object sender, EventArgs e)
- {
- // if (bookList.SelectedIndex != null && bookCopyList.SelectedIndex)
- _bookService.Remove(bookList.SelectedItem as Book);
- }
- private void bookList_SelectedIndexChanged(object sender, EventArgs e)
- {
- Book selectedBook = bookList.SelectedItem as Book;
- bookCopyList.Items.Clear();
- foreach (BookCopy bc in selectedBook.NumCopies)
- {
- bookCopyList.Items.Add(bc.Id + "--" + bc.Book.Title);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement