mirror of
				https://github.com/fspc/BikeShed-1.git
				synced 2025-10-30 16:35:35 -04:00 
			
		
		
		
	Added api/time_entry/delete method
This commit is contained in:
		
							parent
							
								
									24facd4dee
								
							
						
					
					
						commit
						d94c436ba5
					
				| @ -1,5 +1,6 @@ | ||||
| class Api::V1::TimeEntriesController < Api::V1::BaseController | ||||
|   EXPECTED_TIME_ENTRY = "Expected time entry in submitted data" | ||||
|   NOT_FOUND = "Time entry not found" | ||||
| 
 | ||||
|   def create | ||||
|     if params[:time_entries] && time_entry = params[:time_entries].first | ||||
| @ -19,4 +20,13 @@ class Api::V1::TimeEntriesController < Api::V1::BaseController | ||||
|       render json: { errors: [EXPECTED_TIME_ENTRY]}, status: 422 and return | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|   def delete | ||||
|     if time_entry = TimeEntry.find_by_id(params[:id]) | ||||
|       time_entry.delete | ||||
|       render nothing: true, status: 204 and return | ||||
|     else | ||||
|       render json: { errors: [NOT_FOUND]}, status: 404 and return | ||||
|     end | ||||
|   end | ||||
| end | ||||
|  | ||||
| @ -31,6 +31,7 @@ Velocipede::Application.routes.draw do | ||||
|       put  'tasks/update' => "tasks#update", as: "api_update_task" | ||||
| 
 | ||||
|       post 'time_entries/create' => "time_entries#create", as: "api_create_time_entry" | ||||
|       delete 'time_entries/:id' => "time_entries#delete", as: "api_delete_time_entry" | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|  | ||||
| @ -77,4 +77,41 @@ describe Api::V1::TimeEntriesController do | ||||
|       end | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|   describe "#delete" do | ||||
|     context "as a user" do | ||||
|       let!(:user){ FactoryGirl.create(:user) } | ||||
| 
 | ||||
|       before(:each) do | ||||
|         sign_in user | ||||
|       end | ||||
| 
 | ||||
|       context "entry does not exist" do | ||||
|         it "returns 404" do | ||||
|           delete :delete, id: 9000 | ||||
|           expect(@response.code.to_i).to eql 404 | ||||
|         end | ||||
| 
 | ||||
|         it "returns not found" do | ||||
|           delete :delete, id: 9000 | ||||
|           json = JSON.parse(@response.body) | ||||
|           expect(json).to have_key("errors") | ||||
|           expect(json.to_s).to include("not found") | ||||
|         end | ||||
|       end | ||||
| 
 | ||||
|       context "entry exists" do | ||||
|         let!(:entry){ FactoryGirl.create(:time_entry) } | ||||
| 
 | ||||
|         it "deletes the time entry" do | ||||
|           expect{delete :delete, id: entry.id}.to change{TimeEntry.count} | ||||
|         end | ||||
| 
 | ||||
|         it "returns 204" do | ||||
|           delete :delete, id: entry.id | ||||
|           expect(@response.code.to_i).to eql 204 | ||||
|         end | ||||
|       end | ||||
|     end | ||||
|   end | ||||
| end | ||||
|  | ||||
							
								
								
									
										10
									
								
								spec/factories/time_entries.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								spec/factories/time_entries.rb
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,10 @@ | ||||
| FactoryGirl.define do | ||||
|   factory :time_entry do | ||||
|     loggable_type User.to_s | ||||
|     logger_type User.to_s | ||||
|     start_date Time.now | ||||
|     end_date Time.now | ||||
|     log_action_id 1 | ||||
|     log_action_type ActsAsLoggable::UserAction.to_s | ||||
|   end | ||||
| end | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user