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
 class Api::V1::RecordsController < ApplicationController
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
   before_action :check_login
3
   before_action :check_login
4
 
4
 
5
   def index
5
   def index
10
     render json: RecordSerializer.new(@record).serializable_hash.to_json
10
     render json: RecordSerializer.new(@record).serializable_hash.to_json
11
   end
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
   private
31
   private
15
 
32
 
18
     params.require(:record).permit(:duration, :user_id, :task_id, :is_handled, :handled_by_id)
35
     params.require(:record).permit(:duration, :user_id, :task_id, :is_handled, :handled_by_id)
19
   end
36
   end
20
 
37
 
38
+  # def record_params_update
39
+  #   params.require(:record).permit(:is_handled, :handled_by_id)
40
+  # end
41
+
21
   def set_record
42
   def set_record
22
     @record = Task.find(params[:task_id]).records.find(params[:id])
43
     @record = Task.find(params[:task_id]).records.find(params[:id])
23
   end
44
   end

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

5
     @record = records(:one)
5
     @record = records(:one)
6
     @activity = activities(:one)
6
     @activity = activities(:one)
7
     @task = tasks(:one)
7
     @task = tasks(:one)
8
+    @user_two = users(:two)
8
   end
9
   end
9
 
10
 
10
   # INDEX
11
   # INDEX
41
     assert_response :forbidden
42
     assert_response :forbidden
42
   end
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
 end
72
 end

Loading…
Cancel
Save