-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
81 lines (61 loc) · 1.65 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
require 'rubygems'
require 'sinatra'
require 'sinatra/reloader'
require 'sqlite3'
def init_db
@db = SQLite3::Database.new 'leprosorium.db'
@db.results_as_hash = true
end
before do
init_db
end
configure do
init_db
@db.execute 'CREATE TABLE IF NOT EXISTS Posts
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_date DATE,
content TEXT
)'
@db.execute 'CREATE TABLE IF NOT EXISTS Comments
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_date DATE,
content TEXT,
post_id INTEGER
)'
end
get '/' do
@results = @db.execute 'SELECT * FROM POSTS ORDER BY id DESC'
erb :index
end
get '/NewPost' do
erb :NewPost
end
post '/NewPost' do
content = params[:content]
if content.length == 0
@error = "Type post text"
return erb :NewPost
end
@db.execute 'INSERT INTO Posts (content, created_date) VALUES (?, datetime())', [content]
#После добавления каждого нового поста перенаправление на главную
redirect to '/'
end
get '/details/:post_id' do
#получаем переменную из URL
post_id = params[:post_id]
#получаем пост из базы
results = @db.execute 'SELECT * FROM POSTS WHERE id = ?', [post_id]
@row = results[0]
@comments = @db.execute 'SELECT * FROM Comments WHERE post_id = ? ORDER BY id', [post_id]
erb :details
end
post '/details/:post_id' do
post_id = params[:post_id]
content = params[:content]
@db.execute 'INSERT INTO Comments
(content, created_date, post_id)
VALUES (?, datetime(), ?)', [content, post_id]
redirect to('details/' + post_id)
end