Skip to content

RailAndBusが指定された場合はSQLレベルでRailを先頭にソート#1381

Merged
TinyKitten merged 1 commit into
devfrom
fix/nearby-stations
Jan 16, 2026
Merged

RailAndBusが指定された場合はSQLレベルでRailを先頭にソート#1381
TinyKitten merged 1 commit into
devfrom
fix/nearby-stations

Conversation

@TinyKitten

@TinyKitten TinyKitten commented Jan 16, 2026

Copy link
Copy Markdown
Member

Summary by CodeRabbit

リリースノート

  • 改善
    • 座標検索による駅検索結果の表示順序ロジックを最適化しました。
    • 交通機関タイプに基づくフィルタリング処理の動作を改善し、より正確な検索結果の並び替えを実現しました。

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai

coderabbitai Bot commented Jan 16, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

駅リポジトリのSQL ORDER BY句を修正し、transport_type がNULLの場合は駅をtransport_type値でソートしてから距離でソートするよう2段階のソートを導入。同時にgRPCコントローラーから Rail を優先するための条件付きソートロジックを削除。

Changes

Cohort / File(s) 変更内容
SQL順序付けロジック更新
stationapi/src/infrastructure/station_repository.rs
ORDER BY句にCASE式を追加し、transport_typeが指定されない場合にtransport_type値による初期ソートキーを導入。距離ベースのソートは既存のロジックで維持。
コントローラーのソートロジック削除
stationapi/src/presentation/controller/grpc.rs
TransportTypeインポートを削除。RailAndBus要求時にRailを優先するための条件付きソートブロックを削除。stationsの可変バインディングを削除。

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

🐰✨ Order bytes rearranged with care,
SQL sorts stations here and there,
Rails no longer claim the lead,
Distance speaks—the new creed! 🚉

