diff --git a/lib/arrow/shuttles.ex b/lib/arrow/shuttles.ex index a4b6128e9..eb3bac7b3 100644 --- a/lib/arrow/shuttles.ex +++ b/lib/arrow/shuttles.ex @@ -231,4 +231,88 @@ 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) + + @spec create_shuttle( + :invalid + | %{optional(:__struct__) => none(), optional(atom() | binary()) => any()} + ) :: any() + @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 diff --git a/lib/arrow/shuttles/shuttle.ex b/lib/arrow/shuttles/shuttle.ex index d9746d163..7189071b6 100644 --- a/lib/arrow/shuttles/shuttle.ex +++ b/lib/arrow/shuttles/shuttle.ex @@ -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 diff --git a/lib/arrow_web/controllers/shuttle_controller.ex b/lib/arrow_web/controllers/shuttle_controller.ex index 034ec0cab..0fbe37a5e 100644 --- a/lib/arrow_web/controllers/shuttle_controller.ex +++ b/lib/arrow_web/controllers/shuttle_controller.ex @@ -1,28 +1,53 @@ defmodule ArrowWeb.ShuttleController do use ArrowWeb, :controller + + alias Arrow.Shuttles alias Arrow.Shuttles.Shuttle def index(conn, _params) do - render(conn, :index, shuttles: []) + shuttles = Shuttles.list_shuttles() + render(conn, :index, shuttles: shuttles) end def new(conn, _params) do - render(conn, :new, changeset: nil) + changeset = Shuttles.change_shuttle(%Shuttle{}) + render(conn, :new, changeset: changeset) end - def create(conn, %{"shuttle" => _shuttle_params}) do - render(conn, :new, changeset: nil) + 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 - render(conn, :show, shuttle: nil) + def show(conn, %{"id" => id}) do + shuttle = Shuttles.get_shuttle!(id) + render(conn, :show, shuttle: shuttle) end def edit(conn, %{"id" => id}) do - render(conn, :edit, shuttle: %Shuttle{id: id}, changeset: nil) + 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 - render(conn, :edit, shuttle: nil, changeset: nil) + 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 diff --git a/lib/arrow_web/controllers/shuttle_html/edit.html.heex b/lib/arrow_web/controllers/shuttle_html/edit.html.heex index f6bb8e895..0cc85593e 100644 --- a/lib/arrow_web/controllers/shuttle_html/edit.html.heex +++ b/lib/arrow_web/controllers/shuttle_html/edit.html.heex @@ -1,6 +1,5 @@ <.header> Edit Shuttle <%= @shuttle.id %> - <:subtitle>Use this form to manage shuttle records in your database. <.shuttle_form changeset={@changeset} action={~p"/shuttles/#{@shuttle}"} /> diff --git a/lib/arrow_web/controllers/shuttle_html/index.html.heex b/lib/arrow_web/controllers/shuttle_html/index.html.heex index ea6ed6ba0..5c8b46370 100644 --- a/lib/arrow_web/controllers/shuttle_html/index.html.heex +++ b/lib/arrow_web/controllers/shuttle_html/index.html.heex @@ -16,9 +16,4 @@ <.link navigate={~p"/shuttles/#{shuttle}/edit"}>Edit - <:action :let={shuttle}> - <.link href={~p"/shuttles/#{shuttle}"} method="delete" data-confirm="Are you sure?"> - Delete - - diff --git a/lib/arrow_web/controllers/shuttle_html/new.html.heex b/lib/arrow_web/controllers/shuttle_html/new.html.heex index 93a13d835..071a3b9b8 100644 --- a/lib/arrow_web/controllers/shuttle_html/new.html.heex +++ b/lib/arrow_web/controllers/shuttle_html/new.html.heex @@ -1,6 +1,5 @@ <.header> New Shuttle - <:subtitle>Use this form to manage shuttle records in your database. <.shuttle_form changeset={@changeset} action={~p"/shuttles"} /> diff --git a/lib/arrow_web/controllers/shuttle_html/show.html.heex b/lib/arrow_web/controllers/shuttle_html/show.html.heex index 577b0565b..c28dc2c89 100644 --- a/lib/arrow_web/controllers/shuttle_html/show.html.heex +++ b/lib/arrow_web/controllers/shuttle_html/show.html.heex @@ -1,6 +1,5 @@ <.header> Shuttle <%= @shuttle.id %> - <:subtitle>This is a shuttle record from your database. <:actions> <.link href={~p"/shuttles/#{@shuttle}/edit"}> <.button>Edit shuttle diff --git a/lib/arrow_web/controllers/shuttle_html/shuttle_form.html.heex b/lib/arrow_web/controllers/shuttle_html/shuttle_form.html.heex index 826995e92..69d8b3464 100644 --- a/lib/arrow_web/controllers/shuttle_html/shuttle_form.html.heex +++ b/lib/arrow_web/controllers/shuttle_html/shuttle_form.html.heex @@ -1,18 +1,16 @@ -<%= if @changeset do %> - <.simple_form :let={f} for={@changeset} action={@action}> - <.error :if={@changeset.action}> - Oops, something went wrong! Please check the errors below. - - <.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 - - -<% end %> +<.simple_form :let={f} for={@changeset} action={@action}> + <.error :if={@changeset.action}> + Oops, something went wrong! Please check the errors below. + + <.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 + + diff --git a/test/arrow/shuttle_test.exs b/test/arrow/shuttles_test.exs similarity index 68% rename from test/arrow/shuttle_test.exs rename to test/arrow/shuttles_test.exs index 443efb1fa..21867d30d 100644 --- a/test/arrow/shuttle_test.exs +++ b/test/arrow/shuttles_test.exs @@ -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 diff --git a/test/arrow_web/controllers/shuttle_controller_test.exs b/test/arrow_web/controllers/shuttle_controller_test.exs index 1ffe81604..19fb8ea09 100644 --- a/test/arrow_web/controllers/shuttle_controller_test.exs +++ b/test/arrow_web/controllers/shuttle_controller_test.exs @@ -1,6 +1,11 @@ defmodule ArrowWeb.ShuttleControllerTest do use ArrowWeb.ConnCase - alias Arrow.Shuttles.Shuttle + + 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 @@ -11,24 +16,60 @@ defmodule ArrowWeb.ShuttleControllerTest do end describe "new shuttle" do - @tag :authenticated + @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 - test "renders successfully", %{conn: conn, shuttle: shuttle} do + @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{id: 1, shuttle_name: "test", status: :draft}} + shuttle = shuttle_fixture() + %{shuttle: shuttle} end end diff --git a/test/support/fixtures/shuttles_fixtures.ex b/test/support/fixtures/shuttles_fixtures.ex index c079bf334..8d073b078 100644 --- a/test/support/fixtures/shuttles_fixtures.ex +++ b/test/support/fixtures/shuttles_fixtures.ex @@ -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