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.

58 lines
1.1 KiB

13 years ago
13 years ago
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. belongs_to :list
  11. end
  12. class List
  13. include DataMapper::Resource
  14. property :id, Serial
  15. property :name, String, required: true
  16. has n, :tasks, :constraint => :destroy
  17. end
  18. DataMapper.finalize
  19. get '/' do
  20. @lists = List.all(:order => [:name])
  21. haml :index
  22. end
  23. post '/:id' do
  24. List.get(params[:id]).tasks.create params['task']
  25. redirect '/'
  26. end
  27. delete '/task/:id' do
  28. Task.get(params[:id]).destroy
  29. redirect '/'
  30. end
  31. put '/task/:id' do
  32. task = Task.get params[:id]
  33. task.completed_at = task.completed_at.nil? ? Time.now : nil
  34. task.save
  35. redirect '/'
  36. end
  37. get '/:task' do
  38. @task = params[:task].split('-').join(' ').capitalize
  39. # haml :task
  40. end
  41. post '/new/list' do
  42. List.create params['list']
  43. redirect '/'
  44. end
  45. delete '/list/:id' do
  46. List.get(params[:id]).destroy
  47. redirect '/'
  48. end