-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
102 lines (81 loc) · 2.02 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
require "sinatra"
require "sinatra/reloader" if development?
require_relative "models/user"
require_relative "models/post"
require_relative "models/vote"
require_relative "lib/lib"
enable :sessions
get "/" do
posts = all_post
votes = all_vote
erb(:'/posts/index', locals: {posts: posts, votes: votes})
end
get "/about" do
erb :about
end
get "/login" do
erb :'/sessions/login'
end
post "/login" do
if params[:email] != '' && params[:password] != ''
user = find_one_user_by_email(params[:email])
else
user = false
end
if user && BCrypt::Password.new(user["password_digest"]) == params[:password]
session[:user_id] = user["id"]
redirect "/"
else
erb :'/sessions/login'
end
end
delete "/logout" do
session[:user_id] = nil
redirect "/"
end
# Sign up
get "/users/new" do
erb :'/users/new'
end
post "/users" do
if params[:username] != '' && params[:email] != '' && params[:password] != ''
if params[:password] == params[:confirm_password]
create_user(params[:email], params[:password], params[:username])
redirect "/login"
else
redirect "/users/new"
end
else
redirect "/users/new"
end
end
get "/users/:id/posts/new" do
erb :'/posts/new'
end
post "/users/:id/posts/new" do
create_post(params[:content], current_user["id"])
redirect "/"
end
get "/users/:id/posts" do
posts = find_all_post_by_user(params[:id])
erb(:'/posts/show', locals: {posts: posts})
end
delete "/users/:id/posts" do
delete_post(params[:post_id])
redirect "/users/#{ current_user["id"] }/posts"
end
post "/votes/:post_id/new" do
current_user_votes = post_votes_by_user(current_user["id"])
post_votes = current_user_votes.select { |vote| vote["post_id"] == params["post_id"] }
if post_votes.count != 0
delete_vote(params[:post_id])
redirect "/"
else
create_vote(params[:post_id], current_user["id"])
redirect "/"
end
end
# post "/posts/:id/edit" do
# update_post(params[:id], params[:upvote], params[:downvote], current_user["id"])
# redirect "/"
# end