Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add shuttle route definition to shuttles #1022

Merged
merged 14 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 13 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
36 changes: 28 additions & 8 deletions lib/arrow/shuttles.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Arrow.Shuttles do
alias Arrow.Repo
alias ArrowWeb.ErrorHelpers

alias Arrow.Gtfs.Route, as: GtfsRoute
alias Arrow.Shuttles.KML
alias Arrow.Shuttles.Shape
alias Arrow.Shuttles.ShapesUpload
Expand Down Expand Up @@ -244,7 +245,7 @@ defmodule Arrow.Shuttles do

"""
def list_shuttles do
Repo.all(Shuttle)
Repo.all(Shuttle) |> Repo.preload(routes: [:shape])
end

@doc """
Expand All @@ -261,7 +262,9 @@ defmodule Arrow.Shuttles do
** (Ecto.NoResultsError)

"""
def get_shuttle!(id), do: Repo.get!(Shuttle, id)
def get_shuttle!(id) do
Repo.get!(Shuttle, id) |> Repo.preload(routes: [:shape])
end

@doc """
Creates a shuttle.
Expand All @@ -276,9 +279,15 @@ defmodule Arrow.Shuttles do

"""
def create_shuttle(attrs \\ %{}) do
%Shuttle{}
|> Shuttle.changeset(attrs)
|> Repo.insert()
created_shuttle =
%Shuttle{}
|> Shuttle.changeset(attrs)
|> Repo.insert()

case created_shuttle do
{:ok, shuttle} -> {:ok, Repo.preload(shuttle, routes: [:shape])}
err -> err
end
end

@doc """
Expand All @@ -294,9 +303,15 @@ defmodule Arrow.Shuttles do

"""
def update_shuttle(%Shuttle{} = shuttle, attrs) do
shuttle
|> Shuttle.changeset(attrs)
|> Repo.update()
updated_shuttle =
shuttle
|> Shuttle.changeset(attrs)
|> Repo.update()

case updated_shuttle do
{:ok, shuttle} -> {:ok, Repo.preload(shuttle, routes: [:shape])}
err -> err
end
end

@doc """
Expand All @@ -311,4 +326,9 @@ defmodule Arrow.Shuttles do
def change_shuttle(%Shuttle{} = shuttle, attrs \\ %{}) do
Shuttle.changeset(shuttle, attrs)
end

def list_disruptable_routes do
query = from(r in GtfsRoute, where: r.type in [:light_rail, :heavy_rail])
Repo.all(query)
end
end
7 changes: 4 additions & 3 deletions lib/arrow/shuttles/route.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ defmodule Arrow.Shuttles.Route do
field :direction_id, Ecto.Enum, values: [:"0", :"1"]
field :direction_desc, :string
field :waypoint, :string
field :shuttle_id, :id
field :shape_id, :id
belongs_to :shuttle, Arrow.Shuttles.Shuttle
belongs_to :shape, Arrow.Shuttles.Shape

timestamps(type: :utc_datetime)
end

@doc false
def changeset(route, attrs) do
route
|> cast(attrs, [:direction_id, :direction_desc, :destination, :waypoint, :suffix])
|> cast(attrs, [:direction_id, :direction_desc, :destination, :waypoint, :suffix, :shape_id])
|> foreign_key_constraint(:shape_id)
|> validate_required([:direction_id, :direction_desc, :destination])
end
end
2 changes: 1 addition & 1 deletion lib/arrow/shuttles/route_stop.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Arrow.Shuttles.RouteStop do
field :stop_id, :string
field :stop_sequence, :integer
field :time_to_next_stop, :decimal
field :shuttle_route_id, :id
belongs_to :route, Arrow.Shuttles.Route

timestamps(type: :utc_datetime)
end
Expand Down
29 changes: 29 additions & 0 deletions lib/arrow/shuttles/shuttle.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Arrow.Shuttles.Shuttle do
field :status, Ecto.Enum, values: [:draft, :active, :inactive]
field :shuttle_name, :string
field :disrupted_route_id, :string
has_many :routes, Arrow.Shuttles.Route, preload_order: [asc: :direction_id]

timestamps(type: :utc_datetime)
end
Expand All @@ -15,7 +16,35 @@ defmodule Arrow.Shuttles.Shuttle do
def changeset(shuttle, attrs) do
shuttle
|> cast(attrs, [:shuttle_name, :disrupted_route_id, :status])
|> cast_assoc(:routes, with: &Arrow.Shuttles.Route.changeset/2)
|> validate_required([:shuttle_name, :status])
|> validate_required_for(:status)
|> foreign_key_constraint(:disrupted_route_id)
|> unique_constraint(:shuttle_name)
end

