Skip to content

feat(stop_events): add ApiWeb.StopEventView#979

Open
runkelcorey wants to merge 3 commits intomasterfrom
feat-stop_event-view
Open

feat(stop_events): add ApiWeb.StopEventView#979
runkelcorey wants to merge 3 commits intomasterfrom
feat-stop_event-view

Conversation

@runkelcorey
Copy link
Contributor

@runkelcorey runkelcorey commented Mar 18, 2026

Part 2 of disaggregating #973

Summary of changes

  1. Adds all attributes from Model.StopEvent to ApiWeb.StopEventView
  2. Defines relationships for stop, route, trip, and vehicle using out-of-the-box functionality
  3. Adds functionality for schedule relationship by widening State.Schedule.schedule_for and State.Schedule.schedule_for_many to accept a Model.StopEvent

A future PR will add the location for StopEvents along with the StopEventController. I think that's also the right place to test for included trip/stop/route/vehicle lookups that don't return results.

How were these changes validated?

  1. Wrote tests for ApiWeb.StopEventView

What questions should reviewers consider?

  1. Should we define any other attributes to return via include?
  2. Should I update the State.ScheduleTest to test a slightly wider type? I think the current tests are sufficient b/c the functionality doesn't really change

@runkelcorey runkelcorey requested a review from a team as a code owner March 18, 2026 21:33
@runkelcorey runkelcorey requested review from jadrian and removed request for a team March 18, 2026 21:33
@runkelcorey runkelcorey added enhancement New feature or request elixir Pull requests that update Elixir code labels Mar 18, 2026
@runkelcorey runkelcorey self-assigned this Mar 18, 2026
@runkelcorey runkelcorey requested review from EmmaSimon and huangh March 18, 2026 21:33
Copy link
Contributor

@EmmaSimon EmmaSimon left a comment

Choose a reason for hiding this comment

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

Looks perfect!

field: :vehicle_id
)

attributes([
Copy link

@jadrian jadrian Mar 20, 2026

Choose a reason for hiding this comment

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

I think it's redundant to declare :vehicle_id, :trip_id, etc here; the has_one calls above implicitly declare these fields as part of the relationships to other types. I'd also suggest putting attributes before all the has_ones, just for stylistic consistency with other view definitions. Compare to stop_view.ex and vehicle_view.ex.

def departed(%{departed: %DateTime{} = dt}, _conn), do: DateTime.to_iso8601(dt)

@doc """
Preloads schedule relationships for stop events when requested via ?include=schedule to prevent N+1 queries.
Copy link

Choose a reason for hiding this comment

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

Do we want to preload other relationships too? I believe preload(super, ...) would do that.

assert get_in(rendered, ["data", Access.at(0), "relationships", "schedule", "data", "id"]) ==
"schedule-trip1-stop1-1"

assert get_in(rendered, ["data", Access.at(1), "relationships", "schedule", "data", "id"]) ==
Copy link

Choose a reason for hiding this comment

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

Should you also assert that rendered["included"] contains the actual contents of the schedules? (Here and in the next test case.)

refute Map.has_key?(relationships, "schedule")
end

test "preloads schedules for multiple stop_events", %{conn: conn} do
Copy link

Choose a reason for hiding this comment

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

I see that here you're testing for ?include=schedule in the request, but do you also want to support (for example) ?include=trip? If so there should be a test case for at least one of the relationships.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

elixir Pull requests that update Elixir code enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants