Score is now MatchScore & Score Attribute is Points Attribute
This commit is contained in:
parent
e763f14719
commit
dd2f66f49d
|
|
@ -0,0 +1,33 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class MatchScoresController < ApplicationController
|
||||
before_action :set_match_score, only: %i[show update]
|
||||
before_action :authenticate_user!, only: %i[update]
|
||||
before_action -> { require_owner! @match_score.owner }, only: %i[update]
|
||||
|
||||
# GET /scores/1
|
||||
def show
|
||||
render json: @match_score
|
||||
end
|
||||
|
||||
# PATCH/PUT /scores/1
|
||||
def update
|
||||
if @match_score.update(match_score_params)
|
||||
render json: @match_score
|
||||
else
|
||||
render json: @match_score.errors, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Use callbacks to share common setup or constraints between actions.
|
||||
def set_match_score
|
||||
@match_score = MatchScore.find(params[:id])
|
||||
end
|
||||
|
||||
# Only allow a trusted parameter "white list" through.
|
||||
def match_score_params
|
||||
deserialize_params only: %i[points]
|
||||
end
|
||||
end
|
||||
|
|
@ -1,33 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class ScoresController < ApplicationController
|
||||
before_action :set_score, only: %i[show update]
|
||||
before_action :authenticate_user!, only: %i[update]
|
||||
before_action -> { require_owner! @score.owner }, only: %i[update]
|
||||
|
||||
# GET /scores/1
|
||||
def show
|
||||
render json: @score
|
||||
end
|
||||
|
||||
# PATCH/PUT /scores/1
|
||||
def update
|
||||
if @score.update(score_params)
|
||||
render json: @score
|
||||
else
|
||||
render json: @score.errors, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Use callbacks to share common setup or constraints between actions.
|
||||
def set_score
|
||||
@score = Score.find(params[:id])
|
||||
end
|
||||
|
||||
# Only allow a trusted parameter "white list" through.
|
||||
def score_params
|
||||
deserialize_params only: %i[score]
|
||||
end
|
||||
end
|
||||
|
|
@ -6,5 +6,5 @@ Rails.application.routes.draw do
|
|||
resources :matches, only: %i[show]
|
||||
resources :teams, only: %i[show update]
|
||||
resources :tournaments
|
||||
resources :scores, only: %i[show update]
|
||||
resources :match_scores, only: %i[show update]
|
||||
end
|
||||
|
|
|
|||
|
|
@ -2,24 +2,24 @@
|
|||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe ScoresController, type: :controller do
|
||||
RSpec.describe MatchScoresController, type: :controller do
|
||||
before do
|
||||
@score = create(:score)
|
||||
@owner = @score.owner
|
||||
@match_score = create(:match_score)
|
||||
@owner = @match_score.owner
|
||||
end
|
||||
|
||||
describe 'GET #show' do
|
||||
it 'returns a success response' do
|
||||
get :show, params: { id: @score.to_param }
|
||||
get :show, params: { id: @match_score.to_param }
|
||||
expect(response).to be_successful
|
||||
end
|
||||
|
||||
it 'should return the correct score' do
|
||||
get :show, params: { id: @score.to_param }
|
||||
get :show, params: { id: @match_score.to_param }
|
||||
body = deserialize_response response
|
||||
expect(body[:score]).to eq(@score.score)
|
||||
expect(body[:team_id]).to eq(@score.team.id.to_s)
|
||||
expect(body[:match_id]).to eq(@score.match.id.to_s)
|
||||
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)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -27,10 +27,10 @@ RSpec.describe ScoresController, type: :controller do
|
|||
let(:valid_update) do
|
||||
{
|
||||
data: {
|
||||
id: @score.id,
|
||||
type: 'scores',
|
||||
id: @match_score.id,
|
||||
type: 'match_scores',
|
||||
attributes: {
|
||||
score: 42
|
||||
points: 42
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -43,16 +43,16 @@ RSpec.describe ScoresController, type: :controller do
|
|||
end
|
||||
|
||||
it 'updates the requested score' do
|
||||
put :update, params: { id: @score.to_param }.merge(valid_update)
|
||||
@score.reload
|
||||
expect(@score.score).to eq(valid_update[:data][:attributes][:score])
|
||||
put :update, params: { id: @match_score.to_param }.merge(valid_update)
|
||||
@match_score.reload
|
||||
expect(@match_score.points).to eq(valid_update[:data][:attributes][:points])
|
||||
end
|
||||
|
||||
it 'renders a response with the updated team' do
|
||||
put :update, params: { id: @score.to_param }.merge(valid_update)
|
||||
put :update, params: { id: @match_score.to_param }.merge(valid_update)
|
||||
expect(response).to be_successful
|
||||
body = deserialize_response response
|
||||
expect(body[:score]).to eq(valid_update[:data][:attributes][:score])
|
||||
expect(body[:points]).to eq(valid_update[:data][:attributes][:points])
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -62,7 +62,7 @@ RSpec.describe ScoresController, type: :controller do
|
|||
end
|
||||
|
||||
it 'renders a forbidden error response' do
|
||||
put :update, params: { id: @score.to_param }.merge(valid_update)
|
||||
put :update, params: { id: @match_score.to_param }.merge(valid_update)
|
||||
expect(response).to have_http_status(:forbidden)
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe MatchScoresController, type: :routing do
|
||||
describe 'routing' do
|
||||
it 'routes to #show' do
|
||||
expect(get: '/match_scores/1').to route_to('match_scores#show', id: '1')
|
||||
end
|
||||
|
||||
it 'routes to #update via PUT' do
|
||||
expect(put: '/match_scores/1').to route_to('match_scores#update', id: '1')
|
||||
end
|
||||
|
||||
it 'routes to #update via PATCH' do
|
||||
expect(patch: '/match_scores/1').to route_to('match_scores#update', id: '1')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe ScoresController, type: :routing do
|
||||
describe 'routing' do
|
||||
it 'routes to #show' do
|
||||
expect(get: '/scores/1').to route_to('scores#show', id: '1')
|
||||
end
|
||||
|
||||
it 'routes to #update via PUT' do
|
||||
expect(put: '/scores/1').to route_to('scores#update', id: '1')
|
||||
end
|
||||
|
||||
it 'routes to #update via PATCH' do
|
||||
expect(patch: '/scores/1').to route_to('scores#update', id: '1')
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue