-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo_app.rb
52 lines (41 loc) · 947 Bytes
/
todo_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
require 'sinatra'
require 'data_mapper'
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/development.db")
class Tarefa
include DataMapper::Resource
property :id, Serial
property :title, String, :required => true
property :details, String
property :completed, Boolean
end
DataMapper.finalize.auto_upgrade!
get '/' do
@tarefas = Tarefa.all
erb :index
end
post '/' do
Tarefa.create params[:tarefa]
redirect to('/')
end
get '/tarefas/:id' do |id|
@tarefa = Tarefa.get!(id)
erb :'tarefas/show'
end
get '/tarefas/:id/edit' do |id|
@tarefa = Tarefa.get!(id)
erb :'tarefas/edit'
end
put '/tarefas/:id' do |id|
tarefa = Tarefa.get!(id)
success = tarefa.update!(params[:tarefa])
if success
redirect "/tarefas/#{id}"
else
redirect "/tarefas/#{id}/edit"
end
end
delete '/tarefas/:id' do |id|
tarefa = Tarefa.get!(id)
tarefa.destroy!
redirect "/"
end