-
Notifications
You must be signed in to change notification settings - Fork 0
/
blog.rb
116 lines (97 loc) · 2.26 KB
/
blog.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
require "sinatra"
require "data_mapper"
use Rack::MethodOverride
DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/blog.db")
class Blog
include DataMapper::Resource
property :id, Serial
property :title, String
property :body, Text
end
Blog.auto_upgrade!
DataMapper.setup(:comments, "sqlite3://#{Dir.pwd}/comment.db")
class Comment
include DataMapper::Resource
property :id, Serial
property :blog_id, Integer
property :author, String
property :email, String
property :title, String
property :body, Text
end
DataMapper.repository(:comments) {
Comment.auto_upgrade!
}
enable :sessions
get "/" do
@posts = Blog.all(:order => [:id.desc])
erb :home, layout: :blog_template
end
post "/login" do
if params[:username] == 'temp_auth' && params[:password] == 'oogaboogaboo'
session[:my_session_id] = "durr"
redirect back
else
redirect back
end
end
get "/create" do
if session[:my_session_id] == "durr"
erb :create, layout: :blog_template
else
erb :login, layout: :blog_template
end
end
post "/create" do
Blog.create(
title: params[:title],
body: params[:body]
)
redirect to("/")
end
get "/view/:id" do |blog_id|
@post = Blog.get(blog_id)
DataMapper.repository(:comments) {
@comments = Comment.all(blog_id: blog_id, order: [:id.desc])
}
erb :view, layout: :blog_template
end
post "/view/comment/:id" do |post_id|
DataMapper.repository(:comments) {
Comment.create(
blog_id: post_id,
author: params[:name],
email: params[:email],
title: params[:title],
body: params[:body]
)
}
redirect back
end
get "/admin" do
if session[:my_session_id] == "durr"
@posts = Blog.all(order: [:id.desc])
erb :admin, layout: :blog_template
else
erb :login, layout: :blog_template
end
end
delete "/admin/delete/:id" do |blog_id|
if session[:my_session_id] == "durr"
Blog.get(blog_id).destroy
DataMapper.repository(:comments) {
Comment.all(blog_id: blog_id).destroy
}
redirect to("/admin")
else
erb :login, layout: :blog_template
end
end
get "/admin/edit/:id" do |blog_id|
if session[:my_session_id] == "durr"
# TODO: allow the user to edit the title
erb :admin_edit, layout: :blog_template
else
erb :login, layout: :blog_template
end
end