From 5de08bd786481bf8b0418aa7400d3a65d96d29cd Mon Sep 17 00:00:00 2001 From: "tembo[bot]" <208362400+tembo-io[bot]@users.noreply.github.com> Date: Tue, 18 Nov 2025 01:51:06 +0000 Subject: [PATCH] feat: Display flights with pilot names on airplane show page Co-authored-by: Chris --- lib/acm_uc_demo/airplanes.ex | 20 ++++++++++++++++ lib/acm_uc_demo/airplanes/airplane.ex | 2 ++ lib/acm_uc_demo/flights/flight.ex | 5 ++-- .../live/airplane_live/show.ex | 24 ++++++++++++++++++- 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/lib/acm_uc_demo/airplanes.ex b/lib/acm_uc_demo/airplanes.ex index b2dc423..6c241b3 100644 --- a/lib/acm_uc_demo/airplanes.ex +++ b/lib/acm_uc_demo/airplanes.ex @@ -37,6 +37,26 @@ defmodule AcmUcDemo.Airplanes do """ def get_airplane!(id), do: Repo.get!(Airplane, id) + @doc """ + Gets a single airplane with flights preloaded (including pilot information). + + Raises `Ecto.NoResultsError` if the Airplane does not exist. + + ## Examples + + iex> get_airplane_with_flights!(123) + %Airplane{flights: [%Flight{pilot: %Pilot{}}, ...]} + + iex> get_airplane_with_flights!(456) + ** (Ecto.NoResultsError) + + """ + def get_airplane_with_flights!(id) do + Airplane + |> Repo.get!(id) + |> Repo.preload(flights: :pilot) + end + @doc """ Creates a airplane. diff --git a/lib/acm_uc_demo/airplanes/airplane.ex b/lib/acm_uc_demo/airplanes/airplane.ex index d3aaf99..e1f44c7 100644 --- a/lib/acm_uc_demo/airplanes/airplane.ex +++ b/lib/acm_uc_demo/airplanes/airplane.ex @@ -8,6 +8,8 @@ defmodule AcmUcDemo.Airplanes.Airplane do field :tail_number, :string field :initial_hours, :decimal + has_many :flights, AcmUcDemo.Flights.Flight + timestamps(type: :utc_datetime) end diff --git a/lib/acm_uc_demo/flights/flight.ex b/lib/acm_uc_demo/flights/flight.ex index 9f82290..6e97185 100644 --- a/lib/acm_uc_demo/flights/flight.ex +++ b/lib/acm_uc_demo/flights/flight.ex @@ -5,8 +5,9 @@ defmodule AcmUcDemo.Flights.Flight do schema "flights" do field :hobbs_reading, :decimal field :notes, :string - field :pilot_id, :id - field :airplane_id, :id + + belongs_to :pilot, AcmUcDemo.Pilots.Pilot + belongs_to :airplane, AcmUcDemo.Airplanes.Airplane timestamps(type: :utc_datetime) end diff --git a/lib/acm_uc_demo_web/live/airplane_live/show.ex b/lib/acm_uc_demo_web/live/airplane_live/show.ex index d51b881..0223dd4 100644 --- a/lib/acm_uc_demo_web/live/airplane_live/show.ex +++ b/lib/acm_uc_demo_web/live/airplane_live/show.ex @@ -26,6 +26,28 @@ defmodule AcmUcDemoWeb.AirplaneLive.Show do <:item title="Tail number">{@airplane.tail_number} <:item title="Initial hours">{@airplane.initial_hours} + +
+ <.header> + Flights + <:subtitle>Flight history for this airplane + + +
+ <%= if @airplane.flights == [] do %> +
No flights recorded yet.
+ <% else %> + <.table id="flights-table" rows={@airplane.flights}> + <:col :let={flight} label="Pilot">{flight.pilot.name} + <:col :let={flight} label="Hobbs Reading">{flight.hobbs_reading} + <:col :let={flight} label="Notes">{flight.notes} + <:col :let={flight} label="Date"> + {Calendar.strftime(flight.inserted_at, "%b %d, %Y %I:%M %p")} + + + <% end %> +
+
""" end @@ -35,6 +57,6 @@ defmodule AcmUcDemoWeb.AirplaneLive.Show do {:ok, socket |> assign(:page_title, "Show Airplane") - |> assign(:airplane, Airplanes.get_airplane!(id))} + |> assign(:airplane, Airplanes.get_airplane_with_flights!(id))} end end