mirror of
https://github.com/ENSL/ensl.org.git
synced 2025-01-12 21:00:58 +00:00
Add team API
This commit is contained in:
parent
39ff0f0122
commit
6538446a55
5 changed files with 240 additions and 52 deletions
37
app/controllers/api/v1/teams_controller.rb
Executable file
37
app/controllers/api/v1/teams_controller.rb
Executable file
|
@ -0,0 +1,37 @@
|
||||||
|
module Api
|
||||||
|
module V1
|
||||||
|
class TeamsController < Api::V1::BaseController
|
||||||
|
def index
|
||||||
|
render json: Api::V1::TeamsCollection.as_json
|
||||||
|
end
|
||||||
|
|
||||||
|
def show
|
||||||
|
team = Team.find(params[:id])
|
||||||
|
|
||||||
|
render json: {
|
||||||
|
id: team.id,
|
||||||
|
name: team.name,
|
||||||
|
tag: team.tag,
|
||||||
|
logo: team.logo.url,
|
||||||
|
roster:
|
||||||
|
Teamer
|
||||||
|
.joins(:user)
|
||||||
|
.select("users.id, users.username, users.team_id, users.steamid, teamers.rank")
|
||||||
|
.where(teamers: { team_id: team.id })
|
||||||
|
.where("teamers.rank >= ?", Teamer::RANK_MEMBER)
|
||||||
|
.map do |user|
|
||||||
|
{
|
||||||
|
userid: user.id,
|
||||||
|
name: user.username,
|
||||||
|
steamid: user.steamid,
|
||||||
|
rank: user.rank,
|
||||||
|
primary: user.team_id == team.id
|
||||||
|
}
|
||||||
|
end
|
||||||
|
}
|
||||||
|
rescue ActiveRecord::RecordNotFound
|
||||||
|
raise ActionController::RoutingError.new("Team Not Found")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
45
app/services/api/v1/teams_collection.rb
Executable file
45
app/services/api/v1/teams_collection.rb
Executable file
|
@ -0,0 +1,45 @@
|
||||||
|
module Api
|
||||||
|
module V1
|
||||||
|
class TeamsCollection < Api::V1::Collection
|
||||||
|
def self.as_json
|
||||||
|
new.data.to_json
|
||||||
|
end
|
||||||
|
|
||||||
|
def data
|
||||||
|
{ teams: map_query }
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def teams_table
|
||||||
|
Team.arel_table
|
||||||
|
end
|
||||||
|
|
||||||
|
def columns
|
||||||
|
[
|
||||||
|
teams_table[:id],
|
||||||
|
teams_table[:name],
|
||||||
|
teams_table[:tag],
|
||||||
|
teams_table[:logo],
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
def arel_query
|
||||||
|
teams_table
|
||||||
|
.project(columns)
|
||||||
|
.order(teams_table[:id])
|
||||||
|
end
|
||||||
|
|
||||||
|
def map_query
|
||||||
|
execute_query.map do |row|
|
||||||
|
{
|
||||||
|
id: row[0],
|
||||||
|
name: row[1],
|
||||||
|
tag: row[2],
|
||||||
|
logo: row[3]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
105
config/routes.rb
Normal file → Executable file
105
config/routes.rb
Normal file → Executable file
|
@ -1,4 +1,4 @@
|
||||||
Ensl::Application.routes.draw do
|
Ensl::Application.routes.draw do
|
||||||
%w(403 404 422 500).each do |code|
|
%w(403 404 422 500).each do |code|
|
||||||
get code, to: 'errors#show', code: code
|
get code, to: 'errors#show', code: code
|
||||||
end
|
end
|
||||||
|
@ -8,6 +8,7 @@ Ensl::Application.routes.draw do
|
||||||
resources :users, only: [:show, :index]
|
resources :users, only: [:show, :index]
|
||||||
resources :servers, only: [:index]
|
resources :servers, only: [:index]
|
||||||
resources :maps, only: [:index]
|
resources :maps, only: [:index]
|
||||||
|
resources :teams, only: [:show, :index]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -17,12 +18,12 @@ Ensl::Application.routes.draw do
|
||||||
resources :versions
|
resources :versions
|
||||||
end
|
end
|
||||||
|
|
||||||
match 'contests/del_map'
|
match "contests/del_map"
|
||||||
match 'contests/scores'
|
match "contests/scores"
|
||||||
match 'contests/historical', to: "contests#historical"
|
match "contests/historical", to: "contests#historical"
|
||||||
|
|
||||||
resources :contests do
|
resources :contests do
|
||||||
get 'current', on: :collection
|
get "current", on: :collection
|
||||||
end
|
end
|
||||||
|
|
||||||
resources :log_events
|
resources :log_events
|
||||||
|
@ -30,7 +31,7 @@ Ensl::Application.routes.draw do
|
||||||
resources :options
|
resources :options
|
||||||
resources :polls
|
resources :polls
|
||||||
|
|
||||||
match 'comments/quote'
|
match "comments/quote"
|
||||||
|
|
||||||
resources :comments
|
resources :comments
|
||||||
resources :shoutmsgs
|
resources :shoutmsgs
|
||||||
|
@ -43,8 +44,8 @@ Ensl::Application.routes.draw do
|
||||||
resources :forumers
|
resources :forumers
|
||||||
resources :topics
|
resources :topics
|
||||||
|
|
||||||
match 'forums/up'
|
match "forums/up"
|
||||||
match 'forums/down'
|
match "forums/down"
|
||||||
|
|
||||||
resources :forums
|
resources :forums
|
||||||
resources :users
|
resources :users
|
||||||
|
@ -55,7 +56,7 @@ Ensl::Application.routes.draw do
|
||||||
resources :servers
|
resources :servers
|
||||||
resources :predictions
|
resources :predictions
|
||||||
resources :rounds
|
resources :rounds
|
||||||
resources :matches do |m|
|
resources :matches do |_|
|
||||||
get :admin, to: "matches#admin", on: :collection
|
get :admin, to: "matches#admin", on: :collection
|
||||||
get :ref, to: "matches#ref"
|
get :ref, to: "matches#ref"
|
||||||
end
|
end
|
||||||
|
@ -73,66 +74,66 @@ Ensl::Application.routes.draw do
|
||||||
resources :bans
|
resources :bans
|
||||||
resources :tweets
|
resources :tweets
|
||||||
resources :issues
|
resources :issues
|
||||||
|
|
||||||
match 'posts/quote'
|
match "posts/quote"
|
||||||
|
|
||||||
resources :posts
|
resources :posts
|
||||||
resources :brackets
|
resources :brackets
|
||||||
|
|
||||||
match 'about/action'
|
match "about/action"
|
||||||
match 'about/staff'
|
match "about/staff"
|
||||||
match 'about/statistics'
|
match "about/statistics"
|
||||||
|
|
||||||
match 'refresh', to: "application#refresh"
|
match "refresh", to: "application#refresh"
|
||||||
match 'search', to: "application#search"
|
match "search", to: "application#search"
|
||||||
|
|
||||||
match 'news', to: "articles#news_index"
|
match "news", to: "articles#news_index"
|
||||||
match 'news/archive', to: "articles#news_archive"
|
match "news/archive", to: "articles#news_archive"
|
||||||
match 'news/admin', to: "articles#admin"
|
match "news/admin", to: "articles#admin"
|
||||||
match 'articles/cleanup'
|
match "articles/cleanup"
|
||||||
|
|
||||||
match 'data_files/admin'
|
match "data_files/admin"
|
||||||
match 'data_files/addFile'
|
match "data_files/addFile"
|
||||||
match 'data_files/delFile'
|
match "data_files/delFile"
|
||||||
match 'data_files/trash'
|
match "data_files/trash"
|
||||||
|
|
||||||
match 'contesters/recalc'
|
match "contesters/recalc"
|
||||||
|
|
||||||
match 'directories', to: "directories#show", id: 1
|
match "directories", to: "directories#show", id: 1
|
||||||
|
|
||||||
match 'gathers/refresh'
|
match "gathers/refresh"
|
||||||
match 'gathers/latest/:game', to: "gathers#latest", via: :get
|
match "gathers/latest/:game", to: "gathers#latest", via: :get
|
||||||
match 'gather', to: "gathers#latest", game: "ns2", via: :get
|
match "gather", to: "gathers#latest", game: "ns2", via: :get
|
||||||
|
|
||||||
match 'gatherers/:id/status', to: "gatherers#status", via: :post
|
match "gatherers/:id/status", to: "gatherers#status", via: :post
|
||||||
|
|
||||||
match 'groups/addUser'
|
match "groups/addUser"
|
||||||
match 'groups/delUser'
|
match "groups/delUser"
|
||||||
|
|
||||||
match 'movies/download'
|
match "movies/download"
|
||||||
match 'movies/preview'
|
match "movies/preview"
|
||||||
match 'movies/snapshot'
|
match "movies/snapshot"
|
||||||
|
|
||||||
match 'plugin/user'
|
match "plugin/user"
|
||||||
|
|
||||||
match 'users/forgot'
|
match "users/forgot"
|
||||||
match 'users/recover'
|
match "users/recover"
|
||||||
match 'users/agenda'
|
match "users/agenda"
|
||||||
match 'users/logout'
|
match "users/logout"
|
||||||
match 'users/login'
|
match "users/login"
|
||||||
|
|
||||||
match 'users/agenda'
|
match "users/agenda"
|
||||||
match 'users/login'
|
match "users/login"
|
||||||
match 'users/logout'
|
match "users/logout"
|
||||||
match 'users/popup'
|
match "users/popup"
|
||||||
match 'users/forgot', to: "users#forgot"
|
match "users/forgot", to: "users#forgot"
|
||||||
|
|
||||||
match 'votes/create'
|
match "votes/create"
|
||||||
|
|
||||||
match ':controller/:action', requirements: { action: /A-Za-z/ }
|
match ":controller/:action", requirements: { action: /A-Za-z/ }
|
||||||
match ':controller/:action/:id'
|
match ":controller/:action/:id"
|
||||||
match ':controller/:action/:id.:format'
|
match ":controller/:action/:id.:format"
|
||||||
match ':controller/:action/:id/:id2'
|
match ":controller/:action/:id/:id2"
|
||||||
|
|
||||||
match 'teamers/replace', to: 'teamers#replace', as: 'teamers_replace'
|
match "teamers/replace", to: 'teamers#replace', as: "teamers_replace"
|
||||||
end
|
end
|
||||||
|
|
78
spec/controllers/api/v1/teams_controller_spec.rb
Executable file
78
spec/controllers/api/v1/teams_controller_spec.rb
Executable file
|
@ -0,0 +1,78 @@
|
||||||
|
require "spec_helper"
|
||||||
|
|
||||||
|
describe Api::V1::TeamsController do
|
||||||
|
before do
|
||||||
|
request.accept = "application/json"
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#show" do
|
||||||
|
before(:each) do
|
||||||
|
@user = create :user
|
||||||
|
@team = create(:team, founder: @user)
|
||||||
|
end
|
||||||
|
|
||||||
|
def team_expectation(json, team)
|
||||||
|
expect(json["id"]).to eq(team.id)
|
||||||
|
expect(json["name"]).to eq(team.name)
|
||||||
|
expect(json["tag"]).to eq(team.tag)
|
||||||
|
expect(json["logo"]).to eq(team.logo.url)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns team data with empty roster" do
|
||||||
|
teamers = @user.teamers.of_team(@team).active
|
||||||
|
expect(teamers.length).to eq(1)
|
||||||
|
teamers[0].destroy
|
||||||
|
|
||||||
|
get :show, id: @team.id
|
||||||
|
|
||||||
|
expect(response).to be_success
|
||||||
|
team_expectation(json, @team)
|
||||||
|
expect(json["roster"]).to be_empty
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns team data with a roster" do
|
||||||
|
get :show, id: @team.id
|
||||||
|
|
||||||
|
expect(response).to be_success
|
||||||
|
team_expectation(json, @team)
|
||||||
|
expect(json["roster"].count).to eq(1)
|
||||||
|
|
||||||
|
roster_user = json["roster"][0]
|
||||||
|
expect(roster_user["userid"]).to eq(@user.id)
|
||||||
|
expect(roster_user["name"]).to eq(@user.username)
|
||||||
|
expect(roster_user["steamid"]).to eq(@user.steamid)
|
||||||
|
expect(roster_user["rank"]).to eq(Teamer::RANK_LEADER)
|
||||||
|
expect(roster_user["primary"]).to eq(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns 404 if team does not exist" do
|
||||||
|
expect { get :show, id: -1 }
|
||||||
|
.to raise_error(ActionController::RoutingError)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#index" do
|
||||||
|
before do
|
||||||
|
5.times { create(:user_with_team) }
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns all teams" do
|
||||||
|
teams = Team.all
|
||||||
|
|
||||||
|
get :index
|
||||||
|
|
||||||
|
expect(response).to be_success
|
||||||
|
expect(json["teams"].size).to eq(teams.size)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns the excpected JSON keys" do
|
||||||
|
get :index
|
||||||
|
team_json = json["teams"].first
|
||||||
|
|
||||||
|
expect(team_json).to have_key("id")
|
||||||
|
expect(team_json).to have_key("name")
|
||||||
|
expect(team_json).to have_key("tag")
|
||||||
|
expect(team_json).to have_key("logo")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
27
spec/services/api/v1/teams_collection_spec.rb
Executable file
27
spec/services/api/v1/teams_collection_spec.rb
Executable file
|
@ -0,0 +1,27 @@
|
||||||
|
require "spec_helper"
|
||||||
|
|
||||||
|
describe Api::V1::TeamsCollection do
|
||||||
|
let(:collection) { Api::V1::TeamsCollection.new }
|
||||||
|
|
||||||
|
describe "#execute_query" do
|
||||||
|
describe "when there are no teams" do
|
||||||
|
it "returns 0 results" do
|
||||||
|
expect(collection.execute_query.size).to eq(0)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "when there are some teams" do
|
||||||
|
before do
|
||||||
|
3.times { create(:user_with_team) }
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns 3 results" do
|
||||||
|
expect(collection.execute_query.size).to eq(3)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns 4 columns" do
|
||||||
|
expect(collection.execute_query.first.size).to eq(4)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue