Skip to content

fix/QuestionGroupsで存在しないcampIDだったら404を返す#363

Merged
Ida-ji merged 10 commits into
mainfrom
fix/add404
Jul 2, 2026
Merged

fix/QuestionGroupsで存在しないcampIDだったら404を返す#363
Ida-ji merged 10 commits into
mainfrom
fix/add404

Conversation

@Ida-ji

@Ida-ji Ida-ji commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

close #140
QuestionGroupsで返り値が無かった場合に、campIDの存在確認をするようにした
RouterのQuestionGroupにNot Foundを追加した
OpenAPIに404 Not Foundを追加した

Summary by CodeRabbit

  • Bug Fixes
    • キャンプID指定の問合せ(GET /api/camps/{campId}/question-groups、管理用POST /api/admin/camps/{campId}/question-groups)で、対象キャンプが存在しない場合は 404 Not Found"Camp not found")を返すようになりました。結果が0件の場合は従来どおり空で返します。
    • API仕様のレスポンス定義に 404 を追加し、エラー応答を明確化しました。
  • Tests
    • 存在しないキャンプ時の 404、メッセージ("Camp not found")、戻り値(nil)を検証するケースを追加しました。

@Ida-ji Ida-ji requested a review from akimon658 June 30, 2026 09:48
@Ida-ji Ida-ji self-assigned this Jun 30, 2026
@coderabbitai

coderabbitai Bot commented Jun 30, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

QuestionGroup の取得と作成で、存在しない campID を ErrCampNotFound に統一し、ルーターで 404 を返すように変更。OpenAPI 仕様とリポジトリ・ルーター両方のテストも更新されている。

Changes

QuestionGroup の 404 対応

Layer / File(s) Summary
リポジトリ層: camp 存在判定と ErrCampNotFound 返却
repository/gormrepository/question_group.go, repository/gormrepository/question_group_test.go
CreateQuestionGroup の外部キー違反を repository.ErrCampNotFound に変換し、GetQuestionGroups では 0 件時に campExists を確認して存在しない場合のみ ErrCampNotFound を返す。関連テストに Non-existent CampNotFound の検証を追加。
ルーター層: ErrCampNotFound を 404 に変換
router/question_groups.go, router/question_groups_test.go, openapi.yaml
errors.Is(err, repository.ErrCampNotFound) を追加して GET /api/camps/{campId}/question-groupsPOST /api/admin/camps/{campId}/question-groups で 404 を返し、OpenAPI に 404 レスポンスを追加。ルーターテストに Camp Not Found サブケースを追加。

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • traPtitech/rucQ#350: repository.ErrCampNotFound を HTTP 404 "Camp not found" に変換するルーター側の扱いが近いです。

Suggested reviewers: akimon658

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed タイトルは存在しないcampIDで404を返す変更を簡潔に表しており、主な差分と一致しています。
Linked Issues check ✅ Passed 取得・作成の両方で存在しないcampIDを404にする要件が、repository・router・OpenAPI・テストで実装されています。
Out of Scope Changes check ✅ Passed OpenAPI、repository、router、テストの変更はいずれも非存在campIDを404にする目的に沿っており、明らかな逸脱はありません。
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/add404

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@repository/gormrepository/question_group_test.go`:
- Around line 219-229: The “Non-existent Camp” subtest in TestGetQuestionGroup
is calling the wrong repository method, so it never exercises the newly added
ErrCampNotFound branch. Update the subtest to invoke GetQuestionGroups on the
repository instance r instead of GetRoomGroups, and keep the existing assertions
against repository.ErrCampNotFound and a nil result so the question_group.go
error path is actually covered.

In `@router/question_groups_test.go`:
- Around line 141-156: The Camp Not Found subtest in TestGetQuestionGroups is
exercising the room-groups path instead of the question-groups path, so it never
verifies the new 404 mapping in question_groups.go. Update this subtest to use
MockQuestionGroupRepository.GetQuestionGroups and send the request to the
question-groups endpoint (matching the GetQuestionGroups handler) so the
NotFound response is tested against the correct API.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7c64f0ed-2628-4019-9ef0-447120f3c939

📥 Commits

Reviewing files that changed from the base of the PR and between c53a359 and b4684f8.

📒 Files selected for processing (5)
  • openapi.yaml
  • repository/gormrepository/question_group.go
  • repository/gormrepository/question_group_test.go
  • router/question_groups.go
  • router/question_groups_test.go

Comment thread repository/gormrepository/question_group_test.go Outdated
Comment thread router/question_groups_test.go

@akimon658 akimon658 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

良さそうです!

CodeRabbitに指摘されている箇所(取得は修正されているが作成が修正されていない、テスト対象がRoomGroupsになっている)だけ対応をお願いします 🙏

@akimon658

Copy link
Copy Markdown
Member

作成のケースにどう対応するか明示していませんでしたが、たぶんこれもRoomGroupと同様にできそう

if err := gorm.G[model.RoomGroup](r.db).Create(ctx, roomGroup); err != nil {
if errors.Is(err, gorm.ErrForeignKeyViolated) {
return repository.ErrCampNotFound
}
return err
}

QuestionGroupとはちょっとCreateの書き方が違いますが、エラーハンドリングは同じようにやれば大丈夫なはずです

@Ida-ji Ida-ji requested a review from akimon658 June 30, 2026 13:54

@akimon658 akimon658 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

もう少しだけ!

  • OpenAPIの/api/admin/camps/{campId}/question-groupsにも404を追加してほしいです
  • router/question_groups.goAdminPostQuestionGroupsでも、repository.ErrCampNotFoundをチェックしてhttp.StatusNotFoundを返すようにしてほしいです
  • repository、routerそれぞれに、QuestionGroupの作成についてのテストも追加してほしいです

Comment thread repository/gormrepository/question_group_test.go Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@openapi.yaml`:
- Around line 492-493: The response mapping is replacing the existing 400 entry
instead of adding the missing 404 entry, which leaves adminPostQuestionGroup out
of sync with the implementation. Update the OpenAPI response definitions for
this operation so the existing 400 response remains unchanged and add a separate
404 entry that references NotFound. Locate the responses block for
adminPostQuestionGroup and preserve both status codes side by side.

