forked from pgaspar/oqfc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.rb
87 lines (69 loc) · 1.91 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
require 'rubygems'
require 'bundler/setup'
require 'sinatra'
require 'sinatra/partial'
require 'escape_utils'
require 'sinatra/basic_auth'
require 'sinatra/content_for'
if development?
require 'sinatra/reloader'
also_reload './config/config.rb'
also_reload './models.rb'
end
require './config/config.rb'
require './models.rb'
# Filters
before '/' do
session[:order] = (params[:order] || session[:order] || 'score')
if session[:order] == 'recent'
@dm_order = [:created_at.desc]
else
session[:order] = 'score'
@dm_order = [:vote_score.desc, :created_at.asc]
end
redirect to '/#suggestions' if params[:order]
end
# Controllers
get '/' do
if session[:order] == 'recent'
@entries = Entry.all(:order => @dm_order)
else
@entries = Entry.all(:vote_score.gte => 0, :order => @dm_order)
@buried_entries = Entry.all(:vote_score.lt => 0, :order => @dm_order)
end
erb :index
end
post '/create_entry' do
entry = Entry.create(:text => EscapeUtils.escape_html(params[:suggestion]))
redirect back unless entry.saved?
entry.vote(request.ip)
redirect to "/#suggestion-#{entry.id}"
end
post '/vote' do
entry = Entry.get!(params[:entry_id]) rescue halt(404)
entry.vote(request.ip, params[:up] != 'false')
if params[:vote_page]
redirect to "/entry/#{entry.id}"
else
redirect to "/#suggestion-#{entry.id}"
end
end
get '/entry/:id' do
@entry = Entry.get!(params[:id]) rescue halt(404)
erb :entry
end
# Dashboard
protect do
get '/dash' do
@entries = Entry.page(params[:page], :per_page => 15, :order => [ ((params[:order].nil? || params[:order].empty?) ? :vote_score : params[:order].to_sym).desc, :created_at.desc ])
erb :dash, :layout => false
end
post '/dash/remove_entry' do
Entry.get!(params[:entry_id]).destroy rescue halt(404)
redirect to "/dash?order=#{params[:order]}&page=#{params[:page]}"
end
end
# Errors
not_found do
"<h1>Not Found.</h1>"
end