Skip to content

Commit

Permalink
Players endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Dahlgren committed Dec 25, 2019
1 parent a8b8b0a commit e691955
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class EventsController < ApplicationController
class Missions::EventsController < ApplicationController
before_action :restrict_access, only: [:create, :update, :destroy]
before_action :set_mission
before_action :set_event, only: [:show, :update, :destroy]
Expand Down
17 changes: 17 additions & 0 deletions app/controllers/missions/players_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Missions::PlayersController < ApplicationController
before_action :set_mission

# GET /missions/:mission_id/players
def index
@players = @mission.events.select("data #>> '{player, name}' as name, data #>> '{player, uid}' as uid")
.where("data #>> '{player, uid}' != 'null'")
.group("uid, name")

render json: @players
end

private
def set_mission
@mission = Mission.find(params[:mission_id])
end
end
10 changes: 2 additions & 8 deletions app/controllers/players_controller.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
class PlayersController < ApplicationController
before_action :set_mission

# GET /missions/:mission_id/players
# GET /players
def index
@players = @mission.events.select("data #>> '{player, name}' as name, data #>> '{player, uid}' as uid")
@players = Event.select("data #>> '{player, name}' as name, data #>> '{player, uid}' as uid")
.where("data #>> '{player, uid}' != 'null'")
.group("uid, name")

render json: @players
end

private
def set_mission
@mission = Mission.find(params[:mission_id])
end
end
8 changes: 6 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

resources :missions do
resources :events
resources :players, only: [:index]
scope module: :missions do
resources :events
resources :players, only: [:index]
end
end

resources :players, only: [:index]
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'test_helper'

class EventsControllerTest < ActionDispatch::IntegrationTest
class Missions::EventsControllerTest < ActionDispatch::IntegrationTest
setup do
@event = events(:player_connected)
@mission = missions(:one)
Expand Down
12 changes: 12 additions & 0 deletions test/controllers/missions/players_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'test_helper'

class Missions::PlayersControllerTest < ActionDispatch::IntegrationTest
setup do
@mission = missions(:one)
end

test "should get index" do
get mission_players_url(@mission)
assert_response :success
end
end
8 changes: 8 additions & 0 deletions test/controllers/players_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'test_helper'

class PlayersControllerTest < ActionDispatch::IntegrationTest
test "should get index" do
get players_url
assert_response :success
end
end

0 comments on commit e691955

Please sign in to comment.