/
app.rb
64 lines (55 loc) · 1.48 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
# frozen_string_literal: true
require 'sinatra'
require 'sinatra/reloader'
require 'json'
require 'securerandom'
helpers do
def h(text)
Rack::Utils.escape_html(text)
end
end
get '/memos' do
File.open('json/memos.json', 'r') do |file|
@memos = JSON.parse(file.read)
end
erb :memos
end
get '/memos/new' do
erb :create_memo
end
post '/memos' do
hash = File.open('json/memos.json') { |file| JSON.parse(file.read) }
hash[SecureRandom.uuid] = { 'title' => params[:title], 'memo' => params[:memo] }
json = JSON.pretty_generate(hash)
File.open('json/memos.json', 'w') { |file| file.puts(json) }
redirect to('/memos')
end
get '/memos/:id' do |id|
File.open('json/memos.json', 'r') do |file|
memos = JSON.parse(file.read)
@memo = memos[id]
@id = id
end
erb :show_memo
end
delete '/memos/:id' do |id|
hash = File.open('json/memos.json') { |file| JSON.parse(file.read) }
hash.delete(id)
json = JSON.pretty_generate(hash)
File.open('json/memos.json', 'w') { |file| file.puts(json) }
redirect to('/memos')
end
get '/memos/:id/edit' do |id|
hash = File.open('json/memos.json') { |file| JSON.parse(file.read) }
@memo = hash[id]
@id = id
erb :edit_memo
end
patch '/memos/:id' do |id|
hash = File.open('json/memos.json') { |file| JSON.parse(file.read) }
hash[id]['title'] = params[:title]
hash[id]['memo'] = params[:memo]
json = JSON.pretty_generate(hash)
File.open('json/memos.json', 'w') { |file| file.puts(json) }
redirect to('/memos')
end