-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
96 lines (80 loc) · 2 KB
/
main.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
# frozen_string_literal: true
require 'sinatra'
require 'sinatra/reloader'
require 'securerandom'
require 'pg'
enable :method_override
helpers do
def h(text)
Rack::Utils.escape_html(text)
end
end
class Memo
def self.prepare_db
return if @connection
@connection = PG.connect(host: 'localhost', user: 'postgres', dbname: 'memos', port: 5432)
@connection.prepare('read_db', 'SELECT * FROM memos;')
@connection.prepare('create_memo', 'INSERT INTO memos (memo_id, memo_name, memo_text) VALUES ($1, $2, $3);')
@connection.prepare('edit_memo', 'UPDATE memos SET memo_name = $1, memo_text = $2 WHERE memo_id = $3;')
@connection.prepare('delete_memo', 'DELETE FROM memos WHERE memo_id = $1;')
end
def self.read_db
@connection.exec_prepared('read_db')
end
def self.create_memo(memo_name, memo_id, memo_text)
@connection.exec_prepared('create_memo', [memo_id, memo_name, memo_text])
end
def self.edit_memo(memo_name, memo_id, memo_text)
@connection.exec_prepared('edit_memo', [memo_name, memo_text, memo_id])
end
def self.delete_memo(memo_id)
@connection.exec_prepared('delete_memo', [memo_id])
end
end
before do
Memo.prepare_db
end
get '/memos' do
@title = 'top'
@memos = Memo.read_db
erb :top
end
# ok
get '/memos/create_memo' do
@title = 'New memo'
erb :new_memo
end
# ok
get '/memos/:id/edit' do
@title = 'Edit memo'
memos = Memo.read_db
@memo_date = memos.find do |memo_date|
memo_date['memo_id'] == params[:id]
end
erb :edit_memo
end
# ok
patch '/memos/:id' do
Memo.edit_memo(params[:memo_name], params[:id], params[:memo_text])
redirect '/memos'
end
# ok
get '/memos/:id' do
@title = 'Show memo'
memos = Memo.read_db
@memo_date = memos.find do |memo_date|
memo_date['memo_id'] == params[:id]
end
erb :show_memo
end
# ok
delete '/memos/:id' do
Memo.delete_memo(params[:id])
redirect '/memos'
end
# ok
post '/memos' do
Memo.create_memo(params[:memo_name], SecureRandom.uuid, params[:memo_text])
redirect '/memos'
end
# ok