-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.rb
More file actions
367 lines (309 loc) · 11.1 KB
/
cli.rb
File metadata and controls
367 lines (309 loc) · 11.1 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
class CommandLineInterface
def initialize
@prompt = TTY::Prompt.new
@font = TTY::Font.new(:doom)
@user = nil
@reservation = nil
@restaurant = nil
@review = nil
@pastel = Pastel.new
end
###############################USER METHODS#######################################
def new_user
puts "Please enter your name: "
user_input = gets.chomp
new_user = User.create(name: user_input)
puts "Welcome to Make Res, #{new_user.name}"
@user = new_user
choices
end
def returning_user
puts "Please enter your name: "
user_input = gets.chomp
ret_user = User.find_by(name: user_input)
if ret_user == nil
puts "user name not found, please try again or create an account"
greeting_prompt
else
puts `clear`
@user = ret_user
puts "Welcome back, #{ret_user.name}!"
choices
end
end
def view_all_reservations
puts `clear`
if @user.reservations.length == 0
@prompt.select("You have no reservations at this time, would you like to make one?") do |menu|
menu.choice "yes", -> { select_restaurant }
menu.choice "no", -> { choices }
end
else
@prompt.select("Here are your reservations") do |menu|
Reservation.all.map do |reservation|
# @reservation = reservation
if reservation.user_id == @user.id
menu.choice "#{reservation.restaurant.name} - #{reservation.time}", -> { view_reservation(reservation) }
end
end
menu.choice "back", -> { choices }
end
end
end
def view_favorite_restaurants
puts `clear`
restaurant = nil
restaurant_names = []
review_array = Review.where(user_id: user_id = @user.id).where(rating: rating = 5)
if review_array.length == 0
puts "you haven't rated any restaurants 5 stars you grouch"
else
puts "These are the restaurants you've rated 5 stars or more"
review_array.map do |review|
restaurant = Restaurant.find(review.restaurant_id)
restaurant_names << restaurant.name
end
end
restaurant_names.uniq!
restaurant_names.each do |restaurant_name|
puts restaurant_name
end
puts "######################################################"
choices
end
##########################RESTAURANT METHODS#############################
#SELECT
def select_restaurant
#puts `clear`
@prompt.select("pick a restaurant") do |menu|
Restaurant.all.map do |restaurant|
# @restaurant = restaurant
menu.choice restaurant.name, -> { reserve_or_review(restaurant.name) }
end
menu.choice "back", -> { choices }
end
end
def reserve_or_review(restaurant_name)
@restaurant = Restaurant.find_by(name: restaurant_name)
@prompt.select("Would you like to reserve or leave a review for this restaurant?") do |menu|
menu.choice "Make a reservation", -> { make_reservation }
menu.choice "Leave a review", -> { write_review }
menu.choice "Back", -> { choices }
end
end
#MAKE RESERVATION
def make_reservation
time = Time.new
#ask for name
#ask for date (09-12-19 Format)
#ask for time
# puts "Please enter your name: "
# user_name = gets.chomp
# new_user = User.find_by(name: user_name)
#binding.pry
puts "You are making a reservation at #{@restaurant.name}:"
puts "This restaurant has had #{@restaurant.users.length} visitor(s) this month."
puts "#######################################"
puts "For which date? "
puts time.strftime("today is %A %B %d")
res_date = gets.chomp
puts "at what time?"
res_time = gets.chomp
puts "for how many people?"
res_num = gets.chomp
# binding.pry
reservation = Reservation.create(user_id: @user.id, restaurant_id: @restaurant.id, time: res_time, date: res_date, number_of_people: res_num )
puts "#########################################################################"
puts "You have just made a reservation at #{reservation.restaurant.name}"
puts "on #{reservation.date}"
puts "at #{reservation.time}"
puts "for #{reservation.number_of_people} person(s)"
choices
end
#VIEW
def view_reservation(reservation)
puts `clear`
@reservation = reservation
puts "You have a reservation
at #{@reservation.restaurant.name}
on #{@reservation.date}
at #{@reservation.time}
for #{@reservation.number_of_people} people!"
@prompt.select("You can :") do |menu|
menu.choice "edit reservation", -> { update_reservation }
menu.choice "delete reservation", -> { delete_reservation }
menu.choice "back", -> { view_all_reservations }
end
end
#DELETE
def delete_reservation
puts `clear`
@prompt.select("are you sure?") do |menu|
menu.choice "yes", -> { @reservation.destroy }
menu.choice "no", -> { view_reservation(@reservation) }
end
view_all_reservations
end
#UPDATE
def update_reservation
puts `clear`
@prompt.select("What would you like to change?") do |menu|
menu.choice 'I want to change the date', -> { change_date }
menu.choice 'I want to change the time', -> { change_time }
menu.choice 'I want to change the number of people in my party', -> { change_num_people }
end
end
def change_date
puts `clear`
puts "When would you like to change the date to?"
new_date = gets.chomp
@reservation.update(date: new_date)
puts "Your reservation now is
at #{@reservation.restaurant.name}
on #{@reservation.date}
at #{@reservation.time}
for #{@reservation.number_of_people} people!"
view_all_reservations
end
def change_time
puts `clear`
puts "When would you like to change the time to?"
new_time = gets.chomp
@reservation.update(time: new_time)
puts "Your reservation now is
at #{@reservation.restaurant.name}
on #{@reservation.date}
at #{@reservation.time}
for #{@reservation.number_of_people} people!"
view_all_reservations
end
def change_num_people
puts `clear`
puts "When would you like to change the number of people in your party to?"
new_num = gets.chomp
@reservation.update(number_of_people: new_num)
puts "Your reservation now is
at #{@reservation.restaurant.name}
on #{@reservation.date}
at #{@reservation.time}
for #{@reservation.number_of_people} people!"
view_all_reservations
end
########################REVIEW METHODS############################
def write_review
puts `clear`
if !@restaurant.users.include?(@user)
@prompt.select("You haven't visited this restaurant, would you like to make a reservation?") do |menu|
menu.choice "yes", -> { make_reservation }
menu.choice "no", -> { choices }
end
else
puts "What would you rate this restaurant from 1-5?"
rating = gets.chomp
puts "What comments do you have about this restaurant?"
content = gets.chomp
new_review = Review.create(rating: rating, content: content, user_id: @user.id, restaurant_id: @restaurant.id)
choices
end
end
def reviews
puts `clear`
if @user.reviews.length == 0
puts "You have no reviews at this time"
select_restaurant
end
@prompt.select("Choose a review to edit or delete") do |menu|
Review.all.map do |review|
if review.user_id == @user.id
menu.choice "Review: #{review.content}\nRestaurant: #{review.restaurant}\nrating: #{review.rating}", -> { edit_or_delete_review(review) }
puts "\n"
end
end
menu.choice "back", -> { choices }
end
end
def edit_or_delete_review(review)
puts `clear`
@review = review
@prompt.select("Would you like to edit or delete this review?") do |menu|
menu.choice "Edit the review", -> { edit_review }
menu.choice "Delete the review", -> { delete_review }
menu.choice "back", -> { choices }
end
end
def edit_review
puts `clear`
puts "What would you rate this restaurant from 1-5?"
rating = gets.chomp
puts "What comments do you have about this restaurant?"
content = gets.chomp
restaurant = @review.restaurant
@restaurant = Restaurant.find_by(name: restaurant)
#binding.pry
@review.update(rating: rating, content: content, user_id: @user.id, restaurant_id: @restaurant.id)
choices
end
############### Changes: added yes/no choice
def delete_review
puts `clear`
@prompt.select("are you sure?") do |menu|
menu.choice "yes", -> { @review.destroy }
menu.choice "no", -> { reviews }
end
reviews
end
########################PROMPT METHODS #########################################################
def greeting_prompt
# puts @pastel.yellow(@font.write("WELCOME TO MAKE RES"))
# puts @font.write("your best way")
# puts @font.write("to make reservations")
@prompt.select("Select an option") do |menu|
menu.choice 'returning user', -> { returning_user }
menu.choice 'new user', -> { new_user }
menu.choice 'just a quick search', -> { yelp_results }
menu.choice 'quit app'
end
end
def choices
#puts `clear`
@prompt.select("what do you want to do today?") do |menu|
menu.choice 'Choose a restaurant to make a reservation or leave a review', -> { select_restaurant }
menu.choice 'View/edit your reservations', -> { view_all_reservations }
################## Changes: "see or edit reviews" for clarity
menu.choice 'See or edit your reviews', -> { reviews }
menu.choice 'View your favorite restaurants', -> { view_favorite_restaurants }
menu.choice 'exit'
end
end
def header_message
puts @pastel.yellow(@font.write("WELCOME TO MAKE RES"))
puts @font.write("your best way")
puts @font.write("to make reservations")
end
def yelp_results
puts `clear`
final_hash = {}
puts "what are you looking for?"
term = gets.chomp
puts "where are you located?"
location = gets.chomp
Yelp.search(term, location).each do |result_hash|
name = result_hash["name"]
number = result_hash["display_phone"]
price = result_hash["price"]
final_hash[name] = []
final_hash[name] << number
final_hash[name] << price
#binding.pry
end
puts "these are some #{term} restaurants we found in #{location}"
final_hash.each_with_index do |(restaurant, restaurant_data), i|
puts "#{i+1}. #{restaurant} - #{restaurant_data[0]} - #{restaurant_data[1]}."
end
greeting_prompt
end
def run
header_message
greeting_prompt
end
end