Skip to content

Commit

Permalink
Merge branch 'master' into jz-import-gtfs-endpoint--ecto-schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
jzimbel-mbta committed Oct 15, 2024
2 parents f847d4a + 02a3885 commit d8d4606
Show file tree
Hide file tree
Showing 29 changed files with 593 additions and 70 deletions.
12 changes: 6 additions & 6 deletions lib/arrow/shuttle.ex → lib/arrow/shuttles.ex
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
defmodule Arrow.Shuttle do
defmodule Arrow.Shuttles do
@moduledoc """
The Shuttle context.
The Shuttles context.
"""

import Ecto.Query, warn: false

alias Arrow.Repo
alias ArrowWeb.ErrorHelpers

alias Arrow.Shuttle.KML
alias Arrow.Shuttle.Shape
alias Arrow.Shuttle.ShapesUpload
alias Arrow.Shuttle.ShapeUpload
alias Arrow.Shuttles.KML
alias Arrow.Shuttles.Shape
alias Arrow.Shuttles.ShapesUpload
alias Arrow.Shuttles.ShapeUpload

@doc """
Returns the list of shapes.
Expand Down
4 changes: 2 additions & 2 deletions lib/arrow/shuttle/kml.ex → lib/arrow/shuttles/kml.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Arrow.Shuttle.KML do
defmodule Arrow.Shuttles.KML do
@moduledoc """
A struct for the full KML representation of a shape to be used with Saxy.Builder
"""
Expand All @@ -13,7 +13,7 @@ defmodule Arrow.Shuttle.KML do
element(
"Folder",
[],
Saxy.Builder.build(%Arrow.Shuttle.ShapeKML{name: name, coordinates: coordinates})
Saxy.Builder.build(%Arrow.Shuttles.ShapeKML{name: name, coordinates: coordinates})
)
end
end
24 changes: 24 additions & 0 deletions lib/arrow/shuttles/route.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Arrow.Shuttles.Route do
@moduledoc "schema for a shuttle route for the db"
use Ecto.Schema
import Ecto.Changeset

schema "shuttle_routes" do
field :suffix, :string
field :destination, :string
field :direction_id, Ecto.Enum, values: [:"0", :"1"]
field :direction_desc, :string
field :waypoint, :string
field :shuttle_id, :id
field :shape_id, :id

timestamps(type: :utc_datetime)
end

@doc false
def changeset(route, attrs) do
route
|> cast(attrs, [:direction_id, :direction_desc, :destination, :waypoint, :suffix])
|> validate_required([:direction_id, :direction_desc, :destination])
end
end
22 changes: 22 additions & 0 deletions lib/arrow/shuttles/route_stop.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
defmodule Arrow.Shuttles.RouteStop do
@moduledoc "schema for a shuttle route stop for the db"
use Ecto.Schema
import Ecto.Changeset

schema "shuttle_route_stops" do
field :direction_id, Ecto.Enum, values: [:"0", :"1"]
field :stop_id, :string
field :stop_sequence, :integer
field :time_to_next_stop, :decimal
field :shuttle_route_id, :id

timestamps(type: :utc_datetime)
end

@doc false
def changeset(route_stop, attrs) do
route_stop
|> cast(attrs, [:direction_id, :stop_id, :stop_sequence, :time_to_next_stop])
|> validate_required([:direction_id, :stop_id, :stop_sequence, :time_to_next_stop])
end
end
2 changes: 1 addition & 1 deletion lib/arrow/shuttle/shape.ex → lib/arrow/shuttles/shape.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Arrow.Shuttle.Shape do
defmodule Arrow.Shuttles.Shape do
@moduledoc "schema for shuttle shapes for the db"
use Ecto.Schema
import Ecto.Changeset
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
defmodule Arrow.Shuttle.ShapeKML do
defmodule Arrow.Shuttles.ShapeKML do
@moduledoc """
A struct for a shape representation to be used with Saxy.Builder
"""
defstruct [:name, :coordinates]
end

defimpl Saxy.Builder, for: Arrow.Shuttle.ShapeKML do
defimpl Saxy.Builder, for: Arrow.Shuttles.ShapeKML do
import Saxy.XML

def build(%{name: name, coordinates: coordinates}) do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Arrow.Shuttle.ShapeUpload do
defmodule Arrow.Shuttles.ShapeUpload do
@moduledoc "schema for shuttle shapes as an embedded schema"
use Ecto.Schema
import Ecto.Changeset
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
defmodule Arrow.Shuttle.ShapesUpload do
defmodule Arrow.Shuttles.ShapesUpload do
@moduledoc "schema for shapes upload"
use Ecto.Schema
import Ecto.Changeset

@type t :: %__MODULE__{
filename: String.t(),
shapes: list(Arrow.Shuttle.ShapeUpload.t())
shapes: list(Arrow.Shuttles.ShapeUpload.t())
}

embedded_schema do
field :filename, :string
embeds_many :shapes, Arrow.Shuttle.ShapeUpload
embeds_many :shapes, Arrow.Shuttles.ShapeUpload
end

@doc false
Expand Down Expand Up @@ -66,7 +66,7 @@ defmodule Arrow.Shuttle.ShapesUpload do
@doc """
Parses one or many Shapes from a map of the KML/XML
"""
@spec shapes_from_kml(map) :: {:ok, list(Arrow.Shuttle.ShapeUpload.t())} | {:error, any}
@spec shapes_from_kml(map) :: {:ok, list(Arrow.Shuttles.ShapeUpload.t())} | {:error, any}
def shapes_from_kml(saxy_shapes) do
placemarks = saxy_shapes["kml"]["Folder"]["Placemark"]

Expand Down
21 changes: 21 additions & 0 deletions lib/arrow/shuttles/shuttle.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule Arrow.Shuttles.Shuttle do
@moduledoc "schema for a shuttle for the db"
use Ecto.Schema
import Ecto.Changeset

schema "shuttles" do
field :status, Ecto.Enum, values: [:draft, :active, :inactive]
field :shuttle_name, :string
field :disrupted_route_id, :string

timestamps(type: :utc_datetime)
end

@doc false
def changeset(shuttle, attrs) do
shuttle
|> cast(attrs, [:shuttle_name, :disrupted_route_id, :status])
|> validate_required([:shuttle_name, :disrupted_route_id, :status])
|> unique_constraint(:shuttle_name)
end
end
2 changes: 1 addition & 1 deletion lib/arrow/shuttle/stop.ex → lib/arrow/shuttles/stop.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Arrow.Shuttle.Stop do
defmodule Arrow.Shuttles.Stop do
@moduledoc false

use Ecto.Schema
Expand Down
2 changes: 1 addition & 1 deletion lib/arrow/stops.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Arrow.Stops do
import Ecto.Query, warn: false
alias Arrow.Repo

alias Arrow.Shuttle.Stop
alias Arrow.Shuttles.Stop

@doc """
Returns the list of stops.
Expand Down
1 change: 1 addition & 0 deletions lib/arrow_web/components/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ defmodule ArrowWeb.CoreComponents do
name={@name}
class={[
"mt-2 block w-full rounded-md border border-gray-300 bg-white shadow-sm focus:border-zinc-400 focus:ring-0 sm:text-sm",
@errors != [] && "is-invalid",
@class
]}
multiple={@multiple}
Expand Down
18 changes: 9 additions & 9 deletions lib/arrow_web/controllers/shape_controller.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
defmodule ArrowWeb.ShapeController do
require Logger
alias Arrow.Shuttle.ShapesUpload
alias Arrow.Shuttles.ShapesUpload
alias ArrowWeb.ErrorHelpers
alias Ecto.Changeset
use ArrowWeb, :controller

alias Arrow.Shuttle
alias Arrow.Shuttles
alias ArrowWeb.Plug.Authorize

plug(Authorize, :view_disruption when action in [:index, :show, :download])
Expand All @@ -14,7 +14,7 @@ defmodule ArrowWeb.ShapeController do
plug(Authorize, :delete_disruption when action in [:delete])

def index(conn, _params) do
shapes = Shuttle.list_shapes()
shapes = Shuttles.list_shapes()
render(conn, :index, shapes: shapes)
end

Expand Down Expand Up @@ -68,7 +68,7 @@ defmodule ArrowWeb.ShapeController do
|> Enum.filter(fn shape -> shape["save"] == "true" end)
|> Enum.map(fn shape -> %{name: shape["name"], coordinates: shape["coordinates"]} end)

case Shuttle.create_shapes(saved_shapes) do
case Shuttles.create_shapes(saved_shapes) do
{:ok, []} ->
conn
|> put_flash(
Expand Down Expand Up @@ -100,14 +100,14 @@ defmodule ArrowWeb.ShapeController do
end

def show(conn, %{"id" => id}) do
shape = Shuttle.get_shape!(id)
shape_upload = Shuttle.get_shapes_upload(shape)
shape = Shuttles.get_shape!(id)
shape_upload = Shuttles.get_shapes_upload(shape)
render(conn, :show, shape: shape, shape_upload: shape_upload)
end

def download(conn, %{"id" => id}) do
enabled? = Application.get_env(:arrow, :shape_storage_enabled?)
shape = Shuttle.get_shape!(id)
shape = Shuttles.get_shape!(id)
basic_url = "https://#{shape.bucket}.s3.amazonaws.com/#{shape.path}"

{:ok, url} =
Expand All @@ -124,8 +124,8 @@ defmodule ArrowWeb.ShapeController do
end

def delete(conn, %{"id" => id}) do
shape = Shuttle.get_shape!(id)
{:ok, _shape} = Shuttle.delete_shape(shape)
shape = Shuttles.get_shape!(id)
{:ok, _shape} = Shuttles.delete_shape(shape)

conn
|> put_flash(:info, "Shape deleted successfully.")
Expand Down
2 changes: 1 addition & 1 deletion lib/arrow_web/controllers/shape_html.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule ArrowWeb.ShapeView do
use ArrowWeb, :html
alias Arrow.Shuttle.ShapesUpload
alias Arrow.Shuttles.ShapesUpload

embed_templates "shape_html/*"

Expand Down
2 changes: 1 addition & 1 deletion lib/arrow_web/live/stop_live/stop_live.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule ArrowWeb.StopViewLive do
use ArrowWeb, :live_view

alias Arrow.Shuttle.Stop
alias Arrow.Shuttles.Stop
alias Arrow.Stops
embed_templates "stop_live/*"

Expand Down
16 changes: 16 additions & 0 deletions priv/repo/migrations/20241010164333_create_shuttles.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule Arrow.Repo.Migrations.CreateShuttles do
use Ecto.Migration

def change do
create table(:shuttles) do
add :shuttle_name, :string
add :disrupted_route_id, :string
# add :disrupted_route_id, references(:gtfs_routes, type: :varchar, on_delete: :nothing)
add :status, :string

timestamps(type: :timestamptz)
end

create unique_index(:shuttles, [:shuttle_name])
end
end
20 changes: 20 additions & 0 deletions priv/repo/migrations/20241010164455_create_shuttle_routes.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Arrow.Repo.Migrations.CreateShuttleRoutes do
use Ecto.Migration

def change do
create table(:shuttle_routes) do
add :direction_id, :string
add :direction_desc, :string
add :destination, :string
add :waypoint, :string
add :suffix, :string
add :shuttle_id, references(:shuttles, on_delete: :nothing)
add :shape_id, references(:shapes, on_delete: :nothing)

timestamps(type: :timestamptz)
end

create index(:shuttle_routes, [:shuttle_id])
create index(:shuttle_routes, [:shape_id])
end
end
17 changes: 17 additions & 0 deletions priv/repo/migrations/20241010164555_create_shuttle_route_stops.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Arrow.Repo.Migrations.CreateShuttleRouteStops do
use Ecto.Migration

def change do
create table(:shuttle_route_stops) do
add :direction_id, :string
add :stop_id, :string
add :stop_sequence, :integer
add :time_to_next_stop, :decimal
add :shuttle_route_id, references(:shuttle_routes, on_delete: :nothing)

timestamps(type: :timestamptz)
end

create index(:shuttle_route_stops, [:shuttle_route_id])
end
end
Loading

0 comments on commit d8d4606

Please sign in to comment.