Sitepoint demo app for learning to use Sinatra
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

41 lines
732 B

13 years ago
13 years ago
13 years ago
  1. require 'sinatra'
  2. require 'data_mapper'
  3. require 'haml'
  4. DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/development.db")
  5. class Task
  6. include DataMapper::Resource
  7. property :id, Serial
  8. property :name, String, required: true
  9. property :completed_at, DateTime
  10. end
  11. DataMapper.finalize
  12. get '/' do
  13. @tasks = Task.all
  14. haml :index
  15. end
  16. post '/' do
  17. Task.create params[:task]
  18. redirect '/'
  19. end
  20. delete '/task/:id' do
  21. Task.get(params[:id]).destroy
  22. redirect '/'
  23. end
  24. put '/task/:id' do
  25. task = Task.get params[:id]
  26. task.completed_at = task.completed_at.nil? ? Time.now : nil
  27. task.save
  28. redirect '/'
  29. end
  30. get '/:task' do
  31. @task = params[:task].split('-').join(' ').capitalize
  32. # haml :task
  33. end