In `@repository/gormrepository/question_group_test.go`:
- Around line 176-188: The current NotFound test only rechecks GetQuestionGroup
and never exercises the missing-camp path, so update this case to target
CreateQuestionGroup instead. Use setup(t) and the repository methods around
QuestionGroup creation to attempt creating a group with a non-existent CampID,
then assert repository.ErrCampNotFound and a nil result. Keep the existing
NotFound-style assertions, but point them at CreateQuestionGroup rather than
GetQuestionGroup so the test covers the intended failure branch.

In `@router/question_groups.go`:
- Around line 45-47: The current ErrCampNotFound check in question group
creation only handles the GetOrCreateUser path, so a missing camp during
tx.CreateQuestionGroup still falls through as a 500. Update the Transaction flow
in the question group handler to inspect the returned error from the full
transaction, and apply the same errors.Is(err, repository.ErrCampNotFound) ->
http.StatusNotFound mapping there as well. Use the existing
repository.ErrCampNotFound handling around GetOrCreateUser and extend it to the
transaction result so missing camp IDs are consistently converted to a 404.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9761dc1b-07e3-4fa6-bbfc-699c4a35f610

📥 Commits

Reviewing files that changed from the base of the PR and between 4d953e5 and 6f659a1.

📒 Files selected for processing (3)
  • openapi.yaml
  • repository/gormrepository/question_group_test.go
  • router/question_groups.go

Comment thread openapi.yaml Outdated
Comment thread repository/gormrepository/question_group_test.go
Comment thread router/question_groups.go Outdated
@Ida-ji Ida-ji requested a review from akimon658 June 30, 2026 15:35
Comment thread router/question_groups.go Outdated
Comment thread router/question_groups_test.go
Comment thread repository/gormrepository/question_group_test.go
@Ida-ji

Ida-ji commented Jun 30, 2026

Copy link
Copy Markdown
Contributor Author

これなんで通らないんでしょうか.......

@akimon658 akimon658 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントしたところ以外は良さそうです

Comment thread router/question_groups_test.go Outdated
Comment thread router/question_groups_test.go

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@router/question_groups_test.go`:
- Around line 400-405: The GetOrCreateUser mock in question_groups_test.go is
expecting a randomly generated userID, but the request being exercised sends
X-Forwarded-User with the value username. Update the expectation in the relevant
test case to match the forwarded-user header value that the router passes into
GetOrCreateUser, and keep the mocked UserRepository call aligned with the actual
request header used in the test.
- Line 406: The test file has a gofmt violation caused by trailing
whitespace/blank line formatting. Re-run gofmt on the affected test content in
router/question_groups_test.go (around the question_groups test block) and
remove any trailing spaces or stray empty lines so the file matches standard Go
formatting.
- Around line 407-409: `CreateQuestionGroup`
のモック戻り値数が実装と一致していません。`MockQuestionGroupRepository.EXPECT().CreateQuestionGroup(gomock.Any())`
は error 単一戻り値なので、ここでは `Return(nil, repository.ErrCampNotFound)`
のような2値返却をやめ、`CreateQuestionGroup` のシグネチャに合わせて `repository.ErrCampNotFound`
だけを返す形に修正してください。`CreateQuestionGroup` と `MockQuestionGroupRepository`
を基準に該当テストを見直し、他の同種モックも戻り値数が一致しているか確認してください。
- Around line 395-397: The request body type in the AdminPostQuestionGroup test
is incorrect: it uses api.AdminPostRoomGroupJSONRequestBody instead of the
question-group payload. Update the req initialization in question_groups_test.go
to use api.AdminPostQuestionGroupJSONRequestBody, and include the fields
expected by AdminPostQuestionGroup, especially Due and Questions, so the test
matches the endpoint being exercised.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1fe31982-4a5a-4d53-acf3-ece7366688a4

📥 Commits

Reviewing files that changed from the base of the PR and between 6fe0ccd and 84a7441.

📒 Files selected for processing (2)
  • repository/gormrepository/question_group_test.go
  • router/question_groups_test.go

Comment thread router/question_groups_test.go Outdated
Comment thread router/question_groups_test.go Outdated
Comment thread router/question_groups_test.go Outdated
Comment thread router/question_groups_test.go Outdated
@Ida-ji Ida-ji requested a review from akimon658 July 1, 2026 17:08
@Ida-ji Ida-ji merged commit ff98dc9 into main Jul 2, 2026
6 checks passed
@Ida-ji Ida-ji deleted the fix/add404 branch July 2, 2026 03:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

QuestionGroupの取得、作成で存在しないcampIDが指定されたら404を返す

2 participants