Browse Source

JoinedTeamUser devient Membership

Lou 3 years ago
parent
commit
628e045873

+ 0
- 35
app/controllers/api/v1/joined_team_users_controller.rb View File

@@ -1,35 +0,0 @@
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

+ 35
- 0
app/controllers/api/v1/memberships_controller.rb View File

@@ -0,0 +1,35 @@
1
+class Api::V1::MembershipsController < ApplicationController
2
+  before_action :set_memberships, only: %i[show update destroy]
3
+  before_action :check_login
4
+
5
+  def index
6
+    render json: MembershipSerializer.new(Membership.all).serializable_hash.to_json
7
+  end
8
+
9
+  def show
10
+    render json: MembershipSerializer.new(@membership).serializable_hash.to_json
11
+  end
12
+
13
+  def create
14
+    userId = User.find_by(username: params[:membership][:username]).id
15
+    teamId = Team.find_by(name: params[:membership][:team_name]).id
16
+    jtu = Membership.new(team_id: teamId, user_id: userId, can_edit: false)
17
+    
18
+    if jtu.save
19
+      render json: MembershipSerializer.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 memberships_params
28
+    params.require(:membership).permit(:team_id, :user_id, :can_edit, :created_at, :updated_at)
29
+  end
30
+  
31
+  def set_memberships
32
+    @membership = Membership.find(params[:id])
33
+  end
34
+
35
+end

+ 2
- 2
app/controllers/api/v1/teams_controller.rb View File

@@ -16,7 +16,7 @@ class Api::V1::TeamsController < ApplicationController
16 16
 
17 17
     if team.save 
18 18
       if current_user&.is_admin === false
