-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontroller.rb
160 lines (131 loc) · 3.32 KB
/
controller.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
require_relative 'service.rb'
require 'sinatra/base'
require 'sinatra'
class Sinatra::Request
def user
@user
end
def user= user_object
@user = user_object
end
end
class BlogController < Sinatra::Base
enable :sessions
helpers do
def _add_user request, user=nil
session[:user] ||= nil
session_user = session[:user]
session_user ||= user
request.user = session_user
end
def require_login request
_add_user request
raise if (request.user).nil?
end
def require_admin request
require_login request
raise if !request.user.is_admin
end
end
get '/' do
_add_user request
StaticsService.new.index request
end
get '/about/' do
_add_user request
StaticsService.new.about request
end
get '/posts/' do
_add_user request
PostsService.new.render_all request
end
get '/posts/page/:page/' do |page|
_add_user request
PostsService.new.render_for_page request, page.to_i
end
get '/posts/:post_id/' do |post_id|
_add_user request
PostsService.new.render_by_id request, post_id
end
get %r{/posts/(?<year>\d{4})/(?<month>\d{2})/(?<day>\d{2})/?} do
_add_user request
date = Date.new params[:year].to_i,
params[:month].to_i,
params[:day].to_i
PostsService.new.render_by_date request, date
end
post '/posts/:post_id/comments/create/' do |post_id|
begin
_add_user request
CommentCreateService.new.create_for post_id,
params['posted_by'],
params['url'],
params['body']
ensure
redirect "/posts/#{post_id}/"
end
end
get '/posts/category/:category/' do |category|
_add_user request
PostsService.new.render_by_category request, category
end
get '/post/create/' do
require_admin request
PostCreateService.new.create_form request
end
post '/post/create/' do
require_admin request
begin
PostCreateService.new.create params['title'],
params['body'],
params['categories'].split(',')
ensure
redirect '/posts/'
end
end
get '/user/create/' do
UserCreateService.new.create_form request
end
post '/user/create/' do
begin
UserCreateService.new.create params['name'],
params['password'],
params['email']
ensure
redirect '/user/login/'
end
end
get '/user/login/' do
UserLoginService.new.create_form request
end
get '/user/logout/' do
request.user = nil
session[:user] = nil
redirect '/posts/'
end
post '/user/login/' do
begin
user = UserLoginService.new.login(
params['name'],
params['password']
)
session[:user] = user
_add_user request, user
redirect '/posts/'
rescue
redirect '/user/login/retry/'
end
end
get '/user/login/retry/' do
UserLoginRetryService.new.create_form request
end
not_found do
StaticsService.new.not_found request
end
error do
session[:user] = nil
super
end
set :public_folder, File.dirname(__FILE__) + '/media'
end
BlogController.run!