Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose groups and vpns related list APIs #216

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ gem 'slim-rails'
gem 'turbolinks'
gem 'uglifier'
gem 'whenever', require: false
gem 'kaminari'

group :development, :test do
gem 'capybara'
Expand Down
18 changes: 18 additions & 0 deletions app/controllers/api/v1/groups_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
class ::Api::V1::GroupsController < ::Api::V1::BaseController
def index
groups = Group.order(:id).page(params[:page]).per(params[:per_page])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's add the default page and per_page if it's not provided by user

render json: groups, status: :ok
end

def create
if current_user.admin?
@group = Group.new(group_params)
Expand Down Expand Up @@ -40,6 +45,19 @@ def add_user
head :no_content
end

def list_admins
group = Group.find(params[:id])
users = group.group_admins.joins(:user).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we also check if users in group_admins are not inactive. and if inactive users are there can we filter them out?

select('users.id, users.email, users.name, users.active, group_admins.created_at as join_date')
render json: users, status: :ok
end

def associated_vpns
group = Group.find(params[:id])
vpns = group.vpns
render json: vpns, status: :ok
end

private

def group_params
Expand Down
11 changes: 11 additions & 0 deletions app/controllers/api/v1/vpns_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
class ::Api::V1::VpnsController < ::Api::V1::BaseController
before_action :set_vpn, only: [:assign_group]

def index
vpns = Vpn.order(:id).page(params[:page]).per(params[:per_page])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, default page and per_page in case not present in request

render json: vpns, status: :ok
end

def associated_groups
vpn = Vpn.find(params[:id])
groups = vpn.groups
render json: groups, status: :ok
end

def create
if current_user.admin?
@vpn = Vpn.new(vpn_params)
Expand Down
9 changes: 6 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,12 @@
post 'endpoints' => 'endpoints#create', format: :json, :constraints => { format: 'json' }
post 'endpoints/:id/add_group' => 'endpoints#add_group', format: :json, constraints: { format: 'json' }
post 'groups/:id/users' => 'groups#add_user', format: :json, constraints: { format: 'json' }

resources :groups, only: [:create], format: :json
resources :vpns, only: [:create], format: :json do
get 'groups/:id/admins' => 'groups#list_admins', format: :json, constraints: { format: 'json' }
get 'groups/:id/vpns' => 'groups#associated_vpns', format: :json, constraints: { format: 'json' }
get 'vpns/:id/groups' => 'vpns#associated_groups', format: :json, constraints: { format: 'json' }

resources :groups, only: [:create, :index], format: :json
resources :vpns, only: [:create, :index], format: :json do
member do
post 'assign_group'
end
Expand Down