Merge pull request #18 from turniere/ticket/TURNIERE-112

Change API schema
This commit is contained in:
Daniel Schädler 2018-12-11 17:42:49 +01:00 committed by GitHub
commit 03244c1820
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 73 additions and 78 deletions

View File

@ -24,8 +24,4 @@ class ApplicationController < ActionController::API
]
}, status: :forbidden
end
def deserialize_params(opts)
ActiveModelSerializers::Deserialization.jsonapi_parse(params, opts)
end
end

View File

@ -28,6 +28,6 @@ class MatchScoresController < ApplicationController
# Only allow a trusted parameter "white list" through.
def match_score_params
deserialize_params only: %i[points]
params.require(:match_score).permit(:points)
end
end

View File

@ -3,6 +3,6 @@
class MatchesController < ApplicationController
# GET /matches/1
def show
render json: Match.find(params[:id]), include: ['match_scores.points', 'match_scores.team'], status: status
render json: Match.find(params[:id]), include: ['match_scores.points', 'match_scores.team']
end
end

View File

@ -26,6 +26,6 @@ class TeamsController < ApplicationController
end
def team_params
deserialize_params only: %i[name]
params.require(:team).permit(:name)
end
end

View File

@ -13,7 +13,7 @@ class TournamentsController < ApplicationController
# GET /tournaments/1
def show
render json: @tournament
render json: @tournament, include: '**'
end
# POST /tournaments
@ -48,6 +48,6 @@ class TournamentsController < ApplicationController
end
def tournament_params
deserialize_params only: %i[name description public teams]
params.require(:tournament).permit(:name, :description, :public, :teams)
end
end

View File

@ -3,4 +3,7 @@
class GroupScore < ApplicationRecord
belongs_to :team
belongs_to :group
# :)
alias_attribute :received_points, :recieved_points
end

View File

@ -1,4 +1,5 @@
# frozen_string_literal: true
class ApplicationSerializer < ActiveModel::Serializer
attributes :id
end

View File

@ -0,0 +1,7 @@
# frozen_string_literal: true
class GroupScoreSerializer < ApplicationSerializer
attributes :group_points, :received_points, :scored_points
belongs_to :team
end

View File

@ -0,0 +1,8 @@
# frozen_string_literal: true
class GroupSerializer < ApplicationSerializer
attributes :number
has_many :matches
has_many :group_scores
end

View File

@ -3,6 +3,5 @@
class MatchScoreSerializer < ApplicationSerializer
attributes :points
has_one :team
has_one :match
belongs_to :team
end

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true
class MatchSerializer < ApplicationSerializer
attributes :state
attributes :state, :position
has_many :match_scores
end

View File

@ -1,5 +1,5 @@
# frozen_string_literal: true
class SimpleTournamentSerializer < ApplicationSerializer
attributes :name, :code, :description, :public
attributes :name, :code, :public
end

View File

@ -0,0 +1,8 @@
# frozen_string_literal: true
class StageSerializer < ApplicationSerializer
attributes :level
has_many :matches
has_many :groups
end

View File

@ -1,6 +1,11 @@
# frozen_string_literal: true
class TournamentSerializer < SimpleTournamentSerializer
has_many :teams
attributes :description
has_many :stages
has_many :teams
attribute :owner_username do
object.owner.username
end
end

View File

@ -1,3 +1,3 @@
# frozen_string_literal: true
ActiveModelSerializers.config.adapter = :json_api
ActiveModelSerializers.config.adapter = :attributes

View File

@ -18,21 +18,14 @@ RSpec.describe MatchScoresController, type: :controller do
get :show, params: { id: @match_score.to_param }
body = deserialize_response response
expect(body[:points]).to eq(@match_score.points)
expect(body[:team_id]).to eq(@match_score.team.id.to_s)
expect(body[:match_id]).to eq(@match_score.match.id.to_s)
expect(body[:team][:id]).to eq(@match_score.team.id)
end
end
describe 'PUT #update' do
let(:valid_update) do
{
data: {
id: @match_score.id,
type: 'match_scores',
attributes: {
points: 42
}
}
points: 42
}
end
@ -43,16 +36,16 @@ RSpec.describe MatchScoresController, type: :controller do
end
it 'updates the requested score' do
put :update, params: { id: @match_score.to_param }.merge(valid_update)
put :update, params: { id: @match_score.to_param, match_score: valid_update }
@match_score.reload
expect(@match_score.points).to eq(valid_update[:data][:attributes][:points])
expect(@match_score.points).to eq(valid_update[:points])
end
it 'renders a response with the updated team' do
put :update, params: { id: @match_score.to_param }.merge(valid_update)
put :update, params: { id: @match_score.to_param, match_score: valid_update }
expect(response).to be_successful
body = deserialize_response response
expect(body[:points]).to eq(valid_update[:data][:attributes][:points])
expect(body[:points]).to eq(valid_update[:points])
end
end
@ -62,7 +55,7 @@ RSpec.describe MatchScoresController, type: :controller do
end
it 'renders a forbidden error response' do
put :update, params: { id: @match_score.to_param }.merge(valid_update)
put :update, params: { id: @match_score.to_param, match_score: valid_update }
expect(response).to have_http_status(:forbidden)
end
end

View File

