Permalink
Browse files

Allow working times to be added via the RESTful interface.

  • Loading branch information...
1 parent df02482 commit 9cea411dee920aaf7156b391dfafaafd1defc864 @leenookx committed Feb 12, 2011
Showing with 69 additions and 8 deletions.
  1. +69 −8 app/controllers/working_times_controller.rb
@@ -1,6 +1,10 @@
class WorkingTimesController < ApplicationController
+
+
+ # #####################################################
# GET /working_times
# GET /working_times.xml
+ # #####################################################
def index
@working_times = WorkingTime.all
@@ -10,8 +14,11 @@ def index
end
end
+
+ # #####################################################
# GET /working_times/1
# GET /working_times/1.xml
+ # #####################################################
def show
@working_time = WorkingTime.find(params[:id])
@@ -21,40 +28,75 @@ def show
end
end
+
+ # #####################################################
# GET /working_times/new
# GET /working_times/new.xml
+ # GET /working_times/new.json
+ # #####################################################
def new
@working_time = WorkingTime.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @working_time }
+ format.json { render :json => @working_time }
end
end
+
+ # #####################################################
# GET /working_times/1/edit
+ # #####################################################
def edit
@working_time = WorkingTime.find(params[:id])
end
+
+ # #####################################################
+ # Create a new record in the database.
+ #
# POST /working_times
# POST /working_times.xml
+ # POST /working_times.json
+ # #####################################################
def create
- @working_time = WorkingTime.new(params[:working_time])
- respond_to do |format|
- if @working_time.save
- format.html { redirect_to(@working_time, :notice => 'WorkingTime was successfully created.') }
- format.xml { render :xml => @working_time, :status => :created, :location => @working_time }
- else
- format.html { render :action => "new" }
- format.xml { render :xml => @working_time.errors, :status => :unprocessable_entity }
+ user = validate_user( request.headers["authentication-token"] || params[:auth_code] )
+ if user.nil?
+ respond_to do |format|
+ format.html do
+ flash[:error] = 'Not authenticated.'
+ redirect_to root_url
+ end
+
+ format.xml { render :xml => { :status => :error, :message => 'Invalid authentication code.'}.to_xml, :status => 403 }
+
+ format.json { render :json => { :status => :error, :message => 'Invalid authentication code.'}.to_json, :status => 403 }
+ end
+ else
+ @working_time = WorkingTime.new(params[:working_time])
+
+ respond_to do |format|
+ @working_time.user_id = user.id
+ if @working_time.save
+ format.html { redirect_to(@working_time, :notice => 'WorkingTime was successfully created.') }
+ format.xml { render :xml => @working_time, :status => :created, :location => @working_time }
+ format.json { render :json => @working_time, :status => :created, :location => @working_time }
+ else
+ format.html { render :action => "new" }
+ format.xml { render :xml => @working_time.errors, :status => :unprocessable_entity }
+ format.xml { render :json => @working_time.errors, :status => :unprocessable_entity }
+ end
end
end
end
+
+ # #####################################################
# PUT /working_times/1
# PUT /working_times/1.xml
+ # #####################################################
def update
@working_time = WorkingTime.find(params[:id])
@@ -69,8 +111,11 @@ def update
end
end
+
+ # #####################################################
# DELETE /working_times/1
# DELETE /working_times/1.xml
+ # #####################################################
def destroy
@working_time = WorkingTime.find(params[:id])
@working_time.destroy
@@ -80,4 +125,20 @@ def destroy
format.xml { head :ok }
end
end
+
+
+private
+
+ # #####################################################
+ #
+ # #####################################################
+ def validate_user(params)
+ return @user unless @user.nil?
+
+ if params && !params.empty?
+ return User.find_by_authentication_code( params )
+ else
+ return nil
+ end
+ end
end

0 comments on commit 9cea411

Please sign in to comment.