Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update games serializer and add slug validation with default generation #575

Merged
merged 7 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ GEM
hashdiff (1.1.2)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
io-console (0.7.2)
io-console (0.8.0)
irb (1.14.1)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
json (2.8.2)
json (2.9.0)
json-schema (5.1.1)
addressable (~> 2.8)
bigdecimal (~> 3.1)
Expand Down Expand Up @@ -193,7 +193,7 @@ GEM
net-smtp
marcel (1.0.4)
mini_mime (1.1.5)
minitest (5.25.2)
minitest (5.25.4)
msgpack (1.7.5)
net-imap (0.5.1)
date
Expand All @@ -205,17 +205,17 @@ GEM
net-smtp (0.5.0)
net-protocol
nio4r (2.7.4)
nokogiri (1.16.7-aarch64-linux)
nokogiri (1.17.0-aarch64-linux)
racc (~> 1.4)
nokogiri (1.16.7-arm-linux)
nokogiri (1.17.0-arm-linux)
racc (~> 1.4)
nokogiri (1.16.7-arm64-darwin)
nokogiri (1.17.0-arm64-darwin)
racc (~> 1.4)
nokogiri (1.16.7-x86-linux)
nokogiri (1.17.0-x86-linux)
racc (~> 1.4)
nokogiri (1.16.7-x86_64-darwin)
nokogiri (1.17.0-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.16.7-x86_64-linux)
nokogiri (1.17.0-x86_64-linux)
racc (~> 1.4)
openssl (3.2.0)
ostruct (0.6.1)
Expand Down Expand Up @@ -274,9 +274,9 @@ GEM
activesupport (>= 4.2)
choice (~> 0.2.0)
ruby-graphviz (~> 1.2)
rails-html-sanitizer (1.6.0)
rails-html-sanitizer (1.6.1)
loofah (~> 2.21)
nokogiri (~> 1.14)
nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
railties (7.2.2)
actionpack (= 7.2.2)
activesupport (= 7.2.2)
Expand All @@ -291,7 +291,7 @@ GEM
rb-inotify (0.11.1)
ffi (~> 1.0)
rb-readline (0.5.5)
rbs (3.6.1)
rbs (3.7.0)
logger
rdoc (6.8.1)
psych (>= 4.0.0)
Expand Down Expand Up @@ -339,7 +339,7 @@ GEM
rspec-expectations (~> 3.13)
rspec-mocks (~> 3.13)
rspec-support (~> 3.13)
rspec-support (3.13.1)
rspec-support (3.13.2)
rspec_junit_formatter (0.6.0)
rspec-core (>= 2, < 4, != 2.12.0)
rswag-api (2.16.0)
Expand All @@ -353,21 +353,21 @@ GEM
rswag-ui (2.16.0)
actionpack (>= 5.2, < 8.1)
railties (>= 5.2, < 8.1)
rubocop (1.69.0)
rubocop (1.69.1)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 2.4, < 3.0)
rubocop-ast (>= 1.36.1, < 2.0)
regexp_parser (>= 2.9.3, < 3.0)
rubocop-ast (>= 1.36.2, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 4.0)
rubocop-ast (1.36.2)
parser (>= 3.3.1.0)
rubocop-checkstyle_formatter (0.6.0)
rubocop (>= 1.14.0)
rubocop-erb (0.5.4)
rubocop-erb (0.5.5)
better_html
rubocop (~> 1.45)
rubocop-factory_bot (2.26.1)
Expand Down Expand Up @@ -463,7 +463,7 @@ GEM
simplecov-lcov (0.8.0)
simplecov_json_formatter (0.1.4)
smart_properties (1.17.0)
sorbet-runtime (0.5.11670)
sorbet-runtime (0.5.11690)
spring (4.2.1)
sprockets (4.2.1)
concurrent-ruby (~> 1.0)
Expand All @@ -475,7 +475,7 @@ GEM
stimulus-rails (1.3.4)
railties (>= 6.0.0)
stringio (3.1.2)
svix (1.42.0)
svix (1.43.0)
terminal-table (3.0.2)
unicode-display_width (>= 1.1.1, < 3)
thor (1.3.2)
Expand All @@ -492,7 +492,7 @@ GEM
concurrent-ruby (~> 1.0)
unicode-display_width (2.6.0)
uri (1.0.2)
useragent (0.16.10)
useragent (0.16.11)
versionist (2.0.1)
activesupport (>= 3)
railties (>= 3)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/games_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Api
module V1
class GamesController < AbstractApplicationController
self.klass = ::Game
self.serializer_klass = Serializers::Game
self.serializer_klass = Serializers::GameDetails
self.detail_serializer_klass = Serializers::GameDetails
self.enable_pagination = true
protected
Expand Down
3 changes: 1 addition & 2 deletions app/controllers/api/v1/organizations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ def permitted_params
end