def validate_required_for(changeset, :status) do
# Placeholder validation until form is complete
status = get_field(changeset, :status)
# Set error on status field for now
fields = [:status]

case status do
:active ->
message = "can't be set to active when required fields are missing"

%{
changeset
| errors:
Enum.map(
fields,
&{&1, {message, [validation: :required]}}
),
valid?: false
}

_ ->
changeset
end
end
end
2 changes: 1 addition & 1 deletion lib/arrow_web/components/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ defmodule ArrowWeb.CoreComponents do

def label(assigns) do
~H"""
<label for={@for}>
<label for={@for} class="col-form-label">
<%= render_slot(@inner_block) %>
</label>
"""
Expand Down
44 changes: 39 additions & 5 deletions lib/arrow_web/controllers/shuttle_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,19 @@ defmodule ArrowWeb.ShuttleController do
end

def new(conn, _params) do
changeset = Shuttles.change_shuttle(%Shuttle{})
render(conn, :new, changeset: changeset)
changeset =
Shuttles.change_shuttle(%Shuttle{
routes: [%Shuttles.Route{direction_id: :"0"}, %Shuttles.Route{direction_id: :"1"}]
})

gtfs_disruptable_routes = Shuttles.list_disruptable_routes()
shapes = Shuttles.list_shapes()

render(conn, :new,
changeset: changeset,
gtfs_disruptable_routes: gtfs_disruptable_routes,
shapes: shapes
)
end

def create(conn, %{"shuttle" => shuttle_params}) do
Expand All @@ -22,7 +33,14 @@ defmodule ArrowWeb.ShuttleController do
|> redirect(to: ~p"/shuttles/#{shuttle}")

{:error, %Ecto.Changeset{} = changeset} ->
render(conn, :new, changeset: changeset)
gtfs_disruptable_routes = Shuttles.list_disruptable_routes()
shapes = Shuttles.list_shapes()

render(conn, :new,
changeset: changeset,
gtfs_disruptable_routes: gtfs_disruptable_routes,
shapes: shapes
)
end
end

Expand All @@ -34,7 +52,15 @@ defmodule ArrowWeb.ShuttleController do
def edit(conn, %{"id" => id}) do
shuttle = Shuttles.get_shuttle!(id)
changeset = Shuttles.change_shuttle(shuttle)
render(conn, :edit, shuttle: shuttle, changeset: changeset)
gtfs_disruptable_routes = Shuttles.list_disruptable_routes()
shapes = Shuttles.list_shapes()

render(conn, :edit,
shuttle: shuttle,
changeset: changeset,
gtfs_disruptable_routes: gtfs_disruptable_routes,
shapes: shapes
)
end

def update(conn, %{"id" => id, "shuttle" => shuttle_params}) do
Expand All @@ -47,7 +73,15 @@ defmodule ArrowWeb.ShuttleController do
|> redirect(to: ~p"/shuttles/#{shuttle}")

{:error, %Ecto.Changeset{} = changeset} ->
render(conn, :edit, shuttle: shuttle, changeset: changeset)
gtfs_disruptable_routes = Shuttles.list_disruptable_routes()
shapes = Shuttles.list_shapes()

