-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
executable file
·187 lines (171 loc) · 3.82 KB
/
app.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
require 'bundler'
Bundler.require
enable :sessions
DB = Sequel.connect(ENV['DATABASE_URL'] || 'sqlite://db/main.db')
require './models.rb'
def login?
if session['id'] == nil
redirect '/user/login'
end
end
# READ
get '/' do
login?
@posts = Post.reverse_order(:likes)
erb :index
end
post '/post/new' do
post = Post.new
post.title = params[:title]
post.description = params[:desc]
post.user_id = session['id']
post.date = DateTime.now
post.voted = ", "
post.likes = 0
post.save
redirect '/'
end
post '/user/create' do
if params[:password].to_s == params[:passwordcheck].to_s && User.where(:username => params[:username]).count == 0
u = User.new
u.username = params[:username]
u.password = BCrypt::Password.create(params[:password].to_s)
u.save
u = User.first(:username => params[:username])
session['id'] = u.id
redirect "/welcome/#{u.username}/2"
else
redirect '/user/signup'
end
end
get '/user/login' do
erb :login
end
get '/user/signup' do
erb :signup
end
post '/user/authenticate' do
u = User.first(:username => params[:username])
if u == nil
redirect '/user/login'
elsif BCrypt::Password.new(u.password.to_s) == params[:password]
session['id'] = u.id
redirect "/welcome/#{u.username}/1"
else
redirect '/user/login'
end
end
get '/welcome/:name/:test' do
login?
@name = params[:name]
@test = params[:test]
erb :welcome
end
get '/user/signout' do
login?
session['id'] = nil
redirect '/user/login'
end
get '/users' do
login?
@users = User.all
@likes =[]
@users.each_with_index do |user, i|
posts = Post.where(:user_id => user.id)
discs = Disc.where(:author_id => user.id)
likes = 0
posts.each do |post|
likes += post.likes
end
discs.each do |post|
likes += post.likes
end
@likes[i] = likes
end
erb :users
end
get '/post/view/:id' do
login?
@post = Post.first(:id => params[:id])
@discs = Disc.where(:post_id => @post.id).reverse_order!(:likes)
erb :postview
end
get '/disc/like/:id' do
login?
d = Disc.first(:id => params[:id].to_i)
new = d.voted.split(', ')
if new.include?(session["id"].to_s) == true
d.likes -= 1
new.delete(session["id"].to_s)
d.voted = new.join(", ")
d.save
redirect "/post/view/#{d.post_id}"
else
d.likes += 1
new.push(session["id"])
d.voted = new.join(", ")
d.save
redirect "/post/view/#{d.post_id}"
end
end
get '/post/like/:id/:user' do
login?
d = Post.first(:id => params[:id])
new = d.voted.split(', ')
if new.include?(session["id"].to_s) == true
d.likes -= 1
new.delete(session["id"].to_s)
d.voted = new.join(", ")
d.save
if params[:user].to_i == 2
redirect "/"
else
redirect "/user/view/#{d.user_id}"
end
else
d.likes += 1
new.push(session["id"])
d.voted = new.join(", ")
d.save
if params[:user].to_i == 2
redirect "/"
else
redirect "/user/view/#{d.user_id}"
end
end
end
post '/disc/new/:id' do
disc = Disc.new
disc.voted = ""
disc.likes = 0
disc.title = params[:title]
disc.message = params[:message]
disc.author_id = session['id']
disc.post_id = params[:id]
disc.date = DateTime.now
disc.save
post = Post.first(:id => params[:id])
redirect "/post/view/#{post.id}"
end
get '/user/view/:name' do
login?
@user = User.first(:id => params[:name])
@posts = Post.where(:user_id => @user.id).reverse_order!(:likes)
@discs = Disc.where(:author_id => @user.id).reverse_order!(:likes)
@likes = 0
@posts.each do |post|
@likes += post.likes
end
@discs.each do |post|
@likes += post.likes
end
erb :userview
end
post '/user/search' do
login?
if User.first(:username => params[:user]) != nil
redirect "/user/view/#{User.first(:username => params[:user]).id}"
else
redirect "/users"
end
end