diff --git a/README.md b/README.md index 6313794..939bed1 100644 --- a/README.md +++ b/README.md @@ -124,8 +124,8 @@ Run the following command: This example aimed at _anyone_ building a Phoenix App -who wants to _automatically_ have a REST API. -For us @dwyl +who wants to _automatically_ have a REST API.
+For us [`@dwyl`]() who are building our API and App Web UI simultaneously, it serves as a gentle intro to the topic. @@ -156,8 +156,9 @@ for more detailed step-by-step introduction to Phoenix: Once you are comfortable with Phoenix, proceed with this example! -By the end -Our aim is to have two routes +### Is there an _Official_ Way of Doing Content Negotiation? + + @@ -234,12 +235,12 @@ Finished in 0.02 seconds -### 2. Generate `Quotes` Controller, View & Templates +### 2. Generate the `Quotes` Controller, View, Templates and Tests ``` -mix phx.gen.html Ctx Quotes quotes author:string text:string tags:string source:string --no-context --no-schema +mix phx.gen.html Ctx Quotes quotes author:string text:string tags:string source:string ``` @@ -296,10 +297,20 @@ but it still cannot be found, you likely have cyclic module usage in your code +> Before: +[`quotes_controller.ex`](https://github.com/dwyl/phoenix-content-negotiation-tutorial/blob/538fcd3388d8e9a7e4373600b2a33db686d15bc5/lib/app_web/controllers/quotes_controller.ex) +> and +> After: +[`quotes_controller.ex`]() + + +With tests passing again, let's do a bit of tidying up before proceeding. + + + -With tests passing again, let's do a bit of tidying up. -#### 2.1 Add the Quotes Resources to `lib/app_web/router.ex` +#### 2.3 Add the Quotes Resources to `lib/app_web/router.ex` Let's follow the instructions to add the resources to `lib/app_web/router.ex` diff --git a/lib/app_web/controllers/quotes_controller.ex b/lib/app_web/controllers/quotes_controller.ex deleted file mode 100644 index fde13a2..0000000 --- a/lib/app_web/controllers/quotes_controller.ex +++ /dev/null @@ -1,62 +0,0 @@ -defmodule AppWeb.QuotesController do - use AppWeb, :controller - - alias App.Ctx - alias App.Ctx.Quotes - - def index(conn, _params) do - quotes = Ctx.list_quotes() - render(conn, "index.html", quotes: quotes) - end - - def new(conn, _params) do - changeset = Ctx.change_quotes(%Quotes{}) - render(conn, "new.html", changeset: changeset) - end - - def create(conn, %{"quotes" => quotes_params}) do - case Ctx.create_quotes(quotes_params) do - {:ok, quotes} -> - conn - |> put_flash(:info, "Quotes created successfully.") - |> redirect(to: Routes.quotes_path(conn, :show, quotes)) - - {:error, %Ecto.Changeset{} = changeset} -> - render(conn, "new.html", changeset: changeset) - end - end - - def show(conn, %{"id" => id}) do - quotes = Ctx.get_quotes!(id) - render(conn, "show.html", quotes: quotes) - end - - def edit(conn, %{"id" => id}) do - quotes = Ctx.get_quotes!(id) - changeset = Ctx.change_quotes(quotes) - render(conn, "edit.html", quotes: quotes, changeset: changeset) - end - - def update(conn, %{"id" => id, "quotes" => quotes_params}) do - quotes = Ctx.get_quotes!(id) - - case Ctx.update_quotes(quotes, quotes_params) do - {:ok, quotes} -> - conn - |> put_flash(:info, "Quotes updated successfully.") - |> redirect(to: Routes.quotes_path(conn, :show, quotes)) - - {:error, %Ecto.Changeset{} = changeset} -> - render(conn, "edit.html", quotes: quotes, changeset: changeset) - end - end - - def delete(conn, %{"id" => id}) do - quotes = Ctx.get_quotes!(id) - {:ok, _quotes} = Ctx.delete_quotes(quotes) - - conn - |> put_flash(:info, "Quotes deleted successfully.") - |> redirect(to: Routes.quotes_path(conn, :index)) - end -end diff --git a/lib/app_web/templates/quotes/edit.html.eex b/lib/app_web/templates/quotes/edit.html.eex deleted file mode 100644 index 81863bb..0000000 --- a/lib/app_web/templates/quotes/edit.html.eex +++ /dev/null @@ -1,5 +0,0 @@ -

Edit Quotes

- -<%= render "form.html", Map.put(assigns, :action, Routes.quotes_path(@conn, :update, @quotes)) %> - -<%= link "Back", to: Routes.quotes_path(@conn, :index) %> diff --git a/lib/app_web/templates/quotes/form.html.eex b/lib/app_web/templates/quotes/form.html.eex deleted file mode 100644 index 36d6915..0000000 --- a/lib/app_web/templates/quotes/form.html.eex +++ /dev/null @@ -1,27 +0,0 @@ -<%= form_for @changeset, @action, fn f -> %> - <%= if @changeset.action do %> -
-

Oops, something went wrong! Please check the errors below.

-
- <% end %> - - <%= label f, :author %> - <%= text_input f, :author %> - <%= error_tag f, :author %> - - <%= label f, :text %> - <%= text_input f, :text %> - <%= error_tag f, :text %> - - <%= label f, :tags %> - <%= text_input f, :tags %> - <%= error_tag f, :tags %> - - <%= label f, :source %> - <%= text_input f, :source %> - <%= error_tag f, :source %> - -
- <%= submit "Save" %> -
-<% end %> diff --git a/lib/app_web/templates/quotes/index.html.eex b/lib/app_web/templates/quotes/index.html.eex deleted file mode 100644 index 996be43..0000000 --- a/lib/app_web/templates/quotes/index.html.eex +++ /dev/null @@ -1,32 +0,0 @@ -

Listing Quotes

- - - - - - - - - - - - - -<%= for quotes <- @quotes do %> - - - - - - - - -<% end %> - -
AuthorTextTagsSource
<%= quotes.author %><%= quotes.text %><%= quotes.tags %><%= quotes.source %> - <%= link "Show", to: Routes.quotes_path(@conn, :show, quotes) %> - <%= link "Edit", to: Routes.quotes_path(@conn, :edit, quotes) %> - <%= link "Delete", to: Routes.quotes_path(@conn, :delete, quotes), method: :delete, data: [confirm: "Are you sure?"] %> -
- -<%= link "New Quotes", to: Routes.quotes_path(@conn, :new) %> diff --git a/lib/app_web/templates/quotes/new.html.eex b/lib/app_web/templates/quotes/new.html.eex deleted file mode 100644 index 02ac4a1..0000000 --- a/lib/app_web/templates/quotes/new.html.eex +++ /dev/null @@ -1,5 +0,0 @@ -

New Quotes

- -<%= render "form.html", Map.put(assigns, :action, Routes.quotes_path(@conn, :create)) %> - -<%= link "Back", to: Routes.quotes_path(@conn, :index) %> diff --git a/lib/app_web/templates/quotes/show.html.eex b/lib/app_web/templates/quotes/show.html.eex deleted file mode 100644 index c52ab77..0000000 --- a/lib/app_web/templates/quotes/show.html.eex +++ /dev/null @@ -1,28 +0,0 @@ -

Show Quotes

- - - -<%= link "Edit", to: Routes.quotes_path(@conn, :edit, @quotes) %> -<%= link "Back", to: Routes.quotes_path(@conn, :index) %> diff --git a/lib/app_web/views/quotes_view.ex b/lib/app_web/views/quotes_view.ex deleted file mode 100644 index 5aee76e..0000000 --- a/lib/app_web/views/quotes_view.ex +++ /dev/null @@ -1,3 +0,0 @@ -defmodule AppWeb.QuotesView do - use AppWeb, :view -end diff --git a/test/app_web/controllers/quotes_controller_test.exs b/test/app_web/controllers/quotes_controller_test.exs deleted file mode 100644 index 78900af..0000000 --- a/test/app_web/controllers/quotes_controller_test.exs +++ /dev/null @@ -1,88 +0,0 @@ -defmodule AppWeb.QuotesControllerTest do - use AppWeb.ConnCase - - alias App.Ctx - - @create_attrs %{author: "some author", source: "some source", tags: "some tags", text: "some text"} - @update_attrs %{author: "some updated author", source: "some updated source", tags: "some updated tags", text: "some updated text"} - @invalid_attrs %{author: nil, source: nil, tags: nil, text: nil} - - def fixture(:quotes) do - {:ok, quotes} = Ctx.create_quotes(@create_attrs) - quotes - end - - describe "index" do - test "lists all quotes", %{conn: conn} do - conn = get(conn, Routes.quotes_path(conn, :index)) - assert html_response(conn, 200) =~ "Listing Quotes" - end - end - - describe "new quotes" do - test "renders form", %{conn: conn} do - conn = get(conn, Routes.quotes_path(conn, :new)) - assert html_response(conn, 200) =~ "New Quotes" - end - end - - describe "create quotes" do - test "redirects to show when data is valid", %{conn: conn} do - conn = post(conn, Routes.quotes_path(conn, :create), quotes: @create_attrs) - - assert %{id: id} = redirected_params(conn) - assert redirected_to(conn) == Routes.quotes_path(conn, :show, id) - - conn = get(conn, Routes.quotes_path(conn, :show, id)) - assert html_response(conn, 200) =~ "Show Quotes" - end - - test "renders errors when data is invalid", %{conn: conn} do - conn = post(conn, Routes.quotes_path(conn, :create), quotes: @invalid_attrs) - assert html_response(conn, 200) =~ "New Quotes" - end - end - - describe "edit quotes" do - setup [:create_quotes] - - test "renders form for editing chosen quotes", %{conn: conn, quotes: quotes} do - conn = get(conn, Routes.quotes_path(conn, :edit, quotes)) - assert html_response(conn, 200) =~ "Edit Quotes" - end - end - - describe "update quotes" do - setup [:create_quotes] - - test "redirects when data is valid", %{conn: conn, quotes: quotes} do - conn = put(conn, Routes.quotes_path(conn, :update, quotes), quotes: @update_attrs) - assert redirected_to(conn) == Routes.quotes_path(conn, :show, quotes) - - conn = get(conn, Routes.quotes_path(conn, :show, quotes)) - assert html_response(conn, 200) =~ "some updated author" - end - - test "renders errors when data is invalid", %{conn: conn, quotes: quotes} do - conn = put(conn, Routes.quotes_path(conn, :update, quotes), quotes: @invalid_attrs) - assert html_response(conn, 200) =~ "Edit Quotes" - end - end - - describe "delete quotes" do - setup [:create_quotes] - - test "deletes chosen quotes", %{conn: conn, quotes: quotes} do - conn = delete(conn, Routes.quotes_path(conn, :delete, quotes)) - assert redirected_to(conn) == Routes.quotes_path(conn, :index) - assert_error_sent 404, fn -> - get(conn, Routes.quotes_path(conn, :show, quotes)) - end - end - end - - defp create_quotes(_) do - quotes = fixture(:quotes) - %{quotes: quotes} - end -end