-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
252 lines (172 loc) · 5.63 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
require 'sinatra'
require 'sinatra/reloader' if development? #stops and restarts your app when you change your code
require 'pg' # we need these functions to talk to the db
require 'pry' if development? #allows us to use commands like binding.pry to debug
require 'bcrypt' # allows us to use the encryption digestion for passwords
require 'cloudinary'
cloudinary_options = { # this is for cloudinary to allow the storing of pictures (HOW WILL THIS WORK WHEN ITS HOSTED ON THE CLOUD? NEED A SECRET FILE SOMEWHERE MAYBE?)
cloud_name: 'dciyilvza',
api_key: ENV['CLOUDINARY_API_KEY'],
api_secret: ENV['CLOUDINARY_API_SECRET']
}
enable :sessions #this allows us to create a login session for the user
# MY RESOURCES - Users, ducks, likes, comments
# Likes, comments and ducks can BELONG to a user
require_relative 'models/duck.rb'
require_relative 'models/user.rb'
require_relative 'models/comment.rb'
require_relative 'models/like.rb'
# helper functions, like in GFH.
def logged_in?
if session[:user_id]
return true
else
return false
end
end
def current_user
sql = "select * from users where id = #{session[:user_id]};"
user = db_query(sql).first
return OpenStruct.new(user) # this returns the user in an object-like state, so on the layout erb we can use js-like stuff like current_user.email
end
# def same_user?
# sql = 'select * from users where id = $1;'
# post_creater = db_query(sql, [params['user_id']])
# end
get '/' do
# allow the user to log in, or just see some posts
result = random_five_ducks()
erb :index, locals: {
ducks: result
}
end
get '/ducks/search' do
erb :search_duck
end
get '/ducks/search_results_name' do
name = params['name']
sql = "select * from ducks where name = $1;"
results = db_query(sql, [name])
erb :search_results_name, locals: {
results: results
}
end
get '/ducks/search_results_location' do
location = params['location_spotted']
sql = "select * from ducks where location_spotted = $1;"
results = db_query(sql, [location])
erb :search_results_location, locals: {
results: results
}
end
get '/ducks/search_results_user' do
user_email = params['email']
user_id = find_user_id(user_email)
sql = "select * from ducks where user_id = $1;"
results = db_query(sql, [user_id])
erb :search_results_user, locals: {
results: results
}
end
get '/sign_up' do
erb :new_user
end
post '/users' do
email = params['email']
sql = "select * from users where email = $1"
result = db_query(sql, [email])
if result.count == 0
create_user(params['email'], params['password'])
redirect '/'
else
erb :new_user
end
end
get '/ducks/new' do # allows the user to see the form to make a new dick if they're logged in
redirect '/login' unless logged_in?
erb :new_duck
end
post '/ducks' do # sends the new duck info to server
redirect '/login' unless logged_in?
duck_pic = params['image']['tempfile']
result = Cloudinary::Uploader.upload(duck_pic, cloudinary_options)
result_url = result['secure_url']
create_duck(params['name'], result_url, params['location_spotted'], current_user.id, likes_count = 0, comments_count = 0)
redirect '/'
end
delete '/ducks' do
# redirect '/login' unless logged_in?
# redirect "/ducks/#{params['id']}" unless current_user.id == duck['user_id']
delete_duck(params['duck_id'])
redirect '/'
end
get '/ducks/:duck_id' do # this is a SHOW, we're showing 1 post.
duck_id = params['duck_id']
duck = db_query("select * from ducks where id = $1;", [duck_id]).first
comments = db_query('select * from comments join users on (users.id = comments.user_id) where duck_id = $1', [duck_id])
erb :show_duck, locals: {
duck: duck,
comments: comments
}
end
get '/ducks/:id/edit' do
redirect '/login' unless logged_in?
sql = "select * from ducks where id = $1;"
duck = db_query(sql, [params['id']]).first
redirect "/ducks/#{params['id']}" unless current_user.id == find_duck(params['id']).first['user_id']
erb :edit_duck, locals: {
duck: duck
}
end
put '/ducks/:id' do
redirect '/login' unless logged_in?
redirect "/ducks/#{params['id']}" unless current_user.id == find_duck(params['id']).first['user_id']
update_duck(
params['name'],
params['image_url'],
params['location_spotted'],
params['id']
)
redirect "/ducks/#{params['id']}"
end
get '/login' do
redirect '/' if logged_in?
erb :login
end
post '/session' do
email = params['email']
user_plain_password = params['password']
sql = 'select * from users where email = $1;'
result = db_query(sql, [email])
if result.count > 0 && BCrypt::Password.new(result[0]['password_digest']) == user_plain_password
session[:user_id] = result[0]['id']
redirect '/'
else
erb :login
end
end
delete '/session' do
session[:user_id] = nil
redirect '/login'
end
post '/like' do
redirect '/login' unless logged_in?
redirect "/ducks/#{params['duck_id']}" unless current_user.id == params['user_id']
like_duck(params['user_id'], params['duck_id'])
redirect "/ducks/#{params['duck_id']}"
end
post '/unlike' do
redirect '/login' unless logged_in?
redirect "/ducks/#{params['duck_id']}" unless current_user.id == params['user_id']
unlike_duck(params['user_id'], params['duck_id'])
redirect "/ducks/#{params['duck_id']}"
end
post '/comment' do
redirect '/login' unless logged_in?
redirect "/ducks/#{params['duck_id']}" unless current_user.id == params['user_id']
if params['content'].length > 255
redirect "/ducks/#{params['duck_id']}"
end
create_comment(params['content'], params['user_id'], params['duck_id'])
redirect "/ducks/#{params['duck_id']}"
end