@ -17,9 +17,9 @@ RSpec.describe MatchesController, type: :controller do
it 'should return the correct state' do
get :show, params: { id: @match.to_param }
body = ActiveModelSerializers::Deserialization.jsonapi_parse(JSON.parse(response.body))
body = deserialize_response response
expect(body[:state]).to eq(@match.state)
expect(body[:match_score_ids]).to eq(@match.match_scores.map { |match_score| match_score.id.to_s })
expect(body[:match_scores].map { |ms| ms[:id] }).to eq(@match.match_scores.map(&:id))
end
end
end

View File

@ -24,13 +24,7 @@ RSpec.describe TeamsController, type: :controller do
describe 'PUT #update' do
let(:valid_update) do
{
data: {
id: @team.id,
type: 'teams',
attributes: {
name: Faker::Dog.name
}
}
name: Faker::Dog.name
}
end
@ -40,17 +34,17 @@ RSpec.describe TeamsController, type: :controller do
end
it 'updates the requested team' do
put :update, params: { id: @team.to_param }.merge(valid_update)
put :update, params: { id: @team.to_param, team: valid_update }
@team.reload
expect(response).to be_successful
expect(@team.name).to eq(valid_update[:data][:attributes][:name])
expect(@team.name).to eq(valid_update[:name])
end
it 'renders a response with the updated team' do
put :update, params: { id: @team.to_param }.merge(valid_update)
put :update, params: { id: @team.to_param, team: valid_update }
expect(response).to be_successful
body = deserialize_response response
expect(body[:name]).to eq(valid_update[:data][:attributes][:name])
expect(body[:name]).to eq(valid_update[:name])
end
end
@ -60,7 +54,7 @@ RSpec.describe TeamsController, type: :controller do
end
it 'renders a forbidden error response' do
put :update, params: { id: @team.to_param }.merge(valid_update)
put :update, params: { id: @team.to_param, team: valid_update }
expect(response).to have_http_status(:forbidden)
end
end

View File

@ -4,7 +4,7 @@ require 'rails_helper'
RSpec.describe TournamentsController, type: :controller do
def tournament_ids(response)
deserialize_list(response).map { |t| t[:id].to_i }
deserialize_response(response).map { |t| t[:id].to_i }
end
before do
@tournament = create(:tournament)
@ -22,14 +22,14 @@ RSpec.describe TournamentsController, type: :controller do
it 'returns all public tournaments' do
get :index
tournaments = deserialize_list response
tournaments = deserialize_response response
public_tournaments = tournaments.select { |t| t[:public] }
expect(public_tournaments.size).to eq((Tournament.where public: true).size)
end
it 'returns no private tournaments for unauthenticated users' do
get :index
tournaments = deserialize_list response
tournaments = deserialize_response response
private_tournaments = tournaments.reject { |t| t[:public] }
expect(private_tournaments.size).to eq(0)
end
@ -59,21 +59,14 @@ RSpec.describe TournamentsController, type: :controller do
end
end
describe 'POST #create' do
describe 'POST #create', skip: true do
let(:create_data) do
{
data: {
type: 'tournaments',
attributes: {
name: Faker::Dog.name,
description: Faker::Lorem.sentence,
public: false
},
relationships: {
teams: {
data: @teams.map { |team| { type: 'teams', id: team.id } }
}
}
name: Faker::Dog.name,
description: Faker::Lorem.sentence,
public: false,
teams: {
data: @teams.map { |team| { type: 'teams', id: team.id } }
}
}
end
@ -116,20 +109,14 @@ RSpec.describe TournamentsController, type: :controller do
describe 'PUT #update' do
let(:valid_update) do
{
data: {
type: 'tournaments',
id: @tournament.id,
attributes: {
name: Faker::Dog.name
}
}
name: Faker::Dog.name
}
end
context 'with valid params' do
context 'without authentication headers' do
it 'renders a unauthorized error response' do
put :update, params: { id: @tournament.to_param }.merge(valid_update)
put :update, params: { id: @tournament.to_param, tournament: valid_update }
expect(response).to have_http_status(:unauthorized)
end
end
@ -140,13 +127,13 @@ RSpec.describe TournamentsController, type: :controller do
end
it 'updates the requested tournament' do
put :update, params: { id: @tournament.to_param }.merge(valid_update)
put :update, params: { id: @tournament.to_param, tournament: valid_update }
@tournament.reload
expect(@tournament.name).to eq(valid_update[:data][:attributes][:name])
expect(@tournament.name).to eq(valid_update[:name])
end
it 'renders a JSON response with the tournament' do
put :update, params: { id: @tournament.to_param }.merge(valid_update)
put :update, params: { id: @tournament.to_param, tournament: valid_update }
expect(response).to have_http_status(:ok)
expect(response.content_type).to eq('application/json')
end
@ -158,7 +145,7 @@ RSpec.describe TournamentsController, type: :controller do
end
it 'renders a forbidden error response' do
put :update, params: { id: @tournament.to_param }.merge(valid_update)
put :update, params: { id: @tournament.to_param, tournament: valid_update }
expect(response).to have_http_status(:forbidden)
end
end

View File

@ -2,12 +2,6 @@
module DeserializeHelpers
def deserialize_response(response)
ActiveModelSerializers::Deserialization.jsonapi_parse(JSON.parse(response.body))
end
def deserialize_list(response)
JSON.parse(response.body, symbolize_names: true)[:data].map do |raw_obj|
raw_obj[:attributes].merge raw_obj.except(:attributes)
end
JSON.parse(response.body, symbolize_names: true)
end
end