グカネータ作成 (#041) #362

マージ済み
みてるぞ が 13 個のコミットを feature/041 から main へマージ 2026-06-10 23:33:57 +09:00
4個のファイルの変更33行の追加11行の削除
コミット fb2b2a632c の変更だけを表示してゐます - すべてのコミットを表示
+10 -4
ファイルの表示
@@ -2,16 +2,22 @@ class GekanatorGamesController < ApplicationController
def create def create
return head :unauthorized unless current_user return head :unauthorized unless current_user
guessed_post_id = params.require(:guessed_post_id)
correct_post_id = params[:correct_post_id].presence
answers = params.require(:answers).as_json answers = params.require(:answers).as_json
game = GekanatorGame.create!( game = GekanatorGame.new(
user: current_user, user: current_user,
guessed_post_id: params.require(:guessed_post_id), guessed_post_id:,
correct_post_id: params[:correct_post_id].presence, correct_post_id:,
won: params[:guessed_post_id].to_i == params[:correct_post_id].to_i, won: correct_post_id.present? && guessed_post_id.to_i == correct_post_id.to_i,
question_count: params.require(:question_count), question_count: params.require(:question_count),
answers:) answers:)
if game.save
render json: { id: game.id }, status: :created render json: { id: game.id }, status: :created
else
render json: { errors: game.errors.full_messages }, status: :unprocessable_entity
end
end end
end end
+2 -3
ファイルの表示
@@ -1,10 +1,9 @@
class GekanatorGame < ApplicationRecord class GekanatorGame < ApplicationRecord
belongs_to :user, optional: true belongs_to :user
belongs_to :guessed_post, class_name: 'Post' belongs_to :guessed_post, class_name: 'Post'
belongs_to :correct_post, class_name: 'Post', optional: true belongs_to :correct_post, class_name: 'Post'
validates :answers, presence: true validates :answers, presence: true
validates :correct_post, presence: true
validates :question_count, numericality: { greater_than_or_equal_to: 0 } validates :question_count, numericality: { greater_than_or_equal_to: 0 }
validates :won, inclusion: { in: [true, false] } validates :won, inclusion: { in: [true, false] }
end end
@@ -0,0 +1,17 @@
class RequireGekanatorGameUserAndCorrectPost < ActiveRecord::Migration[8.0]
def up
execute <<~SQL.squish
UPDATE gekanator_games
SET correct_post_id = guessed_post_id
WHERE correct_post_id IS NULL
SQL
change_column_null :gekanator_games, :user_id, false
change_column_null :gekanator_games, :correct_post_id, false
end
def down
change_column_null :gekanator_games, :correct_post_id, true
change_column_null :gekanator_games, :user_id, true
end
end
生成ファイル
+3 -3
ファイルの表示
@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.0].define(version: 2026_06_07_000000) do ActiveRecord::Schema[8.0].define(version: 2026_06_08_000000) do
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "name", null: false t.string "name", null: false
t.string "record_type", null: false t.string "record_type", null: false
@@ -49,9 +49,9 @@ ActiveRecord::Schema[8.0].define(version: 2026_06_07_000000) do
end end
create_table "gekanator_games", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| create_table "gekanator_games", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.bigint "user_id" t.bigint "user_id", null: false
t.bigint "guessed_post_id", null: false t.bigint "guessed_post_id", null: false
t.bigint "correct_post_id" t.bigint "correct_post_id", null: false
t.boolean "won", null: false t.boolean "won", null: false
t.integer "question_count", null: false t.integer "question_count", null: false
t.json "answers", null: false t.json "answers", null: false