-
Notifications
You must be signed in to change notification settings - Fork 0
/
library_ui.rb
211 lines (187 loc) · 5.49 KB
/
library_ui.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
require './lib/book'
require './lib/author'
require 'pg'
DB = PG.connect(:dbname => 'library')
def main_menu
print "\n\n********* WELCOME TO THE LIBRARY **********\n\n"
puts "Press 'l' if you are a librarian"
puts "Press 'p' if you are a patron"
puts "Press 'x' to exit\n\n"
main_choice = gets.chomp
case main_choice
when 'l'
librarian_menu
when 'p'
patron_menu
when 'x'
puts "Good bye!"
exit
else
puts "That input was invalid. Please choose again."
main_menu
end
end
# def librarian_menu
# print "\n\n********* LIBRARIAN MENU **********\n\n"
# puts "Press 'n' to add a new book to the inventory"
# puts "Press 'l' to list all books in the inventory"
# puts "Press 't' to search for a book by title"
# puts "Press 'a' to search for a book by author"
# puts "Press 'u' to update a book"
# puts "Press 'd' to delete a book from inventory"
# puts "Press 'm' to return to the main menu"
def librarian_menu
print "\n\n", " "*8, "*"*20, " LIBRARIAN MENU ", "*"*20, "\n\n"
puts "\tPress 'n' to add a new book to the inventory",
"\tPress 'l' to list all books in the inventory",
"\tPress 'v' to list all authors in the inventory",
"\tPress 't' to search for a book by title",
"\tPress 'a' to search for a book by author",
"\tPress 'b' to update a book",
"\tPress 'u' to update an author",
"\tPress 'd' to delete a book from inventory",
"\tPress 'm' to return to the main menu\n\n"
user_choice = gets.chomp
case user_choice
when 'n'
add_book
librarian_menu
when 'l'
list_books
puts "\n\n***** Press enter to return to the Librarian Menu"
gets
librarian_menu
when 'v'
list_authors
puts "\n\n***** Press enter to return to the Librarian Menu"
gets
librarian_menu
when 't'
search_by_title
gets
librarian_menu
when 'a'
search_by_author
gets
librarian_menu
when 'b'
update_book
gets
librarian_menu
when 'u'
update_author
gets
librarian_menu
when 'd'
delete_book
gets
librarian_menu
when 'm'
main_menu
else
puts "That was not a valid choice. Please choose again."
librarian_menu
end
end
def add_book
print "\n\n********* Add Book **********\n\n"
print "Enter book title: "
title = gets.chomp
print "Enter author names: "
author = gets.chomp
new_book = Book.create({ :title => title})
new_author = correct_author(author)
author_ids = [new_author.id]
author_names = [new_author.name]
more_authors = true
while more_authors
puts "Are there more authors? (y/n): "
more_author_choice = gets.chomp.downcase
if more_author_choice == 'y'
print "Enter author names: "
author = gets.chomp
new_author = correct_author(author)
author_ids << new_author.id
author_names << new_author.name
elsif more_author_choice == 'n'
more_authors = false
else
puts "That input was not valid. Please try again."
end
end
new_book.add_combos(author_ids)
puts "The book #{title} by #{author_names.join(' and ')} was created successfully!"
end
def list_books
puts "\nThe current books in inventory are: \n"
Book.all.each_with_index do |book, index|
books_authors = book.books_authors.collect{|author| author.name}
puts "\t#{index + 1}. #{book.title} by #{books_authors.join(' and ')}"
end
end
def list_authors
puts "\nThe current authors in inventory are: \n"
Author.all.each_with_index do |author, index|
puts "\t#{index + 1}. #{author.name}"
end
end
def correct_author(author_name)
if Author.search_author(author_name) == []
new_author = Author.create({:name => author_name})
else
new_author = Author.search_author(author_name).first
end
new_author
end
def delete_book
list_books
puts "Enter the title of the book you would like to delete"
delete_choice = gets.chomp
book_to_delete = Book.search_title(delete_choice).first
authors = book_to_delete.books_authors
if authors.length == 1 && authors[0].authors_books.length == 1
authors[0].deleted
end
book_to_delete.deleted
puts "The book #{book_to_delete.title} was successfully deleted!"
end
def search_by_title
print "Enter title to search books by: "
search_term = gets.chomp
list = Book.search_title(search_term)
list.each_with_index do |book, index|
books_authors = book.books_authors.collect{|author| author.name}
puts "#{index + 1}. #{book.title} by #{books_authors.join(' and ')}"
end
end
def search_by_author
print "Enter author to search books by: "
search_term = gets.chomp
list = Author.search_author(search_term)
list.each_with_index do |author, index|
books_titles = author.authors_books.collect{|book| book.title}
puts "#{index + 1}. #{author.name} has authored the following books: "
puts books_titles.join("\n")
end
end
def update_book
list_books
print "Enter the number of the book you would like to update: "
book_choice = gets.chomp
book_to_update = Book.all[book_choice.to_i - 1]
print "Enter the new title of the book: "
new_title = gets.chomp
book_to_update.update(new_title)
puts "The book #{new_title} was successfully updated."
end
def update_author
list_authors
print "Enter the number of the author you would like to update: "
author_choice = gets.chomp
author_to_update = Author.all[author_choice.to_i - 1]
print "Enter the new name of the author: "
new_name = gets.chomp
author_to_update.update(new_name)
puts "The author #{new_name} was successfully updated."
end
main_menu