Browse Source

removed record model and controller

Lou 3 years ago
parent
commit
1a6bfa6df1

+ 0
- 45
app/controllers/api/v1/records_controller.rb View File

@@ -1,45 +0,0 @@
1
-class Api::V1::RecordsController < ApplicationController
2
-  before_action :set_record, only: %i[show update]
3
-  before_action :check_login
4
-
5
-  def index
6
-    render json: RecordSerializer.new(Task.find(params[:task_id]).records).serializable_hash.to_json
7
-  end
8
-
9
-  def show
10
-    render json: RecordSerializer.new(@record).serializable_hash.to_json
11
-  end
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
30
-
31
-  private
32
-
33
-  # Only allow a trusted parameter "white list" through.
34
-  def record_params
35
-    params.require(:record).permit(:duration, :user_id, :task_id, :is_handled, :handled_by_id)
36
-  end
37
-
38
-  # def record_params_update
39
-  #   params.require(:record).permit(:is_handled, :handled_by_id)
40
-  # end
41
-
42
-  def set_record
43
-    @record = Task.find(params[:task_id]).records.find(params[:id])
44
-  end
45
-end

+ 0
- 8
app/models/record.rb View File

@@ -1,8 +0,0 @@
1
-class Record < ApplicationRecord
2
-  belongs_to :user
3
-  belongs_to :task
4
-
5
-  validates :duration, presence: :true
6
-  validates :task, presence: true
7
-  validates :user, presence: true
8
-end

+ 0
- 13
db/migrate/20210511212634_create_records.rb View File

@@ -1,13 +0,0 @@
1
-class CreateRecords < ActiveRecord::Migration[6.1]
2
-  def change
3
-    create_table :records do |t|
4
-      t.integer :duration, null: false
5
-      t.references :user, null: false
6
-      t.references :task, null: false
7
-      t.boolean :is_handled, default: false
8
-      t.references :handled_by, default: nil, null: true
9
-
10
-      t.timestamps
11
-    end
12
-  end
13
-end

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

@@ -1,72 +0,0 @@
1
-require "test_helper"
2
-
3
-class Api::V1::RecordsControllerTest < ActionDispatch::IntegrationTest
4
-  setup do
5
-    @record = records(:one)
6
-    @activity = activities(:one)
7
-    @task = tasks(:one)
8
-    @user_two = users(:two)
9
-  end
10
-
11
-  # INDEX
12
-  test "should access index" do
13
-    get api_v1_activity_task_records_url(@activity, @task),
14
-    headers: { Authorization: JsonWebToken.encode(user_id: @record.user_id) },
15
-    as: :json
16
-    assert_response :success
17
-  end
18
-
19
-  test "should not access index" do
20
-    get api_v1_activity_task_records_url(@activity, @task),
21
-    as: :json
22
-    assert_response :forbidden
23
-  end
24
-
25
-  # SHOW
26
-  test "should access show" do
27
-    get api_v1_activity_task_record_url(@activity, @task, @record),
28
-    headers: { Authorization: JsonWebToken.encode(user_id: @record.user_id) },
29
-    as: :json
30
-    assert_response :success
31
-
32
-    json_response = JSON.parse(self.response.body)
33
-    assert_equal @record.duration, json_response['data']['attributes']['duration']
34
-    assert_equal @record.user_id, json_response['data']['attributes']['user_id']
35
-    assert_equal @record.task_id, json_response['data']['attributes']['task_id']
36
-    assert_equal @record.is_handled, json_response['data']['attributes']['is_handled']
37
-  end
38
-
39
-  test "should not access show" do
40
-    get api_v1_activity_task_record_url(@activity, @task, @record),
41
-    as: :json
42
-    assert_response :forbidden
43
-  end
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
72
-end

+ 0
- 13
test/fixtures/records.yml View File

@@ -1,13 +0,0 @@
1
-# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2
-
3
-one:
4
-  duration: 666
5
-  user: one
6
-  task: one
7
-  is_handled: false
8
-
9
-two:
10
-  duration: 100
11
-  user: one
12
-  task: two 
13
-  is_handled: yes

+ 0
- 24
test/models/record_test.rb View File

@@ -1,24 +0,0 @@
1
-require "test_helper"
2
-
3
-class RecordTest < ActiveSupport::TestCase
4
-  setup do
5
-    @user = users(:one)
6
-    @task = tasks(:one)
7
-    @record = records(:one)
8
-  end
9
-
10
-  test "record should have valid duration, user and task ids" do
11
-    record = Record.new(duration: 100, user_id: @user.id, task_id: @task.id)
12
-    assert record.valid?
13
-  end
14
-
15
-  test "record without a duration should not be valid" do
16
-    record = Record.new(user_id: @user.id, task_id: @task.id)
17
-    assert_not record.valid?
18
-  end
19
-
20
-  test "record without user and task ids should not be valid" do
21
-    record = Record.new(duration: 100)
22
-    assert_not record.valid?
23
-  end
24
-end

Loading…
Cancel
Save