19
-        JoinedTeamUser.create!(
19
+        Membership.create!(
20 20
           user_id: current_user.id,
21 21
           team_id: team.id,
22 22
           can_edit: true
@@ -54,6 +54,6 @@ class Api::V1::TeamsController < ApplicationController
54 54
   end
55 55
 
56 56
   def can_edit?
57
-    head :forbidden unless JoinedTeamUser.where(:team_id => params[:id]).where(:user_id => current_user.id)[0].can_edit
57
+    head :forbidden unless Membership.where(:team_id => params[:id]).where(:user_id => current_user.id)[0].can_edit
58 58
   end
59 59
 end

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

@@ -1,8 +0,0 @@
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
-
6
-  belongs_to :user
7
-  belongs_to :team
8
-end

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

@@ -0,0 +1,8 @@
1
+class Membership < ApplicationRecord
2
+  validates :member_id, presence: :true
3
+  validates :team_id, presence: :true
4
+  validates_uniqueness_of :member_id, scope: :team_id, message: "user already part of this team"
5
+
6
+  belongs_to :member, class_name: 'User'
7
+  belongs_to :team
8
+end

+ 2
- 2
app/models/team.rb View File

@@ -1,6 +1,6 @@
1 1
 class Team < ApplicationRecord
2 2
     validates :name, uniqueness: true, presence: true
3 3
 
4
-    has_many :joined_team_users
5
-    has_many :users, through: :joined_team_users
4
+    has_many :memberships
5
+    has_many :users, through: :memberships
6 6
 end

+ 2
- 2
app/models/user.rb View File

@@ -11,8 +11,8 @@ class User < ApplicationRecord
11 11
 
12 12
   has_many :tasks
13 13
   
14
-  has_many :joined_team_users
15
-  has_many :teams, through: :joined_team_users
14
+  has_many :memberships, foreign_key: :member_id
15
+  has_many :teams, through: :memberships
16 16
   
17 17
   has_secure_password
18 18
 end

app/serializers/joined_team_user_serializer.rb → app/serializers/membership_serializer.rb View File

@@ -1,4 +1,4 @@
1
-class JoinedTeamUserSerializer
1
+class MembershipSerializer
2 2
   include JSONAPI::Serializer
3 3
   attributes :team_id, :user_id, :can_edit
4 4
   

+ 1
- 1
config/routes.rb View File

@@ -4,7 +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
+      resources :memberships
8 8
       resources :activities do
9 9
         resources :tasks
10 10
       end

+ 0
- 11
db/migrate/20210803152520_create_joined_team_users.rb View File

@@ -1,11 +0,0 @@
1
-class CreateJoinedTeamUsers < ActiveRecord::Migration[6.1]
2
-  def change
3
-    create_table :joined_team_users do |t|
4
-      t.references :user
5
-      t.references :team
6
-      t.boolean :can_edit, default: false 
7
-
8
-      t.timestamps
9
-    end
10
-  end
11
-end

+ 11
- 0
db/migrate/20210803152520_create_memberships.rb View File

@@ -0,0 +1,11 @@
1
+class CreateMemberships < ActiveRecord::Migration[6.1]
2
+  def change
3
+    create_table :memberships do |t|
4
+      t.references :member
5
+      t.references :team
6
+      t.boolean :can_edit, default: false 
7
+
8
+      t.timestamps
9
+    end
10
+  end
11
+end

+ 10
- 10
db/schema.rb View File

@@ -26,16 +26,6 @@ ActiveRecord::Schema.define(version: 2021_08_03_152520) do
26 26
     t.index ["name"], name: "index_activities_on_name"
27 27
   end
28 28
 
29
-  create_table "joined_team_users", force: :cascade do |t|
30
-    t.bigint "user_id"
31
-    t.bigint "team_id"
32
-    t.boolean "can_edit", default: false
33
-    t.datetime "created_at", precision: 6, null: false
34
-    t.datetime "updated_at", precision: 6, null: false
35
-    t.index ["team_id"], name: "index_joined_team_users_on_team_id"
36
-    t.index ["user_id"], name: "index_joined_team_users_on_user_id"
37
-  end
38
-
39 29
   create_table "joined_user_activities", force: :cascade do |t|
40 30
     t.bigint "user_id"
41 31
     t.bigint "activity_id"
@@ -45,6 +35,16 @@ ActiveRecord::Schema.define(version: 2021_08_03_152520) do
45 35
     t.index ["user_id"], name: "index_joined_user_activities_on_user_id"
46 36
   end
47 37
 
38
+  create_table "memberships", force: :cascade do |t|
39
+    t.bigint "member_id"
40
+    t.bigint "team_id"
41
+    t.boolean "can_edit", default: false
42
+    t.datetime "created_at", precision: 6, null: false
43
+    t.datetime "updated_at", precision: 6, null: false
44
+    t.index ["member_id"], name: "index_memberships_on_member_id"
45
+    t.index ["team_id"], name: "index_memberships_on_team_id"
46
+  end
47
+
48 48
   create_table "tasks", force: :cascade do |t|
49 49
     t.string "name", null: false
50 50
     t.text "description"

+ 9
- 9
db/seeds.rb View File

@@ -8,8 +8,8 @@ Task.delete_all
8 8
 Task.reset_pk_sequence
9 9
 Team.delete_all
10 10
 Team.reset_pk_sequence
11
-JoinedTeamUser.delete_all
12
-JoinedTeamUser.reset_pk_sequence
11
+Membership.delete_all
12
+Membership.reset_pk_sequence
13 13
 
14 14
 3.times do |i|
15 15
   team = Team.create(name: Faker::Company.name, description: Faker::Company.catch_phrase)
@@ -18,11 +18,11 @@ end
18 18
 
19 19
 10.times do |i|
20 20
   name = Faker::Name.first_name.downcase
21
-  user = User.create! username: "#{name}", email: "#{name}@email.com", password: "azerty"
21
+  user = User.create(username: "#{name}", email: "#{name}@email.com", password: "azerty")
22 22
   puts "Created USER ##{i+1} - #{user.username}"
23 23
   
24 24
   2.times do
25
-    activity = Activity.create!(
25
+    activity = Activity.create(
26 26
       name: Faker::Science.element,
27 27
       client: Faker::Company.name,
28 28
       description: Faker::Company.catch_phrase,
@@ -30,22 +30,22 @@ end
30 30
     )
31 31
     puts "Created ACTIVITY \"#{activity.name}\" for #{activity.client}"
32 32
 
33
-    joined_user_activity = JoinedUserActivity.create!(
33
+    joined_user_activity = JoinedUserActivity.create(
34 34
       user_id: user.id,
35 35
       activity_id: activity.id
36 36
     )
37 37
     puts "Created ASSOCIATION ##{joined_user_activity.id}"
38 38
   end
39 39
   
40
-  joined_team_users = JoinedTeamUser.create!(
41
-    user_id: user.id,
40
+  memberships = Membership.create(
41
+    member_id: user.id,
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 ##{memberships.user_id} joined TEAM ##{memberships.team_id}"
46 46
 end
47 47
 
48
-User.create! username: "admin", email: "admin@email.com", password: "azerty", is_admin: true
48
+User.create(username: "admin", email: "admin@email.com", password: "azerty", is_admin: true)
49 49
 puts "Created Admin"
50 50
 
51 51
 30.times do |i|

+ 0
- 59
test/controllers/api/v1/joined_team_users_controller_test.rb View File

@@ -1,59 +0,0 @@
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

+ 59
- 0
test/controllers/api/v1/memberships_controller_test.rb View File

@@ -0,0 +1,59 @@
1
+require "test_helper"
2
+
3
+class Api::V1::MembershipsControllerTest < ActionDispatch::IntegrationTest
4
+  setup do
5
+    @membership = memberships(:one)
6
+    @user = users(:one)
7
+    @team = teams(:one)
8
+  end
9
+
10
+  # INDEX
11
+  test "should access Membership index" do
12
+    get api_v1_memberships_url,
13
+    headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, 
14
+    as: :json
15
+    assert_response :success
16
+  end
17
+
18
+  test "should forbid Membership index" do
19
+    get api_v1_memberships_url,
20
+    as: :json
21
+    assert_response :forbidden
22
+  end
23
+
24
+  # SHOW
25
+  test "should show Membership" do
26
+    get api_v1_memberships_url(@membership),
27
+    headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, 
28
+    as: :json
29
+    assert_response :success
30
+  end
31
+
32
+  test "should forbid show Membership" do
33
+    get api_v1_memberships_url(@membership),
34
+    as: :json
35
+    assert_response :forbidden
36
+  end
37
+
38
+  # CREATE
39
+  test "should create membership" do
40
+    assert_difference("Membership.count") do
41
+    post api_v1_memberships_url,
42
+    params: { membership: { 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 membership" do
50
+    assert_no_difference("Membership.count") do
51
+      post api_v1_memberships_url,
52
+      params: { membership: { 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

+ 2
- 2
test/controllers/api/v1/teams_controller_test.rb View File

@@ -89,7 +89,7 @@ class Api::V1::TeamsControllerTest < ActionDispatch::IntegrationTest
89 89
     assert_response :forbidden
90 90
   end
91 91
 
92
-  # Ajouter un test pour vérifier le statut de "current_user.can_edit" dans le modèle JoinedTeamUser
92
+  # Ajouter un test pour vérifier le statut de "current_user.can_edit" dans le modèle Membership
93 93
 
94 94
   # DESTROY
95 95
   test "should destroy team" do
@@ -107,6 +107,6 @@ class Api::V1::TeamsControllerTest < ActionDispatch::IntegrationTest
107 107
     end
108 108
     assert_response :forbidden
109 109
   end
110
-  # Ajouter un test pour vérifier le statut de "current_user.can_edit" dans le modèle JoinedTeamUser
110
+  # Ajouter un test pour vérifier le statut de "current_user.can_edit" dans le modèle Membership
111 111
 
112 112
 end

test/fixtures/joined_team_users.yml → test/fixtures/memberships.yml View File


+ 0
- 5
test/models/joined_team_user_test.rb View File

@@ -1,5 +0,0 @@
1
-require "test_helper"
2
-
3
-class JoinedTeamUserTest < ActiveSupport::TestCase
4
-
5
-end

+ 5
- 0
test/models/membership_test.rb View File

@@ -0,0 +1,5 @@
1
+require "test_helper"
2
+
3
+class MembershipTest < ActiveSupport::TestCase
4
+
5
+end

Loading…
Cancel
Save