-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
202 lines (173 loc) · 3.93 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
require 'pg'
require 'pry'
require 'sinatra'
require 'httparty'
# require 'sinatra/reloader'
require_relative 'db_config'
require_relative 'models/user'
require_relative 'models/comment'
require_relative 'models/book'
require_relative 'models/wish'
enable :sessions
helpers do
def current_user
User.find_by(id: session[:user_id])
end
def logged_in?
if current_user
return true
else
return false
end
end
end
get '/' do
@wishes = Wish.all.pluck
wish_id = Wish.all.pluck(:id)
@wish_total = wish_id.length
erb :index
end
get '/search_result' do
@book= params[:book]
result = HTTParty.get("https://www.googleapis.com/books/v1/volumes?q=#{@book}").parsed_response
@search = result["items"]
erb :search_result
end
get '/book_result' do
book_result = HTTParty.get("https://www.googleapis.com/books/v1/volumes?q=#{params[:id]}").parsed_response
book_search = book_result["items"][0]["volumeInfo"]
@title = book_search["title"]
begin
@author = book_search["authors"][0]
rescue
@author = " "
end
@publisher = book_search["publisher"]
@published_date = book_search["publishedDate"]
@description = book_search["description"]
@page_count = book_search["pageCount"]
begin
@category = book_search["categories"][0]
rescue
@category = " "
end
@rating = book_search["averageRating"]
@id = book_search["industryIdentifiers"][0]["identifier"]
@thumbnail = book_search["imageLinks"]["smallThumbnail"]
begin
@price = book_result["items"][0]["saleInfo"]["listPrice"]["amount"]
rescue
@price = 0
end
erb :book_result
end
get '/about' do
@wishes = Wish.all.pluck
wish_id = Wish.all.pluck(:id)
@wish_total = wish_id.length
erb :about
end
get '/sign_up' do
erb :sign_up
end
get '/login' do
erb :login
end
post '/users' do
user_exists = User.find_by(email: params[:email])
if user_exists == params[:email]
erb :login
else
user = User.new
user.email = params[:email]
user.password = params[:password]
user.save
session[:user_id] = user.id
redirect '/bookshelf'
end
end
get '/bookshelf' do
redirect '/login' unless logged_in?
@books = Book.all.pluck
@wishes = Wish.all.pluck
wish_id = Wish.all.pluck(:id)
@wish_total = wish_id.length
erb :bookshelf
end
get '/bookshelf/:id' do
@book = Book.find(params[:id])
@comments = Comment.where(book_id: params[:id])
erb :bookshelf
end
post '/bookshelf' do
book = Book.new
book.title = params[:title]
book.genre = params[:genre]
book.author = params[:author]
book.comment = params[:comment]
book.save
redirect '/bookshelf'
end
# post '/comments' do
# comment = Comment.new
# comment.body = params[:body]
# comment.book_id = params[:book_id]
# comment.save
# redirect "/bookshelf/#{comment.book_id}"
# end
get '/wishlist' do
redirect '/login' unless logged_in?
@wishes = Wish.all.pluck
wish_id = Wish.all.pluck(:id)
@wish_total = wish_id.length
erb :wishlist
end
post '/wishes' do
book_wish = HTTParty.get("https://www.googleapis.com/books/v1/volumes?q=#{params[:id]}").parsed_response
wish_result = book_wish["items"][0]
begin
@title = wish_result["volumeInfo"]["title"]
rescue
@title = " "
end
begin
rescue
@genre = " "
end
begin
@author = wish_result["volumeInfo"]["authors"][0]
rescue
@author = " "
end
begin
@price = wish_result["saleInfo"]["listPrice"]["amount"]
rescue
@price = 0
end
wish = Wish.new
wish.title = @title
wish.genre = @genre
wish.author = @author
wish.price = @price
wish.save
redirect '/wishlist'
end
# delete '/wishes/:id' do
# wish = Wish.find(params[:id])
# wish.destroy
#
# redirect '/wishlist'
# end
post '/session' do
user = User.find_by(email: params[:email])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect '/bookshelf'
else
erb :login
end
end
delete '/session' do
session[:user_id] = nil
redirect '/login'
end