def tournaments_permitted_params
params.require(:tournament).permit(
params.permit(
:tournament_id,
:format,
:name,
:start_at, :end_at,
:game_id, :format_id,
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/api/v1/tournaments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ def show

def create
authorize @organization, :create_tournament?

@tournament = ::Tournament.new permitted_params
if @tournament.save

render json: serialize_details, status: :created
else

render json: @tournament.errors, status: :unprocessable_entity
end
rescue ActionController::ParameterMissing => e
Expand Down Expand Up @@ -96,10 +99,12 @@ def set_tournaments

def set_organization
@organization = if permitted_params[:organization_id].present?

::Organization.find(permitted_params[:organization_id])
else
::Tournament.find(params[:id]).organization
end

@organization
rescue ActiveRecord::RecordNotFound
render json: { error: "Organization not found" }, status: :not_found
Expand Down
7 changes: 7 additions & 0 deletions app/models/game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ def self.policy_class
GamePolicy
end
validates :name, presence: true
validates :slug, presence: true, uniqueness: true
has_many :formats, class_name: "Format", dependent: :nullify
has_many :tournaments, class_name: "Tournament", dependent: :nullify

before_validation :set_defaults, on: :create

def set_defaults
self.slug = name&.parameterize if slug.blank? && name.present?
end
end
2 changes: 1 addition & 1 deletion app/policies/organization_policy.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class OrganizationPolicy < ApplicationPolicy
def owner?
record.owner == account
record&.owner&.id == account.id
end

def admin?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class InsertValuesIntoGamesAndFormat < ActiveRecord::Migration[7.2]
def change
add_column :games, :slug, :string, null: false
add_index :games, :slug, unique: true

sv = Game.create(name: "Scarlet & Violet", slug: "sv")
swsh = Game.create(name: "Sword & Shield", slug: "swsh")
lgpe = Game.create(name: "Let's Go Pikachu & Eevee", slug: "lgpe")
bdsp = Game.create(name: "Brilliand Diamond & Shining Pearl", slug: "bdsp")
pogo = Game.create(name: "Pokemon Go", slug: "pogo")

Format.create(name: "Regulation A", game: sv)
Format.create(name: "Regulation B", game: sv)
Format.create(name: "Regulation C", game: sv)
Format.create(name: "Regulation D", game: sv)
Format.create(name: "Regulation E", game: sv)
Format.create(name: "Regulation F", game: sv)
Format.create(name: "Regulation G", game: sv)
Format.create(name: "Regulation H", game: sv)
Format.create(name: "Regulation I", game: sv)
Format.create(name: "Regulation J", game: sv)

Format.create(name: "Series 1", game: swsh)
Format.create(name: "Series 2", game: swsh)
Format.create(name: "Series 3", game: swsh)
Format.create(name: "Series 4", game: swsh)
Format.create(name: "Series 5", game: swsh)
Format.create(name: "Series 6", game: swsh)
Format.create(name: "Series 7", game: swsh)
Format.create(name: "Series 8", game: swsh)
Format.create(name: "Series 9", game: swsh)
Format.create(name: "Series 10", game: swsh)
Format.create(name: "Series 11", game: swsh)
Format.create(name: "Series 12", game: swsh)
Format.create(name: "Series 13", game: swsh)

Format.create(name: "National Dex", game: lgpe)
Format.create(name: "National Dex", game: bdsp)

Format.create(name: "Great League", game: pogo)
Format.create(name: "Ultra League", game: pogo)
Format.create(name: "Master League", game: pogo)
end
end
4 changes: 3 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def create_format(name:, game:)
Format.find_or_create_by!(name:, game:)
end

game = Game.find_or_create_by!(name: "Pokemon VGC")
game = Game.find_or_create_by!(name: "Scarlet & Violet", slug: "sv")

format = Format.find_or_create_by!(name: "Regulation H", game: game)

Expand Down
17 changes: 3 additions & 14 deletions openapi/v1/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -754,13 +754,13 @@ paths:
application/json:
schema:
"$ref": "#/components/schemas/TournamentDetails"
'400':
description: bad request
'422':
description: unprocessable_entity
requestBody:
content:
application/json:
schema:
"$ref": "#/components/schemas/TournamentDetails"
"$ref": "#/components/schemas/TournamentPostRequest"
"/tournaments/{tournament_id}/phases":
parameters:
- name: tournament_id
Expand Down Expand Up @@ -2320,10 +2320,6 @@ components:
type: string
format: date-time
nullable: true
registration_end_at:
type: string
format: date-time
nullable: true
late_registration:
type: boolean
check_in_start_at:
Expand Down Expand Up @@ -2351,9 +2347,6 @@ components:
type: object
title: Tournament Post Request
properties:
organization_id:
type: integer
format: int64
name:
type: string
game_id:
Expand All @@ -2374,10 +2367,6 @@ components:
type: string
format: date-time
nullable: true
registration_end_at:
type: string
format: date-time
nullable: true
late_registration:
type: boolean
check_in_start_at:
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/api/v1/games_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
parameter name: :id, in: :path, type: :integer, description: "ID of the game", required: true
parameter VERCEL_TOKEN_HEADER_PARAMETER

let(:test_game) { create(:game, name: "Test Game") }
let(:test_game) { create(:game, name: "Test Game", slug: "tg") }
let(:id) { test_game.id }

get("Show Game") do
Expand Down
15 changes: 10 additions & 5 deletions spec/requests/api/v1/organizations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@
description "Creates a new tournament for a given organization."
operationId "postOrganizationTournament"

parameter name: :tournament, in: :body, schema: { "$ref" => "#/components/schemas/TournamentDetails" }
parameter name: :tournament, in: :body, schema: { "$ref" => "#/components/schemas/TournamentPostRequest" }

security [Bearer: []]

Expand All @@ -302,7 +302,7 @@
let(:format) { create(:format, game:) }
let(:tournament) do
{
tournament: {

name: "New Tournament",
start_at: Time.current.iso8601,
end_at: 1.day.from_now,
Expand All @@ -315,7 +315,7 @@
late_registration: false,
open_team_sheets: false,
teamlists_required: false
}

}
end

Expand All @@ -325,9 +325,14 @@
run_test!
end

response(400, "bad request") do
response(422, "unprocessable_entity") do
let(:request_account) { owner }
let(:tournament) { {} }
let(:game) { create(:game) }
let(:format) { create(:format, game:) }
let(:tournament) { {

}
}

include_context "with Request Specs - Clerk JWT + Vercel OIDC Token Verification"

Expand Down
Loading
Loading