diff --git a/Gemfile b/Gemfile index 904b1069..9bf521b8 100755 --- a/Gemfile +++ b/Gemfile @@ -31,6 +31,7 @@ gem 'slim-rails' gem 'turbolinks' gem 'uglifier' gem 'whenever', require: false +gem 'kaminari' group :development, :test do gem 'capybara' diff --git a/app/controllers/api/v1/groups_controller.rb b/app/controllers/api/v1/groups_controller.rb index 14bef8c1..d622130b 100755 --- a/app/controllers/api/v1/groups_controller.rb +++ b/app/controllers/api/v1/groups_controller.rb @@ -1,4 +1,9 @@ class ::Api::V1::GroupsController < ::Api::V1::BaseController + def index + groups = Group.order(:id).page(params[:page]).per(params[:per_page]) + render json: groups, status: :ok + end + def create if current_user.admin? @group = Group.new(group_params) @@ -40,6 +45,24 @@ def add_user head :no_content end + def list_admins + group = Group.find_by_id(params[:id]) + return head :not_found unless group.present? + + users = group.group_admins.joins(:user). + select('users.id, users.email, users.name, users.active, group_admins.created_at as join_date'). + where('users.active = ?', true) + render json: users, status: :ok + end + + def associated_vpns + group = Group.find_by_id(params[:id]) + return head :not_found unless group.present? + + vpns = group.vpns + render json: vpns, status: :ok + end + private def group_params diff --git a/app/controllers/api/v1/vpns_controller.rb b/app/controllers/api/v1/vpns_controller.rb index 3b0d3353..4160a152 100755 --- a/app/controllers/api/v1/vpns_controller.rb +++ b/app/controllers/api/v1/vpns_controller.rb @@ -1,6 +1,20 @@ 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]) + vpns = vpns.where("name LIKE ?", "%#{params[:q]}%") if params[:q].present? + render json: vpns, status: :ok + end + + def associated_groups + vpn = Vpn.find_by_id(params[:id]) + return head :not_found unless vpn.present? + + groups = vpn.groups + render json: groups, status: :ok + end + def create if current_user.admin? @vpn = Vpn.new(vpn_params) diff --git a/config/routes.rb b/config/routes.rb index c4401ac5..f32759b3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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