Browse Source

working on update def

Lou 3 years ago
parent
commit
ee6916d0f5

+ 22
- 1
app/controllers/api/v1/records_controller.rb View File

@@ -1,5 +1,5 @@
1 1
 class Api::V1::RecordsController < ApplicationController
2
-  before_action :set_record, only: %i[show update destroy]
2
+  before_action :set_record, only: %i[show update]
3 3
   before_action :check_login
4 4
 
5 5
   def index
@@ -10,6 +10,23 @@ class Api::V1::RecordsController < ApplicationController
10 10
     render json: RecordSerializer.new(@record).serializable_hash.to_json
11 11
   end
12 12
   
13
+  def create
14
+    record = current_user.records.build(record_params)
15
+    record.task_id = params[:task_id]
16
+    if record.save
17
+      render json: RecordSerializer.new(record).serializable_hash.to_json, status: :created
18
+    else
19
+      render json: { errors: record.errors }, status: :unprocessable_entity
20
+    end
21
+  end
22
+
23
+  def update
24
+    if @record.update(handled_by_id = current_user.id)
25
+      render json: RecordSerializer.new(@record).serializable_hash.to_json, status: :ok
26
+    else
27
+      render json: @task.errors, status: :unprocessable_entity
28
+    end
29
+  end
13 30
 
14 31
   private
15 32
 
@@ -18,6 +35,10 @@ class Api::V1::RecordsController < ApplicationController
18 35
     params.require(:record).permit(:duration, :user_id, :task_id, :is_handled, :handled_by_id)
19 36
   end
20 37
 
38
+  # def record_params_update
39
+  #   params.require(:record).permit(:is_handled, :handled_by_id)
40
+  # end
41
+
21 42
   def set_record
22 43
     @record = Task.find(params[:task_id]).records.find(params[:id])
23 44
   end

+ 28
- 0
test/controllers/api/v1/records_controller_test.rb View File

@@ -5,6 +5,7 @@ class Api::V1::RecordsControllerTest < ActionDispatch::IntegrationTest
5 5
     @record = records(:one)
6 6
     @activity = activities(:one)
7 7
     @task = tasks(:one)
8
+    @user_two = users(:two)
8 9
   end
9 10
 
10 11
   # INDEX
@@ -41,4 +42,31 @@ class Api::V1::RecordsControllerTest < ActionDispatch::IntegrationTest
41 42
     assert_response :forbidden
42 43
   end
43 44
 
45
+  # CREATE
46
+  test "should create task" do
47
+    assert_difference("Record.count") do
48
+      post api_v1_activity_task_records_url(@activity, @task),
49
+      params: { record: { duration: @record.duration, user_id: @record.user_id, task_id: @record.task_id } },
50
+      headers: { Authorization: JsonWebToken.encode(user_id: @record.user_id) },
51
+      as: :json
52
+    end
53
+    assert_response :created
54
+  end
55
+
56
+  test "should not create task" do
57
+    assert_no_difference("Record.count") do
58
+      post api_v1_activity_task_records_url(@activity, @task),
59
+      params: { record: { duration: @record.duration, user_id: @record.user_id, task_id: @record.task_id } },
60
+      as: :json
61
+    end
62
+    assert_response :forbidden
63
+  end
64
+
65
+  # UPDATE
66
+  test "should update task" do
67
+    patch api_v1_activity_task_record_url(@activity, @task, @record),
68
+    params: { task: { is_handled: true } },
69
+    headers: { Authorization: JsonWebToken.encode(user_id: @user_two.id) },
70
+    as: :json
71
+  end
44 72
 end

Loading…
Cancel
Save