🚥 Pre-merge checks | ✅ 2 | ❌ 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 (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed PRタイトルは変更セットの主要な目的を明確に表現している。SQLレベルでRailAndBus指定時にRailを優先的にソートするという、ファイル間の一貫した変更内容を的確に要約している。

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings


📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7b89c15 and 09f4964.

📒 Files selected for processing (2)
  • stationapi/src/infrastructure/station_repository.rs
  • stationapi/src/presentation/controller/grpc.rs
🧰 Additional context used
📓 Path-based instructions (4)
stationapi/src/infrastructure/**/*.rs

📄 CodeRabbit inference engine (AGENTS.md)

stationapi/src/infrastructure/**/*.rs: Repository implementations in infrastructure layer should share an Arc<PgPool> connection pool
Integration tests in infrastructure layer must use #[cfg_attr(not(feature = "integration-tests"), ignore)] attribute to conditionally ignore tests when the feature is not enabled

Files:

  • stationapi/src/infrastructure/station_repository.rs
stationapi/src/**/*.rs

📄 CodeRabbit inference engine (AGENTS.md)

Run cargo test --lib --package stationapi or make test-unit for unit tests focusing on entities and repository mocks without database

Files:

  • stationapi/src/infrastructure/station_repository.rs
  • stationapi/src/presentation/controller/grpc.rs
**/*.rs

📄 CodeRabbit inference engine (AGENTS.md)

**/*.rs: Before committing, run cargo fmt on all Rust code
Before committing, run cargo clippy --all-targets --all-features and resolve new Clippy warnings unless covered by existing #![allow] attributes

Files:

  • stationapi/src/infrastructure/station_repository.rs
  • stationapi/src/presentation/controller/grpc.rs
stationapi/src/presentation/controller/**/*.rs

📄 CodeRabbit inference engine (AGENTS.md)

gRPC presentation layer controller should wire service implementations (e.g., MyApi) to generated server types in presentation/controller/grpc.rs

Files:

  • stationapi/src/presentation/controller/grpc.rs
🧠 Learnings (8)
📓 Common learnings
Learnt from: TinyKitten
Repo: TrainLCD/StationAPI PR: 1305
File: stationapi/.sqlx/query-55bee6b45754d1da9bf555d319f015ce232f8574a41c3ef25b730d11071f8428.json:3-3
Timestamp: 2025-08-06T12:47:37.438Z
Learning: TinyKittenのStationAPIプロジェクトにおいて、SQLクエリの `pass <> 1` 条件は一部のクエリでは不要であり、PostgreSQL移行時にコードの最適化として意図的に除去された。この条件の有無についてはドメイン知識が必要で、機械的にチェックすべきではない。
📚 Learning: 2025-11-25T10:50:36.694Z
Learnt from: CR
Repo: TrainLCD/StationAPI PR: 0
File: AGENTS.md:0-0
Timestamp: 2025-11-25T10:50:36.694Z
Learning: Applies to stationapi/src/use_case/interactor/query.rs : When modifying `QueryInteractor`, ensure enrichment steps (companies, train types, line symbols) still behave as expected and verify helper methods such as `update_station_vec_with_attributes` and `build_route_tree_map`

Applied to files:

  • stationapi/src/infrastructure/station_repository.rs
  • stationapi/src/presentation/controller/grpc.rs
📚 Learning: 2025-11-25T10:50:36.694Z
Learnt from: CR
Repo: TrainLCD/StationAPI PR: 0
File: AGENTS.md:0-0
Timestamp: 2025-11-25T10:50:36.694Z
Learning: Applies to stationapi/src/**/*.rs : Run `cargo test --lib --package stationapi` or `make test-unit` for unit tests focusing on entities and repository mocks without database

Applied to files:

  • stationapi/src/infrastructure/station_repository.rs
  • stationapi/src/presentation/controller/grpc.rs
📚 Learning: 2025-11-25T10:50:36.694Z
Learnt from: CR
Repo: TrainLCD/StationAPI PR: 0
File: AGENTS.md:0-0
Timestamp: 2025-11-25T10:50:36.694Z
Learning: Applies to stationapi/src/domain/**/*.rs : In Rust domain layer, entity definitions and repository abstractions should be organized in the `entity/` module mirroring the gRPC schema, with repository interfaces using `async_trait`

Applied to files:

  • stationapi/src/infrastructure/station_repository.rs
  • stationapi/src/presentation/controller/grpc.rs
📚 Learning: 2025-11-25T10:50:36.694Z
Learnt from: CR
Repo: TrainLCD/StationAPI PR: 0
File: AGENTS.md:0-0
Timestamp: 2025-11-25T10:50:36.694Z
Learning: Applies to stationapi/src/presentation/controller/**/*.rs : gRPC presentation layer controller should wire service implementations (e.g., `MyApi`) to generated server types in `presentation/controller/grpc.rs`

Applied to files:

  • stationapi/src/presentation/controller/grpc.rs
📚 Learning: 2025-11-25T10:50:36.694Z
Learnt from: CR
Repo: TrainLCD/StationAPI PR: 0
File: AGENTS.md:0-0
Timestamp: 2025-11-25T10:50:36.694Z
Learning: Applies to stationapi/proto/**/*.proto : Keep gRPC contract definitions in `proto/stationapi.proto` and use `build.rs` with `tonic-build` to generate server code and `FILE_DESCRIPTOR_SET`

Applied to files:

  • stationapi/src/presentation/controller/grpc.rs
📚 Learning: 2025-11-25T10:50:36.694Z
Learnt from: CR
Repo: TrainLCD/StationAPI PR: 0
File: AGENTS.md:0-0
Timestamp: 2025-11-25T10:50:36.694Z
Learning: Applies to stationapi/src/domain/repository/**/*.rs : Use `async_trait` for defining repository abstractions in Rust

Applied to files:

  • stationapi/src/presentation/controller/grpc.rs
📚 Learning: 2025-11-25T10:50:36.694Z
Learnt from: CR
Repo: TrainLCD/StationAPI PR: 0
File: AGENTS.md:0-0
Timestamp: 2025-11-25T10:50:36.694Z
Learning: Applies to stationapi/src/use_case/**/*.rs : Application logic in use case layer should implement contract traits defined in `traits/` module (e.g., `QueryUseCase` in `interactor/query.rs`)

Applied to files:

  • stationapi/src/presentation/controller/grpc.rs
🧬 Code graph analysis (1)
stationapi/src/presentation/controller/grpc.rs (1)
stationapi/src/use_case/interactor/query.rs (1)
  • stations (235-238)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Test Suite
  • GitHub Check: Test Suite
🔇 Additional comments (3)
stationapi/src/presentation/controller/grpc.rs (2)

2-2: LGTM - インポートの整理

TransportType のインポートが削除され、TransportTypeFilter のみが残されています。これはgRPCコントローラー層で必要なものだけをインポートする適切な変更です。


117-138: LGTM - ソートロジックのリポジトリ層への移動

get_stations_by_coordinatesからRailAndBus時のRail優先ソートロジックが削除され、SQLレベルでの実装(station_repository.rs)に委譲されました。

  • mutキーワードの削除は、後処理が不要になったため適切です
  • データベース層でソートを行うことで、アプリケーション層での追加処理を削減し、効率が向上します
  • コントローラーの責務がシンプルになり、可読性が向上しています
stationapi/src/infrastructure/station_repository.rs (1)

987-989: SQLレベルでのRail優先ソート実装は正確に検証されました

ORDER BY句の2段階ソートロジックは正しく実装されています:

  1. $4 IS NULLRailAndBusの場合): COALESCE(s.transport_type, 0)でソート → Rail(0)がBus(1+)より先に
  2. $4が指定されている場合: CASEは常に0を返すため、距離のみでソート

use case層でのfilter_to_db_type()関数により、TransportTypeFilter::RailAndBusは正しくNoneに変換され、リポジトリに渡されます。get_stations_by_coordinates()内での呼び出し(line 154)でこのマッピングが適切に使用されており、ドメイン層のテストケース(line 2480)でも動作が検証されています。

gRPCコントローラー層でのRust側ソート処理が不要になり、パフォーマンスが向上します。

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.


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

@TinyKitten TinyKitten merged commit 801de75 into dev Jan 16, 2026
10 checks passed
@TinyKitten TinyKitten deleted the fix/nearby-stations branch January 16, 2026 06:10
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.

1 participant