Class: Api::WorkgroupsController

Inherits:
ApplicationController show all
Includes:
ExternalLinks, Follow, Media, Members, Recommendations, Relations, Upload, Utils
Defined in:
app/controllers/api/workgroups_controller.rb

Instance Method Summary collapse

Methods included from Recommendations

#recommended, #similar

Methods included from ExternalLinks

#create_link, #destroy_link, #index_link, #update_link

Methods included from Utils

#get_id_from_short_title, #is_admin, #is_member, #is_reviewer, #nickname_exist, #short_title_exist

Methods included from Relations

#clap, #clappers, #follow, #review, #reviewed_object, #save, #saved_objects

Methods included from Upload

#remove_avatar, #remove_banner, #remove_document, #upload_avatar, #upload_banner, #upload_document

Methods included from Members

#get_current_role, #get_roles_list, #has_membership, #invite, #invite_as_admin, #join, #leave, #members_list, #remove_member, #update_member

Methods included from Media

#media_create, #media_destroy, #media_index, #media_show, #media_update, #media_upload

Methods included from Follow

#followers, #following

Methods included from Response

#json_response

Instance Method Details

#createObject

On creation the creator get all roles, skills and interests are added manually



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/controllers/api/workgroups_controller.rb', line 42

def create
  render(json: { data: 'ShortTitle is already taken' },
         status: :unprocessable_entity) && return unless Workgroup.where(short_title: params[:workgroup][:short_title]).empty?

  @workgroup = Workgroup.new(workgroup_params)
  @workgroup.creator_id = current_user.id
  @workgroup.status = 'active'
  if @workgroup.save
    current_user.add_edge(@workgroup, 'is_author_of')
    @workgroup.update_skills(params[:workgroup][:skills]) unless params[:workgroup][:skills].nil?
    @workgroup.update_ressources(params[:workgroup][:ressources]) unless params[:workgroup][:ressources].nil?
    @workgroup.update_interests(params[:workgroup][:interests]) unless params[:workgroup][:interests].nil?
    current_user.add_role :owner, @workgroup
    current_user.add_role :admin, @workgroup
    current_user.add_role :member, @workgroup
    current_user.add_edge(@workgroup, 'is_member_of')
    current_user.add_edge(@workgroup, 'is_admin_of')
    current_user.add_edge(@workgroup, 'is_owner_of')
    render json: { id: @workgroup.id, data: 'Success' }, status: :created
  end
end

#destroyObject



83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/api/workgroups_controller.rb', line 83

def destroy
  # NOTE: a workgroup with 1 user is considered a test workgroup, and can be destroyed
  if @workgroup.users.count == 1 && current_user.has_role?(:owner, @workgroup)
    @workgroup.destroy
    current_user.remove_edge(@workgroup, 'is_author_of')
    render json: { data: 'Workgroup destroyed' }, status: :ok
  else
    @workgroup.archived!
    render json: { data: 'Workgroup archived' }, status: :ok
  end
end

#indexObject



22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/api/workgroups_controller.rb', line 22

def index
  # take order param in the url, and order array depending on its value
  param = if params[:order] == 'desc'
            'id DESC'
          else
            'id ASC'
          end
  @pagy, @workgroups = pagy(Workgroup.where.not(status: 'archived').order(param).all)
  render json: @workgroups
end

#my_workgroupsObject



33
34
35
36
37
38
39
# File 'app/controllers/api/workgroups_controller.rb', line 33

def my_workgroups
  @workgroups = Workgroup.with_role(:owner, current_user)
  @workgroups += Workgroup.with_role(:admin, current_user)
  @workgroups += Workgroup.with_role(:member, current_user)
  @pagy, @workgroups = pagy_array(@workgroups.uniq)
  render json: @workgroups
end

#showObject



64
65
66
# File 'app/controllers/api/workgroups_controller.rb', line 64

def show
  render json: @workgroup, show_objects: true
end

#updateObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/api/workgroups_controller.rb', line 68

def update
  unless params[:workgroup][:creator_id].nil?
    render json: { data: 'You cannot change the creator_id' },
           status: :forbidden if (@workgroup.creator_id != params[:workgroup][:creator_id]) && (@workgroup.creator_id != current_user.id)
  end
  if @workgroup.update(workgroup_params)
    @workgroup.update_skills(params[:workgroup][:skills]) unless params[:workgroup][:skills].nil?
    @workgroup.update_ressources(params[:workgroup][:ressources]) unless params[:workgroup][:ressources].nil?
    @workgroup.update_interests(params[:workgroup][:interests]) unless params[:workgroup][:interests].nil?
    json_response(@workgroup)
  else
    render json: { data: 'Something went wrong !' }, status: :unprocessable_entity
  end
end