Advertisement
Guest User

Untitled

a guest
Oct 24th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. using Library.Models;
  2. using Library.Repositories;
  3. using Library.Services;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13.  
  14. namespace Library
  15. {
  16. public partial class AddBook : Form
  17. {
  18.  
  19. BookService _bookService;
  20. AuthorService _authorService;
  21. BookCopyService _bookCopyService;
  22.  
  23.  
  24. public AddBook()
  25. {
  26.  
  27. InitializeComponent();
  28. RepositoryFactory repoFactory = new RepositoryFactory();
  29. _bookService = new BookService(repoFactory);
  30. _authorService = new AuthorService(repoFactory);
  31. _bookCopyService = new BookCopyService(repoFactory);
  32.  
  33. _bookService.Updated += ListAllBooks;
  34.  
  35. FillAuthors();
  36.  
  37. foreach (Book b in _bookService.All())
  38. {
  39. bookList.Items.Add(b);
  40. }
  41.  
  42.  
  43.  
  44. }
  45.  
  46. public void ListAllBooks(object sender, EventArgs e)
  47. {
  48. bookList.Items.Clear();
  49. foreach (Book b in _bookService.All())
  50. {
  51. bookList.Items.Add(b);
  52. }
  53. }
  54.  
  55. public void FillAuthors()
  56. {
  57. authorBox.Items.Clear();
  58. foreach (Author a in _authorService.All())
  59. {
  60. authorBox.Items.Add(a);
  61. }
  62. }
  63.  
  64.  
  65. private void button1_Click(object sender, EventArgs e)
  66. {
  67. Author selectedAuthor = authorBox.SelectedItem as Author;
  68.  
  69. Book newEntry = new Book
  70. {
  71. Title = titleBox.Text,
  72. ISBN = Int32.Parse(isbnBox.Text),
  73. Description = descriptionBox.Text,
  74. Author = selectedAuthor
  75. };
  76. BookCopy newCopy = new BookCopy
  77. {
  78. Book = newEntry
  79. };
  80.  
  81. titleBox.Clear();
  82. isbnBox.Clear();
  83. descriptionBox.Clear();
  84. authorBox.SelectedIndex = -1;
  85.  
  86. _bookService.Add(newEntry);
  87. for (int i = 0; i < numCopies.Value; i++)
  88. {
  89. _bookCopyService.Add(newCopy);
  90. newEntry.NumCopies.Add(newCopy);
  91. }
  92.  
  93.  
  94.  
  95. }
  96.  
  97. private void homeBtn_Click(object sender, EventArgs e)
  98. {
  99. this.Close();
  100. }
  101.  
  102. private void editBtn_Click(object sender, EventArgs e)
  103. {
  104.  
  105. Book selectedBook = bookList.SelectedItem as Book;
  106.  
  107. Book editBook = new Book
  108. {
  109. Id = selectedBook.Id,
  110. Title = titleBox.Text,
  111. Description = descriptionBox.Text,
  112. ISBN = Convert.ToInt32(isbnBox.Text),
  113. NumCopies = selectedBook.NumCopies,
  114. Author = authorBox.SelectedItem as Author
  115. };
  116.  
  117. titleBox.Clear();
  118. isbnBox.Clear();
  119. descriptionBox.Clear();
  120. authorBox.SelectedIndex = -1;
  121.  
  122. _bookService.Edit(editBook);
  123. }
  124.  
  125. private void removeBtn_Click(object sender, EventArgs e)
  126. {
  127. // if (bookList.SelectedIndex != null && bookCopyList.SelectedIndex)
  128. _bookService.Remove(bookList.SelectedItem as Book);
  129. }
  130.  
  131. private void bookList_SelectedIndexChanged(object sender, EventArgs e)
  132. {
  133. Book selectedBook = bookList.SelectedItem as Book;
  134. bookCopyList.Items.Clear();
  135. foreach (BookCopy bc in selectedBook.NumCopies)
  136. {
  137. bookCopyList.Items.Add(bc.Id + "--" + bc.Book.Title);
  138. }
  139. }
  140.  
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement