소스 검색

methods index show create pour le controller JoinedTeamUser

Lou 3 년 전
부모
커밋
4d1ff2a451

+ 35
- 0
app/controllers/api/v1/joined_team_users_controller.rb 파일 보기

@@ -0,0 +1,35 @@
1
+class Api::V1::JoinedTeamUsersController < ApplicationController
2
+  before_action :set_joinedTeamUsers, only: %i[show update destroy]
3
+  before_action :check_login
4
+
5
+  def index
6
+    render json: JoinedTeamUserSerializer.new(JoinedTeamUser.all).serializable_hash.to_json
7
+  end
8
+
9
+  def show
10
+    render json: JoinedTeamUserSerializer.new(@joinedTeamUser).serializable_hash.to_json
11
+  end
12
+
13
+  def create
14
+    userId = User.find_by(username: params[:joinedTeamUser][:username]).id
15
+    teamId = Team.find_by(name: params[:joinedTeamUser][:team_name]).id
16
+    jtu = JoinedTeamUser.new(team_id: teamId, user_id: userId, can_edit: false)
17
+    
18
+    if jtu.save
19
+      render json: JoinedTeamUserSerializer.new(jtu).serializable_hash.to_json, status: :created
20
+    else
21
+      render json: { errors: jtu.errors }, status: :unprocessable_entity
22
+    end
23
+  end
24
+
25
+  private
26
+
27
+  def joinedTeamUsers_params
28
+    params.require(:joinedTeamUser).permit(:team_id, :user_id, :can_edit, :created_at, :updated_at)
29
+  end
30
+  
31
+  def set_joinedTeamUsers
32
+    @joinedTeamUser = JoinedTeamUser.find(params[:id])
33
+  end
34
+
35
+end

+ 4
- 3
app/models/joined_team_user.rb 파일 보기

@@ -1,7 +1,8 @@
1 1
 class JoinedTeamUser < ApplicationRecord
2
+  # validates_associated :team, on: :create
3
+  # validates_associated :user, on: :create
4
+  validates_uniqueness_of :user_id, scope: :team_id, message: "user already part of this team"
5
+
2 6
   belongs_to :user
3 7
   belongs_to :team
4
-
5
-  validates_associated :user, on: :create
6
-  validates_associated :team, on: :create
7 8
 end

+ 6
- 0
app/serializers/joined_team_user_serializer.rb 파일 보기

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

+ 1
- 0
config/routes.rb 파일 보기

@@ -4,6 +4,7 @@ Rails.application.routes.draw do
4 4
       resources :users
5 5
       resources :tokens, only: %i[create]
6 6
       resources :teams
7
+      resources :joined_team_users
7 8
       resources :activities do
8 9
         resources :tasks
9 10
       end

+ 1
- 1
db/seeds.rb 파일 보기

@@ -42,7 +42,7 @@ end
42 42
     team_id: Team.all.sample.id,
43 43
     can_edit: true
44 44
   )
45
-  puts "USER ##{joined_team_users.user_id} joined TEAM ##{joined_team_users.team_id}"
45
+  # puts "USER ##{joined_team_users.user_id} joined TEAM ##{joined_team_users.team_id}"
46 46
 end
47 47
 
48 48
 User.create! username: "admin", email: "admin@email.com", password: "azerty", is_admin: true

+ 59
- 0
test/controllers/api/v1/joined_team_users_controller_test.rb 파일 보기

@@ -0,0 +1,59 @@
1
+require "test_helper"
2
+
3
+class Api::V1::JoinedTeamUsersControllerTest < ActionDispatch::IntegrationTest
4
+  setup do
5
+    @joinedTeamUser = joined_team_users(:one)
6
+    @user = users(:one)
7
+    @team = teams(:one)
8
+  end
9
+
10
+  # INDEX
11
+  test "should access JoinedTeamUser index" do
12
+    get api_v1_joined_team_users_url,
13
+    headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, 
14
+    as: :json
15
+    assert_response :success
16
+  end
17
+
18
+  test "should forbid JoinedTeamUser index" do
19
+    get api_v1_joined_team_users_url,
20
+    as: :json
21
+    assert_response :forbidden
22
+  end
23
+
24
+  # SHOW
25
+  test "should show JoinedTeamUser" do
26
+    get api_v1_joined_team_users_url(@joinedTeamUser),
27
+    headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, 
28
+    as: :json
29
+    assert_response :success
30
+  end
31
+
32
+  test "should forbid show JoinedTeamUser" do
33
+    get api_v1_joined_team_users_url(@joinedTeamUser),
34
+    as: :json
35
+    assert_response :forbidden
36
+  end
37
+
38
+  # CREATE
39
+  test "should create joinedTeamUser" do
40
+    assert_difference("JoinedTeamUser.count") do
41
+    post api_v1_joined_team_users_url,
42
+    params: { joinedTeamUser: { team_id: @team.id, user_id: @user.id, can_edit: false } },
43
+    headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, 
44
+    as: :json
45
+    end
46
+    assert_response :created
47
+  end
48
+
49
+  test "should forbid create joinedTeamUser" do
50
+    assert_no_difference("JoinedTeamUser.count") do
51
+      post api_v1_joined_team_users_url,
52
+      params: { joinedTeamUser: { team_id: @team.id, user_id: @user.id, can_edit: false } },
53
+      as: :json
54
+    end
55
+    assert_response :forbidden
56
+  end
57
+  
58
+
59
+end

+ 1
- 1
test/controllers/api/v1/teams_controller_test.rb 파일 보기

@@ -44,7 +44,7 @@ class Api::V1::TeamsControllerTest < ActionDispatch::IntegrationTest
44 44
     assert_response :created
45 45
   end
46 46
 
47
-  test "should forbid create team when not logged in" do
47
+  test "should forbid create team" do
48 48
     assert_no_difference('Team.count') do
49 49
       post api_v1_teams_url,
50 50
       params: { team: { name: "Random name", description: "Random description" } },

Loading…
취소
저장