diff --git a/lib/acm_uc_demo/airplanes/airplane.ex b/lib/acm_uc_demo/airplanes/airplane.ex
index d3aaf99..3f64e3e 100644
--- a/lib/acm_uc_demo/airplanes/airplane.ex
+++ b/lib/acm_uc_demo/airplanes/airplane.ex
@@ -7,6 +7,7 @@ defmodule AcmUcDemo.Airplanes.Airplane do
field :model, :string
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.ex b/lib/acm_uc_demo/flights.ex
index 08ea53d..e86c40a 100644
--- a/lib/acm_uc_demo/flights.ex
+++ b/lib/acm_uc_demo/flights.ex
@@ -21,6 +21,24 @@ defmodule AcmUcDemo.Flights do
Repo.all(Flight)
end
+ @doc """
+ Returns the list of flights for a given airplane with pilot preloaded.
+
+ ## Examples
+
+ iex> list_flights_by_airplane(airplane_id)
+ [%Flight{pilot: %Pilot{}}, ...]
+
+ """
+ def list_flights_by_airplane(airplane_id) do
+ from(f in Flight,
+ where: f.airplane_id == ^airplane_id,
+ order_by: [desc: f.inserted_at],
+ preload: [:pilot]
+ )
+ |> Repo.all()
+ end
+
@doc """
Gets a single flight.
diff --git a/lib/acm_uc_demo/flights/flight.ex b/lib/acm_uc_demo/flights/flight.ex
index 9f82290..cb2b946 100644
--- a/lib/acm_uc_demo/flights/flight.ex
+++ b/lib/acm_uc_demo/flights/flight.ex
@@ -5,8 +5,8 @@ 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/pilots/pilot.ex b/lib/acm_uc_demo/pilots/pilot.ex
index d300cf5..18825b4 100644
--- a/lib/acm_uc_demo/pilots/pilot.ex
+++ b/lib/acm_uc_demo/pilots/pilot.ex
@@ -5,6 +5,7 @@ defmodule AcmUcDemo.Pilots.Pilot do
schema "pilots" do
field :name, :string
field :certificate_number, :string
+ has_many :flights, AcmUcDemo.Flights.Flight
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..4ed2ea7 100644
--- a/lib/acm_uc_demo_web/live/airplane_live/show.ex
+++ b/lib/acm_uc_demo_web/live/airplane_live/show.ex
@@ -2,6 +2,7 @@ defmodule AcmUcDemoWeb.AirplaneLive.Show do
use AcmUcDemoWeb, :live_view
alias AcmUcDemo.Airplanes
+ alias AcmUcDemo.Flights
@impl true
def render(assigns) do
@@ -26,15 +27,43 @@ 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 @flights == [] do %>
+
+ No flights recorded for this airplane yet.
+
+ <% else %>
+ <.table id="flights-table" rows={@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, "%Y-%m-%d %I:%M %p")}
+
+
+ <% end %>
+
+
"""
end
@impl true
def mount(%{"id" => id}, _session, socket) do
+ airplane = Airplanes.get_airplane!(id)
+ flights = Flights.list_flights_by_airplane(id)
+
{:ok,
socket
|> assign(:page_title, "Show Airplane")
- |> assign(:airplane, Airplanes.get_airplane!(id))}
+ |> assign(:airplane, airplane)
+ |> assign(:flights, flights)}
end
end
diff --git a/test/acm_uc_demo/airplanes_test.exs b/test/acm_uc_demo/airplanes_test.exs
index 8ca9985..a8640ea 100644
--- a/test/acm_uc_demo/airplanes_test.exs
+++ b/test/acm_uc_demo/airplanes_test.exs
@@ -21,7 +21,12 @@ defmodule AcmUcDemo.AirplanesTest do
end
test "create_airplane/1 with valid data creates a airplane" do
- valid_attrs = %{make: "some make", model: "some model", tail_number: "some tail_number", initial_hours: "120.5"}
+ valid_attrs = %{
+ make: "some make",
+ model: "some model",
+ tail_number: "some tail_number",
+ initial_hours: "120.5"
+ }
assert {:ok, %Airplane{} = airplane} = Airplanes.create_airplane(valid_attrs)
assert airplane.make == "some make"
@@ -36,7 +41,13 @@ defmodule AcmUcDemo.AirplanesTest do
test "update_airplane/2 with valid data updates the airplane" do
airplane = airplane_fixture()
- update_attrs = %{make: "some updated make", model: "some updated model", tail_number: "some updated tail_number", initial_hours: "456.7"}
+
+ update_attrs = %{
+ make: "some updated make",
+ model: "some updated model",
+ tail_number: "some updated tail_number",
+ initial_hours: "456.7"
+ }
assert {:ok, %Airplane{} = airplane} = Airplanes.update_airplane(airplane, update_attrs)
assert airplane.make == "some updated make"
diff --git a/test/acm_uc_demo/pilots_test.exs b/test/acm_uc_demo/pilots_test.exs
index 22b00d3..09f4929 100644
--- a/test/acm_uc_demo/pilots_test.exs
+++ b/test/acm_uc_demo/pilots_test.exs
@@ -34,7 +34,11 @@ defmodule AcmUcDemo.PilotsTest do
test "update_pilot/2 with valid data updates the pilot" do
pilot = pilot_fixture()
- update_attrs = %{name: "some updated name", certificate_number: "some updated certificate_number"}
+
+ update_attrs = %{
+ name: "some updated name",
+ certificate_number: "some updated certificate_number"
+ }
assert {:ok, %Pilot{} = pilot} = Pilots.update_pilot(pilot, update_attrs)
assert pilot.name == "some updated name"
diff --git a/test/acm_uc_demo_web/live/airplane_live_test.exs b/test/acm_uc_demo_web/live/airplane_live_test.exs
index 37e4893..223dbed 100644
--- a/test/acm_uc_demo_web/live/airplane_live_test.exs
+++ b/test/acm_uc_demo_web/live/airplane_live_test.exs
@@ -4,8 +4,18 @@ defmodule AcmUcDemoWeb.AirplaneLiveTest do
import Phoenix.LiveViewTest
import AcmUcDemo.AirplanesFixtures
- @create_attrs %{make: "some make", model: "some model", tail_number: "some tail_number", initial_hours: "120.5"}
- @update_attrs %{make: "some updated make", model: "some updated model", tail_number: "some updated tail_number", initial_hours: "456.7"}
+ @create_attrs %{
+ make: "some make",
+ model: "some model",
+ tail_number: "some tail_number",
+ initial_hours: "120.5"
+ }
+ @update_attrs %{
+ make: "some updated make",
+ model: "some updated model",
+ tail_number: "some updated tail_number",
+ initial_hours: "456.7"
+ }
@invalid_attrs %{make: nil, model: nil, tail_number: nil, initial_hours: nil}
defp create_airplane(_) do
airplane = airplane_fixture()
diff --git a/test/acm_uc_demo_web/live/pilot_live_test.exs b/test/acm_uc_demo_web/live/pilot_live_test.exs
index 6d93621..723301b 100644
--- a/test/acm_uc_demo_web/live/pilot_live_test.exs
+++ b/test/acm_uc_demo_web/live/pilot_live_test.exs
@@ -5,7 +5,10 @@ defmodule AcmUcDemoWeb.PilotLiveTest do
import AcmUcDemo.PilotsFixtures
@create_attrs %{name: "some name", certificate_number: "some certificate_number"}
- @update_attrs %{name: "some updated name", certificate_number: "some updated certificate_number"}
+ @update_attrs %{
+ name: "some updated name",
+ certificate_number: "some updated certificate_number"
+ }
@invalid_attrs %{name: nil, certificate_number: nil}
defp create_pilot(_) do
pilot = pilot_fixture()