render(conn, :edit,
shuttle: shuttle,
changeset: changeset,
gtfs_disruptable_routes: gtfs_disruptable_routes,
shapes: shapes
)
end
end
end
2 changes: 2 additions & 0 deletions lib/arrow_web/controllers/shuttle_html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ defmodule ArrowWeb.ShuttleView do
"""
attr :changeset, Ecto.Changeset, required: true
attr :action, :string, required: true
attr :gtfs_disruptable_routes, :list, required: true
attr :shapes, :list, required: true

def shuttle_form(assigns)
end
9 changes: 7 additions & 2 deletions lib/arrow_web/controllers/shuttle_html/edit.html.heex
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<.header>
Edit Shuttle <%= @shuttle.id %>
edit shuttle <%= @shuttle.id %>
</.header>

<.shuttle_form changeset={@changeset} action={~p"/shuttles/#{@shuttle}"} />
<.shuttle_form
changeset={@changeset}
action={~p"/shuttles/#{@shuttle}"}
gtfs_disruptable_routes={@gtfs_disruptable_routes}
shapes={@shapes}
/>

<.back navigate={~p"/shuttles"}>Back to shuttles</.back>
3 changes: 2 additions & 1 deletion lib/arrow_web/controllers/shuttle_html/index.html.heex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<.header>
Listing Shuttles
shuttles
<:actions>
<.link href={~p"/shuttles/new"}>
<.button>New Shuttle</.button>
Expand All @@ -9,6 +9,7 @@

<.table id="shuttles" rows={@shuttles} row_click={&JS.navigate(~p"/shuttles/#{&1}")}>
<:col :let={shuttle} label="Shuttle name"><%= shuttle.shuttle_name %></:col>
<:col :let={shuttle} label="Disrupted route"><%= shuttle.disrupted_route_id %></:col>
<:col :let={shuttle} label="Status"><%= shuttle.status %></:col>
<:action :let={shuttle}>
<div class="sr-only">
Expand Down
9 changes: 7 additions & 2 deletions lib/arrow_web/controllers/shuttle_html/new.html.heex
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<.header>
New Shuttle
new shuttle
</.header>

<.shuttle_form changeset={@changeset} action={~p"/shuttles"} />
<.shuttle_form
changeset={@changeset}
action={~p"/shuttles"}
gtfs_disruptable_routes={@gtfs_disruptable_routes}
shapes={@shapes}
/>

<.back navigate={~p"/shuttles"}>Back to shuttles</.back>
3 changes: 2 additions & 1 deletion lib/arrow_web/controllers/shuttle_html/show.html.heex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<.header>
Shuttle <%= @shuttle.id %>
shuttle <%= @shuttle.id %>
<:actions>
<.link href={~p"/shuttles/#{@shuttle}/edit"}>
<.button>Edit shuttle</.button>
Expand All @@ -9,6 +9,7 @@

<.list>
<:item title="Shuttle name"><%= @shuttle.shuttle_name %></:item>
<:item title="Disrupted route"><%= @shuttle.disrupted_route_id %></:item>
<:item title="Status"><%= @shuttle.status %></:item>
</.list>

Expand Down
80 changes: 72 additions & 8 deletions lib/arrow_web/controllers/shuttle_html/shuttle_form.html.heex
Original file line number Diff line number Diff line change
@@ -1,15 +1,79 @@
<.simple_form :let={f} for={@changeset} action={@action}>
<div class="form-group">
<a
target="_blank"
rel="noreferrer noopener"
href="https://www.notion.so/native/mbta-downtown-crossing/Conventions-for-shuttle-bus-information-fc5a788409b24eb088dbfe3a43abf67e?pvs=4&deepLinkOpenNewTab=true#2b8886089b25403991f1ed69144d8fd8"
>
View Shuttle Definition Conventions
</a>
</div>
<.error :if={@changeset.action}>
Oops, something went wrong! Please check the errors below.
</.error>
<.input field={f[:shuttle_name]} type="text" label="Shuttle name" />
<.input
field={f[:status]}
type="select"
label="Status"
prompt="Choose a value"
options={Ecto.Enum.values(Arrow.Shuttles.Shuttle, :status)}
/>
<div class="row">
<div class="col">
<.input field={f[:shuttle_name]} type="text" label="Shuttle Name" />
</div>
<div class="col">
<.input
field={f[:disrupted_route_id]}
type="select"
label="Disrupted Route"
prompt="Choose a route"
options={Enum.map(@gtfs_disruptable_routes, &{&1.long_name, &1.id})}
/>
</div>
<div class="col">
<.input
field={f[:status]}
type="select"
label="Status"
prompt="Choose a value"
options={Ecto.Enum.values(Arrow.Shuttles.Shuttle, :status)}
/>
</div>
</div>
<hr />
<h2>define route</h2>
<.inputs_for :let={f_route} field={f[:routes]}>
<h4>direction <%= input_value(f_route, :direction_id) %></h4>
<div class="row">
<div class="hidden">
<.input field={f_route[:direction_id]} type="text" label="Direction id" />
</div>
<div class="col">
<.input field={f_route[:direction_desc]} type="text" label="Direction desc" />
</div>
<div class="col offset-md-1">
<.input
field={f_route[:shape_id]}
type="select"
label="Shape"
prompt="Choose a shape"
options={Enum.map(@shapes, &{&1.name, &1.id})}
/>
</div>
</div>
<div class="row">
<div class="col">
<.input field={f_route[:destination]} type="text" label="Destination" />
</div>
<div class="col-1 align-self-end italic">
<div class="form-group">
via
</div>
</div>
<div class="col">
<.input field={f_route[:waypoint]} type="text" label="Waypoint" />
</div>
</div>
<div class="row">
<div class="col">
<.input field={f_route[:suffix]} type="text" label="Suffix" />
</div>
</div>
</.inputs_for>
<:actions>
<.button>Save Shuttle</.button>
</:actions>
Expand Down
10 changes: 10 additions & 0 deletions priv/repo/migrations/20241018202407_add_fk_gtfs_route_shuttles.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
defmodule Arrow.Repo.Migrations.AddFkGtfsRouteShuttles do
use Ecto.Migration

def change do
alter table(:shuttles) do
modify :disrupted_route_id, references(:gtfs_routes, type: :varchar, on_delete: :nothing),
from: :string
end
end
end
Loading
Loading