From 3ec039c2518654d34636870bd7e65034c7b432b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lui=CC=81s=20Arteiro?= Date: Thu, 12 Jan 2023 16:38:25 +0000 Subject: [PATCH] fix: Mix format. Fixing typo in API document. #256 --- api.md | 9 +++----- lib/app/timer.ex | 6 +++-- .../controllers/api/item_controller.ex | 10 ++++---- .../controllers/api/timer_controller.ex | 11 ++++----- lib/app_web/router.ex | 4 +++- test/app/timer_test.exs | 3 ++- .../controllers/api/item_controller_test.exs | 6 +++-- .../controllers/api/timer_controller_test.exs | 23 ++++++++++++++----- 8 files changed, 43 insertions(+), 29 deletions(-) diff --git a/api.md b/api.md index 5efc6ab2..8e8de9dd 100644 --- a/api.md +++ b/api.md @@ -77,10 +77,7 @@ and check if it's correctly formatted. ### 2.1 Adding tests -Since we now know what to do, let's create our tests. - -Let's implement these controllers. -But before that, let's approach this +Let's approach this with a [`TDD mindset`](https://github.com/dwyl/learn-tdd) and create our tests first! @@ -609,10 +606,10 @@ add the following private function. end ``` -If `stop` or `start` is nil, we can't compare the datetimes, +If `stop` or `start` is `nil`, we can't compare the datetimes, so we just skip the validation. This usually happens when creating a timer that is ongoing -(`stop` is nil). +(`stop` is `nil`). We won't block creating `timers` with `stop` with a `nil` value. Now let's use this validator! diff --git a/lib/app/timer.ex b/lib/app/timer.ex index 33109081..bd36760e 100644 --- a/lib/app/timer.ex +++ b/lib/app/timer.ex @@ -7,7 +7,7 @@ defmodule App.Timer do alias __MODULE__ require Logger - @derive {Jason.Encoder, only: [:id, :start, :stop, ]} + @derive {Jason.Encoder, only: [:id, :start, :stop]} schema "timers" do field :start, :naive_datetime field :stop, :naive_datetime @@ -22,7 +22,9 @@ defmodule App.Timer do # If start or stop is nil, no comparison occurs. case is_nil(stop) or is_nil(start) do - true -> changeset + true -> + changeset + false -> if NaiveDateTime.compare(start, stop) == :gt do add_error(changeset, :start, "cannot be later than 'stop'") diff --git a/lib/app_web/controllers/api/item_controller.ex b/lib/app_web/controllers/api/item_controller.ex index 4e54f6c4..3b4994ff 100644 --- a/lib/app_web/controllers/api/item_controller.ex +++ b/lib/app_web/controllers/api/item_controller.ex @@ -13,15 +13,17 @@ defmodule AppWeb.API.ItemController do Ecto.NoResultsError -> errors = %{ code: 404, - message: "No item found with the given \'id\'.", + message: "No item found with the given \'id\'." } + json(conn |> put_status(404), errors) Ecto.Query.CastError -> errors = %{ code: 400, - message: "The \'id\' is not an integer.", + message: "The \'id\' is not an integer." } + json(conn |> put_status(400), errors) end end @@ -36,7 +38,6 @@ defmodule AppWeb.API.ItemController do } case Item.create_item(attrs) do - # Successfully creates item {:ok, item} -> id_item = Map.take(item, [:id]) @@ -60,7 +61,6 @@ defmodule AppWeb.API.ItemController do item = Item.get_item!(id) case Item.update_item(item, %{text: new_text}) do - # Successfully updates item {:ok, item} -> json(conn, item) @@ -79,7 +79,7 @@ defmodule AppWeb.API.ItemController do defp make_changeset_errors_readable(changeset) do errors = %{ code: 400, - message: "Malformed request", + message: "Malformed request" } changeset_errors = traverse_errors(changeset, fn {msg, _opts} -> msg end) diff --git a/lib/app_web/controllers/api/timer_controller.ex b/lib/app_web/controllers/api/timer_controller.ex index 310b171e..3fff2786 100644 --- a/lib/app_web/controllers/api/timer_controller.ex +++ b/lib/app_web/controllers/api/timer_controller.ex @@ -20,15 +20,17 @@ defmodule AppWeb.API.TimerController do Ecto.NoResultsError -> errors = %{ code: 404, - message: "No timer found with the given \'id\'.", + message: "No timer found with the given \'id\'." } + json(conn |> put_status(404), errors) Ecto.Query.CastError -> errors = %{ code: 400, - message: "The \'id\' is not an integer.", + message: "The \'id\' is not an integer." } + json(conn |> put_status(400), errors) end end @@ -42,7 +44,6 @@ defmodule AppWeb.API.TimerController do } case Timer.start(attrs) do - # Successfully creates item {:ok, timer} -> id_timer = Map.take(timer, [:id]) @@ -68,7 +69,6 @@ defmodule AppWeb.API.TimerController do } case Timer.update_timer(attrs_to_update) do - # Successfully updates timer {:ok, timer} -> json(conn, timer) @@ -84,11 +84,10 @@ defmodule AppWeb.API.TimerController do end end - defp make_changeset_errors_readable(changeset) do errors = %{ code: 400, - message: "Malformed request", + message: "Malformed request" } changeset_errors = traverse_errors(changeset, fn {msg, _opts} -> msg end) diff --git a/lib/app_web/router.ex b/lib/app_web/router.ex index ff58f9b7..e8bbfaac 100644 --- a/lib/app_web/router.ex +++ b/lib/app_web/router.ex @@ -38,6 +38,8 @@ defmodule AppWeb.Router do pipe_through [:api, :authOptional] resources "/items", API.ItemController, only: [:create, :update, :show] - resources "/items/:item_id/timers", API.TimerController, only: [:create, :update, :show, :index] + + resources "/items/:item_id/timers", API.TimerController, + only: [:create, :update, :show, :index] end end diff --git a/test/app/timer_test.exs b/test/app/timer_test.exs index 9fc7cb16..5d3a202a 100644 --- a/test/app/timer_test.exs +++ b/test/app/timer_test.exs @@ -108,7 +108,8 @@ defmodule App.TimerTest do Timer.stop_timer_for_item_id(item.id) # Update timer with stop earlier than start - {:error, changeset} = Timer.update_timer(%{id: timer.id, start: start, stop: stop}) + {:error, changeset} = + Timer.update_timer(%{id: timer.id, start: start, stop: stop}) assert length(changeset.errors) > 0 end diff --git a/test/app_web/controllers/api/item_controller_test.exs b/test/app_web/controllers/api/item_controller_test.exs index 0defacdc..fded3848 100644 --- a/test/app_web/controllers/api/item_controller_test.exs +++ b/test/app_web/controllers/api/item_controller_test.exs @@ -35,9 +35,11 @@ defmodule AppWeb.API.ItemControllerTest do assert conn.status == 200 assert json_response(conn, 200)["text"] == Map.get(@create_attrs, "text") - assert json_response(conn, 200)["status"] == Map.get(@create_attrs, "status") + assert json_response(conn, 200)["status"] == + Map.get(@create_attrs, "status") - assert json_response(conn, 200)["person_id"] == Map.get(@create_attrs, "person_id") + assert json_response(conn, 200)["person_id"] == + Map.get(@create_attrs, "person_id") end test "an invalid item", %{conn: conn} do diff --git a/test/app_web/controllers/api/timer_controller_test.exs b/test/app_web/controllers/api/timer_controller_test.exs index 240df1c4..b38d1bc3 100644 --- a/test/app_web/controllers/api/timer_controller_test.exs +++ b/test/app_web/controllers/api/timer_controller_test.exs @@ -52,22 +52,25 @@ defmodule AppWeb.API.TimerControllerTest do describe "create" do test "a valid timer", %{conn: conn} do - # Create item {:ok, item} = Item.create_item(@create_item_attrs) # Create timer - conn = post(conn, Routes.timer_path(conn, :create, item.id, @create_attrs)) + conn = + post(conn, Routes.timer_path(conn, :create, item.id, @create_attrs)) assert conn.status == 200 - assert json_response(conn, 200)["start"] == Map.get(@create_attrs, "start") + + assert json_response(conn, 200)["start"] == + Map.get(@create_attrs, "start") end test "an invalid timer", %{conn: conn} do # Create item {:ok, item} = Item.create_item(@create_item_attrs) - conn = post(conn, Routes.timer_path(conn, :create, item.id, @invalid_attrs)) + conn = + post(conn, Routes.timer_path(conn, :create, item.id, @invalid_attrs)) assert conn.status == 400 assert length(json_response(conn, 400)["errors"]["start"]) > 0 @@ -79,7 +82,11 @@ defmodule AppWeb.API.TimerControllerTest do # Create item and timer {item, timer} = item_and_timer_fixture() - conn = put(conn, Routes.timer_path(conn, :update, item.id, timer.id, @update_attrs)) + conn = + put( + conn, + Routes.timer_path(conn, :update, item.id, timer.id, @update_attrs) + ) assert conn.status == 200 assert json_response(conn, 200)["start"] == Map.get(@update_attrs, :start) @@ -89,7 +96,11 @@ defmodule AppWeb.API.TimerControllerTest do # Create item and timer {item, timer} = item_and_timer_fixture() - conn = put(conn, Routes.timer_path(conn, :update, item.id, timer.id, @invalid_attrs)) + conn = + put( + conn, + Routes.timer_path(conn, :update, item.id, timer.id, @invalid_attrs) + ) assert conn.status == 400 assert length(json_response(conn, 400)["errors"]["start"]) > 0