-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes_demo.rb
51 lines (42 loc) · 938 Bytes
/
routes_demo.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
require 'sinatra'
require 'json'
$db_file_path = 'database/users.json'
# User class
class User
def self.all_records
file = File.read($db_file_path)
file == '' ? [] : JSON.parse(file)
end
def self.find_record_by_id(id)
records = all_records
records.find { |record| record['id'] == id.to_i }
end
def self.create(attributes)
records = all_records
attributes = { 'id' => records.count + 1 }.merge(attributes)
records.push(attributes)
File.open($db_file_path, 'w') do |file|
file.write(records.to_json)
file.flush
end
attributes
end
end
get '/' do
'<h2>Welcome to profile checker</h2>'
end
get '/users' do
@users = User.all_records
erb :users
end
get '/users/new' do
erb :create_form
end
get '/users/:id' do
@user = User.find_record_by_id(params[:id]) || {}
erb :user
end
post '/users' do
@user = User.create(params)
redirect "/users/#{@user['id']}"
end