浏览代码

Adds index and show defs, and relatives unit tests

Lou 3 年前
父节点
当前提交
825c392b37
共有 4 个文件被更改,包括 65 次插入0 次删除
  1. 22
    0
      app/controllers/api/v1/tasks_controller.rb
  2. 6
    0
      app/serializers/task_serializer.rb
  3. 二进制
      erd.pdf
  4. 37
    0
      test/controllers/api/v1/tasks_controller_test.rb

+ 22
- 0
app/controllers/api/v1/tasks_controller.rb 查看文件

@@ -0,0 +1,22 @@
1
+class Api::V1::TasksController < ApplicationController
2
+  before_action :set_task, only: %i[show update destroy]
3
+  before_action :check_login
4
+
5
+  def index
6
+    render json: TaskSerializer.new(Task.all).serializable_hash.to_json
7
+  end
8
+
9
+  def show
10
+    render json: TaskSerializer.new(@task).serializable_hash.to_json
11
+  end
12
+
13
+  private
14
+
15
+  def task_params
16
+    params.require(:task).permit(:name, :description, :user_id, :activity_id)
17
+  end
18
+
19
+  def set_task
20
+    @task = Task.find(params[:id])
21
+  end
22
+end

+ 6
- 0
app/serializers/task_serializer.rb 查看文件

@@ -0,0 +1,6 @@
1
+class TaskSerializer
2
+  include JSONAPI::Serializer
3
+  attributes :name, :description, :user_id, :activity_id
4
+
5
+  cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
6
+end

二进制
erd.pdf 查看文件


+ 37
- 0
test/controllers/api/v1/tasks_controller_test.rb 查看文件

@@ -0,0 +1,37 @@
1
+require "test_helper"
2
+
3
+class Api::V1::TasksControllerTest < ActionDispatch::IntegrationTest
4
+  setup do
5
+    @task = tasks(:one)
6
+    @activity = activities(:one)
7
+    @user = users(:one)
8
+  end
9
+
10
+  # INDEX
11
+  test "should access index" do
12
+    get api_v1_activity_tasks_url(@activity),
13
+    headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
14
+    as: :json
15
+    assert_response :success
16
+  end
17
+
18
+  test "should not access index" do
19
+    get api_v1_activity_tasks_url(@activity),
20
+    as: :json
21
+    assert_response :forbidden
22
+  end
23
+
24
+  # SHOW
25
+  test "should access show" do
26
+    get api_v1_activity_task_url(@activity, @task),
27
+    headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
28
+    as: :json
29
+    assert_response :success
30
+  end
31
+
32
+  test "should not access show" do
33
+    get api_v1_activity_task_url(@activity, @task),
34
+    as: :json
35
+    assert_response :forbidden
36
+  end
37
+end

正在加载...
取消
保存