-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
330 lines (284 loc) · 6.88 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
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
# require 'sinatra/reloader'
require 'sinatra'
require 'httparty'
require_relative 'db_config'
require_relative 'models/movie_buffer'
require_relative 'models/search_record'
require_relative 'models/user'
require_relative 'models/favorite_movie'
require_relative 'models/saved_movie'
require_relative 'models/tag'
require_relative 'models/movie_tag'
# sinatra provide this feature
enable :sessions
helpers do
def current_user
User.find_by(id: session[:user_id])
end
def logged_in?
# Use ! to change a object to boolean
!!current_user
end
def user_favor_movie
movie_list = []
favor_movies = FavoriteMovie.where(user_id: session[:user_id]).order("id DESC")
favor_movies.each do |movie|
movie_list.push(MovieBuffer.find_by(movie_id: movie.movie_id))
end
movie_list
end
def user_saved_movie
movie_list = []
saved_movies = SavedMovie.where(user_id: session[:user_id]).order("id DESC")
saved_movies.each do |movie|
movie_list.push(MovieBuffer.find_by(movie_id: movie.movie_id))
end
movie_list
end
def is_liked?(movie_id)
if FavoriteMovie.find_by(user_id: session[:user_id], movie_id: movie_id)
return true
else
return false
end
end
def is_saved?(movie_id)
if SavedMovie.find_by(user_id: session[:user_id], movie_id: movie_id)
return true
else
return false
end
end
end
def render_error(msg)
@error_msg = msg
erb :not_found
end
get '/' do
# @history = MovieBuffer.last(5).reverse
if logged_in?
@push_movies = user_favor_movie.last(5).reverse
else
group = FavoriteMovie.group(:movie_id)
most_liked_movie_ids = group.count.sort_by{|key, value| value}.last(5).reverse.to_h.keys
@push_movies = MovieBuffer.where(movie_id: most_liked_movie_ids)
end
erb :index
end
get '/movie' do
movie_id = params[:movie_id]
def render_normal(result)
if r=result["Ratings"].find{|rating| rating["Source"] == "Rotten Tomatoes"}
@tomato = r["Value"]
else
@tomato = "N/A"
end
new_record = MovieBuffer.create(
title: result['Title'],
year: result['Year'],
rated: result['Rated'],
runtime: result['Runtime'],
director: result['Director'],
poster: result['Poster'],
actors: result['Actors'],
ratings: @tomato,
movie_id: result['imdbID']
)
tag_arr = result['Genre'].split(', ')
tag_arr.each do |tag|
if Tag.find_by(content: tag)
t=Tag.find_by(content: tag)
else
t=Tag.create(content: tag)
end
new_movie_tag = MovieTag.new
new_movie_tag.movie_buffer_id = new_record.id
new_movie_tag.tag_id = t.id
new_movie_tag.save
end
end
def render_from_buffer(result)
@info = {
Title: result.title,
Year: result.year,
Rated: result.rated,
Runtime: result.runtime,
Director: result.director
}
@isPoster = true
if result.poster=="N/A"
@isPoster = false
else
@imgSrc = result.poster
end
@actors = result.actors.split(', ')
@tomato = result.ratings
@movie_id = result.movie_id
@movie_tags = result.tags
erb :movie
end
if !MovieBuffer.find_by(movie_id: movie_id)
result = HTTParty.get("http://www.omdbapi.com/?apikey=#{ENV['OMDB_API_KEY']}&i=#{movie_id}").parsed_response
if result["Response"] == "False"
render_error(result["Error"])
else
render_normal(result)
end
end
result = MovieBuffer.find_by(movie_id: movie_id)
render_from_buffer(result)
end
get '/movie_list' do
movie_name = params[:movie_name]
result = HTTParty.get("http://www.omdbapi.com/?apikey=#{ENV['OMDB_API_KEY']}&s=#{movie_name}&type=movie").parsed_response
def store_search(name)
if SearchRecord.find_by(name: name)
record=SearchRecord.find_by(name: name)
record.count = record.count.to_i+1
record.save
else
new_search = SearchRecord.create(
name: name,
count: 1
)
end
end
def render_normal(result)
@movie_list = result["Search"]
erb :movie_list
end
if result["Response"] == "False"
render_error(result["Error"])
else
store_search(movie_name)
render_normal(result)
end
end
get '/login' do
@user = User.new
session[:return_to] = request.referer
erb :login
end
post '/login' do
@user = User.find_by(email: params[:email])
if @user && @user.authenticate(params[:password])
session[:user_id] = @user.id
redirect '/'
else
@user ||= User.new(email: params[:email])
@login_failed = true
erb :login
end
end
post '/session' do
user = User.find_by(email: params[:email])
if user && user.authenticate(params[:password])
# session is a hash that can be access globally in the server
session[:user_id] = user.id
redirect session.delete(:return_to)
else
erb :login
end
end
delete '/session' do
session[:user_id] = nil
redirect '/'
end
get '/session' do
result = {
"login": logged_in?
}
return result.to_json
end
post '/like' do
if movie = FavoriteMovie.find_by(movie_id: params[:movie_id], user_id: params[:user_id])
movie.delete
return "unliked"
else
FavoriteMovie.create(
movie_id: params[:movie_id],
user_id: params[:user_id]
)
return "liked"
end
end
post '/save' do
if movie = SavedMovie.find_by(movie_id: params[:movie_id], user_id: params[:user_id])
movie.delete
return "deleted"
else
SavedMovie.create(
movie_id: params[:movie_id],
user_id: params[:user_id]
)
return "saved"
end
end
get '/about' do
erb :about
end
get '/signup' do
session[:return_to] = request.referer
erb :signup
end
post '/signup' do
u = User.new
u.name = params[:name]
u.email = params[:email]
u.password = params[:password]
if u.save
session[:user_id] = User.find_by(email: params[:email]).id
redirect session.delete(:return_to)
else
@error_msg = u.errors.messages
erb :signup
end
end
get '/user/:option' do
case params["option"]
when "favor"
@movie_list = user_favor_movie
@title = "#{current_user.name}'s favorite movies"
erb :user
when "saved"
@movie_list = user_saved_movie
@title = "#{current_user.name}'s saved movies"
erb :user
when "edit"
session[:return_to] = request.referer
erb :user_edit
end
end
get '/user/edit/:field' do
case params["field"]
when 'name'
erb :user_edit_name
when 'email'
erb :user_edit_email
when 'pwd'
erb :user_edit_pwd
else
erb :user_edit
end
end
post '/user/edit/pwd' do
user = current_user
if user && user.authenticate(params[:old_pwd])
user.password = params[:new_pwd]
user.save
session[:user_id] = nil
redirect '/'
else
erb :user_edit_pwd
end
end
post '/user/edit/name' do
user = current_user
if user && user.authenticate(params[:pwd])
user.name = params[:name]
user.save
redirect '/user/edit'
else
erb :user_edit_name
end
end