-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
217 lines (176 loc) · 5.06 KB
/
main.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 'sinatra'
# require 'sinatra/reloader'
require 'httparty'
require 'pry'
require_relative 'db_config'
require_relative 'models/mydrink'
# require_relative 'models/comment' # bonus part
require_relative 'models/drink'
require_relative 'models/user'
enable :sessions # sinatra feature to do sessions
# global methods
helpers do
def current_user
User.find_by(id: session[:user_id])
end
def logged_in?
!!current_user
end
# hasnt been used
def drink_exist?
!!Drink.find_by(iddrink: params[:apiDb_id])
end
end
# get index homepage with login
get '/' do
result = HTTParty.get("http://www.thecocktaildb.com/api/json/v1/1/random.php")
@result_hash = result.parsed_response['drinks'].first
erb :index, :layout => false
end
get '/search_results' do
@drink_keyword = "#{params[:drink_name]}"
result = HTTParty.get("http://www.thecocktaildb.com/api/json/v1/1/search.php?s=#{params[:drink_name]}")
@result_hash = result.parsed_response['drinks']
erb :search_results
end
get '/my_drinks' do
# redirect '/' unless logged_in?
# find the drink_id from mydrinks table
mydrinks = Mydrink.all
@drinks = []
mydrinks.each do |elem|
if elem.user_id == current_user.id
@drinks << Drink.find_by(id: elem[:drink_id])
end
end
# binding.pry
erb :my_cocktails
end
put '/my_drinks/:id' do
mydrink = Mydrink.find(params[:id])
mydrink.note_body = params[:body]
mydrink.save
# binding.pry
redirect "/show/#{mydrink.drink_id}"
end
delete '/my_drinks/:id' do
mydrink = Mydrink.find_by(drink_id: params[:id]).delete
redirect '/my_drinks'
end
post '/drinks' do
drink_exist = Drink.find_by(iddrink: params[:apiDb_id])
if drink_exist == nil
drink = Drink.new
drink.iddrink = params[:apiDb_id]
drink.strdrink = params[:apiDb_name]
drink.strdrinkthumb = params[:apiDb_pic]
drink.strglass = params[:apiDb_gla]
drink.strinstructions = params[:apiDb_ins]
drink.stringr1 = params[:apiDb_ing1]
drink.stringr2 = params[:apiDb_ing2]
drink.stringr3 = params[:apiDb_ing3]
drink.stringr4 = params[:apiDb_ing4]
drink.stringr5 = params[:apiDb_ing5]
drink.stringr6 = params[:apiDb_ing6]
drink.stringr7 = params[:apiDb_ing7]
drink.strmeas1 = params[:apiDb_mea1]
drink.strmeas2 = params[:apiDb_mea2]
drink.strmeas3 = params[:apiDb_mea3]
drink.strmeas4 = params[:apiDb_mea4]
drink.strmeas5 = params[:apiDb_mea5]
drink.strmeas6 = params[:apiDb_mea6]
drink.strmeas7 = params[:apiDb_mea7]
drink.save
drink_exist = Drink.find_by(iddrink: params[:apiDb_id])
end
if Mydrink.find_by(drink_id: drink_exist.id)==nil
mydrink = Mydrink.new
mydrink.user_id = current_user.id
mydrink.note_body = params[:body]
# mydrink.rating =
if (Drink.find_by(iddrink: params[:apiDb_id]))== nil
mydrink.drink_id = drink.id
else
mydrink.drink_id = Drink.find_by(iddrink: params[:apiDb_id]).id
end
mydrink.save
end
redirect '/my_drinks'
end
get '/drinks' do
@drinks = Drink.all
erb :popular_cocktails
end
get '/drinks/new' do
erb :new
end
post '/new_drink' do
# add a new cocktail to the database
drink = Drink.new
drink.creater_id = current_user.id
drink.strdrink = params[:strDrink]
drink.strglass = params[:strGlass]
drink.strinstructions = params[:strInstructions]
drink.strdrinkthumb = params[:strDrinkThumb]
drink.stringr1 = params[:strIngredient1]
drink.stringr2 = params[:strIngredient2]
drink.stringr3 = params[:strIngredient3]
drink.stringr4 = params[:strIngredient4]
drink.stringr5 = params[:strIngredient5]
drink.stringr6 = params[:strIngredient6]
drink.stringr7 = params[:strIngredient7]
drink.strmeas1 = params[:strMeasure1]
drink.strmeas2 = params[:strMeasure2]
drink.strmeas3 = params[:strMeasure3]
drink.strmeas4 = params[:strMeasure4]
drink.strmeas5 = params[:strMeasure5]
drink.strmeas6 = params[:strMeasure6]
drink.strmeas7 = params[:strMeasure7]
drink.datemodified = Date.today.to_s
drink.save
mydrink = Mydrink.new
mydrink.user_id = current_user.id
mydrink.drink_id = drink.id
# mydrink.note_body = params[:body]
# mydrink.rating =
mydrink.save
redirect '/my_drinks'
end
get '/show/:id' do
# redirect '/' unless logged_in?
@drink = Drink.find(params[:id])
@mydrink = Mydrink.where(user_id: current_user.id, drink_id: params[:id]).first
erb :show
end
get '/show' do
# redirect '/' unless logged_in?
result = HTTParty.get("http://www.thecocktaildb.com/api/json/v1/1/lookup.php?i=#{params[:i]}")
@result_hash = result.parsed_response['drinks'].first
erb :show_apiDB
end
get '/login' do
erb :login
end
post '/users' do
user = User.new
user.email = params[:email]
user.password = params[:password]
user.nickname = params[:nickname]
user.save
redirect '/'
end
post '/session' do
# check email
user = User.find_by(email: params[:email])
# check password
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect '/my_drinks'
else
erb :index
end
end
delete '/session' do
session[:user_id] = nil
redirect '/'
end