Created
June 5, 2015 14:13
-
-
Save Rosa-Fox/db77f151caf275370d74 to your computer and use it in GitHub Desktop.
server.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
require 'data_mapper' | |
load 'datamapper_setup.rb' | |
class Todo | |
include DataMapper::Resource | |
property :id, Serial # An auto-increment integer key | |
property :title, String | |
property :description,String | |
property :complete, Boolean | |
def completed | |
self.complete = true | |
self.save | |
end | |
end | |
class MindTheCodeApp < Sinatra::Application | |
set :partial_template_engine, :erb | |
set :static, true | |
#Display all today | |
get '/todo' do | |
@todos = Todo.all | |
erb :'todos/index' | |
end | |
#Make a new todo | |
get '/todo/new' do | |
erb :'todos/new' | |
end | |
#Show each to do | |
get '/todo/:id' do |id| | |
@todo = Todo.get!(id) | |
erb :'todos/show' | |
end | |
#Edit each to do | |
get '/todo/:id/edit' do |id| | |
@todo = Todo.get!(id) | |
erb :'todos/edit' | |
end | |
# POST = how a client tells a server to add an entity as a child of the object | |
#identified by the URI. | |
#The entity that the client expects the server to add is transmitted in the request body. | |
post '/todo' do | |
todo = Todo.new(params[:todo]) | |
if todo.save | |
redirect '/todo' | |
else | |
redirect '/todo/new' | |
end | |
end | |
# PUT is used for updating records in the DB | |
# A request using the PUT HTTP verb should act upon a single resource within the collection; | |
put '/todo/:id' do |id| | |
todo = Todo.get!(id) | |
success = todo.update!(params[:todo]) | |
if success | |
redirect "/todo/#{id}" | |
else | |
redirect "/todo/#{id}/edit" | |
end | |
end | |
#delete verb to delete record in the db | |
delete '/todo/:id' do |id| | |
todo = Todo.get!(id) | |
todo.destroy! | |
redirect "/todo" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment