Skip to content

Commit

Permalink
feat: add scaffolding for shuttle page (#1019)
Browse files Browse the repository at this point in the history
  • Loading branch information
meagharty authored Oct 22, 2024
1 parent 1082f0d commit 7cd3c7d
Show file tree
Hide file tree
Showing 13 changed files with 359 additions and 1 deletion.
80 changes: 80 additions & 0 deletions lib/arrow/shuttles.ex
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,84 @@ defmodule Arrow.Shuttles do
def change_shape(%Shape{} = shape, attrs \\ %{}) do
Shape.changeset(shape, attrs)
end

alias Arrow.Shuttles.Shuttle

@doc """
Returns the list of shuttles.
## Examples
iex> list_shuttles()
[%Shuttle{}, ...]
"""
def list_shuttles do
Repo.all(Shuttle)
end

@doc """
Gets a single shuttle.
Raises `Ecto.NoResultsError` if the Shuttle does not exist.
## Examples
iex> get_shuttle!(123)
%Shuttle{}
iex> get_shuttle!(456)
** (Ecto.NoResultsError)
"""
def get_shuttle!(id), do: Repo.get!(Shuttle, id)

@doc """
Creates a shuttle.
## Examples
iex> create_shuttle(%{field: value})
{:ok, %Shuttle{}}
iex> create_shuttle(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_shuttle(attrs \\ %{}) do
%Shuttle{}
|> Shuttle.changeset(attrs)
|> Repo.insert()
end

@doc """
Updates a shuttle.
## Examples
iex> update_shuttle(shuttle, %{field: new_value})
{:ok, %Shuttle{}}
iex> update_shuttle(shuttle, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_shuttle(%Shuttle{} = shuttle, attrs) do
shuttle
|> Shuttle.changeset(attrs)
|> Repo.update()
end

@doc """
Returns an `%Ecto.Changeset{}` for tracking shuttle changes.
## Examples
iex> change_shuttle(shuttle)
%Ecto.Changeset{data: %Shuttle{}}
"""
def change_shuttle(%Shuttle{} = shuttle, attrs \\ %{}) do
Shuttle.changeset(shuttle, attrs)
end
end
2 changes: 1 addition & 1 deletion lib/arrow/shuttles/shuttle.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule Arrow.Shuttles.Shuttle do
def changeset(shuttle, attrs) do
shuttle
|> cast(attrs, [:shuttle_name, :disrupted_route_id, :status])
|> validate_required([:shuttle_name, :disrupted_route_id, :status])
|> validate_required([:shuttle_name, :status])
|> unique_constraint(:shuttle_name)
end
end
53 changes: 53 additions & 0 deletions lib/arrow_web/controllers/shuttle_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
defmodule ArrowWeb.ShuttleController do
use ArrowWeb, :controller

alias Arrow.Shuttles
alias Arrow.Shuttles.Shuttle

def index(conn, _params) do
shuttles = Shuttles.list_shuttles()
render(conn, :index, shuttles: shuttles)
end

def new(conn, _params) do
changeset = Shuttles.change_shuttle(%Shuttle{})
render(conn, :new, changeset: changeset)
end

def create(conn, %{"shuttle" => shuttle_params}) do
case Shuttles.create_shuttle(shuttle_params) do
{:ok, shuttle} ->
conn
|> put_flash(:info, "Shuttle created successfully.")
|> redirect(to: ~p"/shuttles/#{shuttle}")

{:error, %Ecto.Changeset{} = changeset} ->
render(conn, :new, changeset: changeset)
end
end

def show(conn, %{"id" => id}) do
shuttle = Shuttles.get_shuttle!(id)
render(conn, :show, shuttle: shuttle)
end

def edit(conn, %{"id" => id}) do
shuttle = Shuttles.get_shuttle!(id)
changeset = Shuttles.change_shuttle(shuttle)
render(conn, :edit, shuttle: shuttle, changeset: changeset)
end

def update(conn, %{"id" => id, "shuttle" => shuttle_params}) do
shuttle = Shuttles.get_shuttle!(id)

case Shuttles.update_shuttle(shuttle, shuttle_params) do
{:ok, shuttle} ->
conn
|> put_flash(:info, "Shuttle updated successfully.")
|> redirect(to: ~p"/shuttles/#{shuttle}")

{:error, %Ecto.Changeset{} = changeset} ->
render(conn, :edit, shuttle: shuttle, changeset: changeset)
end
end
end
13 changes: 13 additions & 0 deletions lib/arrow_web/controllers/shuttle_html.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule ArrowWeb.ShuttleView do
use ArrowWeb, :html

embed_templates "shuttle_html/*"

@doc """
Renders a shuttle form.
"""
attr :changeset, Ecto.Changeset, required: true
attr :action, :string, required: true

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

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

<.back navigate={~p"/shuttles"}>Back to shuttles</.back>
19 changes: 19 additions & 0 deletions lib/arrow_web/controllers/shuttle_html/index.html.heex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<.header>
Listing Shuttles
<:actions>
<.link href={~p"/shuttles/new"}>
<.button>New Shuttle</.button>
</.link>
</:actions>
</.header>

<.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="Status"><%= shuttle.status %></:col>
<:action :let={shuttle}>
<div class="sr-only">
<.link navigate={~p"/shuttles/#{shuttle}"}>Show</.link>
</div>
<.link navigate={~p"/shuttles/#{shuttle}/edit"}>Edit</.link>
</:action>
</.table>
7 changes: 7 additions & 0 deletions lib/arrow_web/controllers/shuttle_html/new.html.heex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<.header>
New Shuttle
</.header>

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

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

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

<.back navigate={~p"/shuttles"}>Back to shuttles</.back>
16 changes: 16 additions & 0 deletions lib/arrow_web/controllers/shuttle_html/shuttle_form.html.heex
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<.simple_form :let={f} for={@changeset} action={@action}>
<.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)}
/>
<:actions>
<.button>Save Shuttle</.button>
</:actions>
</.simple_form>
1 change: 1 addition & 0 deletions lib/arrow_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ defmodule ArrowWeb.Router do
get("/shapes_upload", ShapeController, :new)
post("/shapes_upload", ShapeController, :create)
get("/shapes/:id/download", ShapeController, :download)
resources("/shuttles", ShuttleController, except: [:delete])

live_dashboard "/dashboard", ecto_repos: [Arrow.Repo], metrics: ArrowWeb.Telemetry
end
Expand Down
52 changes: 52 additions & 0 deletions test/arrow/shuttle_test.exs → test/arrow/shuttles_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,56 @@ defmodule Arrow.ShuttlesTest do
assert %Ecto.Changeset{} = Shuttles.change_shape(shape)
end
end

alias Arrow.Shuttles

describe "shuttles" do
alias Arrow.Shuttles.Shuttle

import Arrow.ShuttlesFixtures

@invalid_attrs %{status: nil, shuttle_name: nil}

test "list_shuttles/0 returns all shuttles" do
shuttle = shuttle_fixture()
assert Shuttles.list_shuttles() == [shuttle]
end

test "get_shuttle!/1 returns the shuttle with given id" do
shuttle = shuttle_fixture()
assert Shuttles.get_shuttle!(shuttle.id) == shuttle
end

test "create_shuttle/1 with valid data creates a shuttle" do
valid_attrs = %{status: :draft, shuttle_name: "some shuttle_name"}

assert {:ok, %Shuttle{} = shuttle} = Shuttles.create_shuttle(valid_attrs)
assert shuttle.status == :draft
assert shuttle.shuttle_name == "some shuttle_name"
end

test "create_shuttle/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Shuttles.create_shuttle(@invalid_attrs)
end

test "update_shuttle/2 with valid data updates the shuttle" do
shuttle = shuttle_fixture()
update_attrs = %{status: :active, shuttle_name: "some updated shuttle_name"}

assert {:ok, %Shuttle{} = shuttle} = Shuttles.update_shuttle(shuttle, update_attrs)
assert shuttle.status == :active
assert shuttle.shuttle_name == "some updated shuttle_name"
end

