Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/acm_uc_demo/airplanes/airplane.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions lib/acm_uc_demo/flights.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 2 additions & 2 deletions lib/acm_uc_demo/flights/flight.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/acm_uc_demo/pilots/pilot.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 30 additions & 1 deletion lib/acm_uc_demo_web/live/airplane_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,15 +27,43 @@ defmodule AcmUcDemoWeb.AirplaneLive.Show do
<:item title="Tail number">{@airplane.tail_number}</:item>
<:item title="Initial hours">{@airplane.initial_hours}</:item>
</.list>

<div class="mt-11">
<.header>
Flights
<:subtitle>Flight history for this airplane</:subtitle>
</.header>

<div class="mt-6">
<%= if @flights == [] do %>
<div class="text-center py-12 text-gray-500">
No flights recorded for this airplane yet.
</div>
<% else %>
<.table id="flights-table" rows={@flights}>
<:col :let={flight} label="Pilot">{flight.pilot.name}</:col>
<:col :let={flight} label="Hobbs Reading">{flight.hobbs_reading}</:col>
<:col :let={flight} label="Notes">{flight.notes}</:col>
<:col :let={flight} label="Date">
{Calendar.strftime(flight.inserted_at, "%Y-%m-%d %I:%M %p")}
</:col>
</.table>
<% end %>
</div>
</div>
</Layouts.app>
"""
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
15 changes: 13 additions & 2 deletions test/acm_uc_demo/airplanes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
6 changes: 5 additions & 1 deletion test/acm_uc_demo/pilots_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 12 additions & 2 deletions test/acm_uc_demo_web/live/airplane_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion test/acm_uc_demo_web/live/pilot_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down