Skip to content

Commit

Permalink
fix: Mix format. Fixing typo in API document. #256
Browse files Browse the repository at this point in the history
  • Loading branch information
LuchoTurtle committed Jan 12, 2023
1 parent 600d123 commit 3ec039c
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 29 deletions.
9 changes: 3 additions & 6 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!

Expand Down Expand Up @@ -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!
Expand Down
6 changes: 4 additions & 2 deletions lib/app/timer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'")
Expand Down
10 changes: 5 additions & 5 deletions lib/app_web/controllers/api/item_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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])
Expand All @@ -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)
Expand All @@ -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)
Expand Down
11 changes: 5 additions & 6 deletions lib/app_web/controllers/api/timer_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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])
Expand All @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion lib/app_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion test/app/timer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions test/app_web/controllers/api/item_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 17 additions & 6 deletions test/app_web/controllers/api/timer_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 3ec039c

Please sign in to comment.