test "update_shuttle/2 with invalid data returns error changeset" do
shuttle = shuttle_fixture()
assert {:error, %Ecto.Changeset{}} = Shuttles.update_shuttle(shuttle, @invalid_attrs)
assert shuttle == Shuttles.get_shuttle!(shuttle.id)
end

test "change_shuttle/1 returns a shuttle changeset" do
shuttle = shuttle_fixture()
assert %Ecto.Changeset{} = Shuttles.change_shuttle(shuttle)
end
end
end
75 changes: 75 additions & 0 deletions test/arrow_web/controllers/shuttle_controller_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
defmodule ArrowWeb.ShuttleControllerTest do
use ArrowWeb.ConnCase

import Arrow.ShuttlesFixtures

@create_attrs %{status: :draft, shuttle_name: "some shuttle_name"}
@update_attrs %{status: :active, shuttle_name: "some updated shuttle_name"}
@invalid_attrs %{status: nil, shuttle_name: nil}

describe "index" do
@tag :authenticated
test "lists all shuttles", %{conn: conn} do
conn = get(conn, ~p"/shuttles")
assert html_response(conn, 200) =~ "Listing Shuttles"
end
end

describe "new shuttle" do
@tag :authenticated_admin
test "renders form", %{conn: conn} do
conn = get(conn, ~p"/shuttles/new")
assert html_response(conn, 200) =~ "New Shuttle"
end
end

describe "create shuttle" do
@tag :authenticated_admin
test "redirects to show when data is valid", %{conn: conn} do
conn = post(conn, ~p"/shuttles", shuttle: @create_attrs)

assert %{id: id} = redirected_params(conn)
assert redirected_to(conn) == ~p"/shuttles/#{id}"
end

@tag :authenticated_admin
test "renders errors when data is invalid", %{conn: conn} do
conn = post(conn, ~p"/shuttles", shuttle: @invalid_attrs)
assert html_response(conn, 200) =~ "New Shuttle"
end
end

describe "edit shuttle" do
setup [:create_shuttle]

@tag :authenticated_admin
test "renders form for editing chosen shuttle", %{conn: conn, shuttle: shuttle} do
conn = get(conn, ~p"/shuttles/#{shuttle}/edit")
assert html_response(conn, 200) =~ "Edit Shuttle"
end
end

describe "update shuttle" do
setup [:create_shuttle]

@tag :authenticated_admin
test "redirects when data is valid", %{conn: conn, shuttle: shuttle} do
redirected = put(conn, ~p"/shuttles/#{shuttle}", shuttle: @update_attrs)
assert redirected_to(redirected) == ~p"/shuttles/#{shuttle}"

conn = get(conn, ~p"/shuttles/#{shuttle}")
assert html_response(conn, 200) =~ "some updated shuttle_name"
end

@tag :authenticated_admin
test "renders errors when data is invalid", %{conn: conn, shuttle: shuttle} do
conn = put(conn, ~p"/shuttles/#{shuttle}", shuttle: @invalid_attrs)
assert html_response(conn, 200) =~ "Edit Shuttle"
end
end

defp create_shuttle(_) do
shuttle = shuttle_fixture()
%{shuttle: shuttle}
end
end
20 changes: 20 additions & 0 deletions test/support/fixtures/shuttles_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,24 @@ defmodule Arrow.ShuttlesFixtures do

shape
end

@doc """
Generate a unique shuttle shuttle_name.
"""
def unique_shuttle_shuttle_name, do: "some shuttle_name#{System.unique_integer([:positive])}"

@doc """
Generate a shuttle.
"""
def shuttle_fixture(attrs \\ %{}) do
{:ok, shuttle} =
attrs
|> Enum.into(%{
shuttle_name: unique_shuttle_shuttle_name(),
status: :draft
})
|> Arrow.Shuttles.create_shuttle()

shuttle
end
end

0 comments on commit 7cd3c7d

Please sign in to comment.