-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adding API router and ItemsController. #256
- Loading branch information
1 parent
12f705e
commit b144656
Showing
3 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
defmodule AppWeb.API.ItemController do | ||
use AppWeb, :controller | ||
alias App.Item | ||
import Ecto.Changeset | ||
|
||
def show(conn, params) do | ||
id = Map.get(params, "id") | ||
|
||
try do | ||
item = Item.get_item!(id) | ||
json(conn, item) | ||
rescue | ||
Ecto.NoResultsError -> | ||
errors = %{ | ||
code: 404, | ||
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.", | ||
} | ||
json(conn |> put_status(400), errors) | ||
end | ||
end | ||
|
||
def create(conn, params) do | ||
# Attributes to create item | ||
# Person_id will be changed when auth is added | ||
attrs = %{ | ||
text: Map.get(params, "text"), | ||
person_id: 0, | ||
status: 2 | ||
} | ||
|
||
case Item.create_item(attrs) do | ||
|
||
# Successfully creates item | ||
{:ok, item} -> | ||
id_item = Map.take(item, [:id]) | ||
json(conn, id_item) | ||
|
||
# Error creating item | ||
{:error, %Ecto.Changeset{} = changeset} -> | ||
errors = make_changeset_errors_readable(changeset) | ||
|
||
json( | ||
conn |> put_status(400), | ||
errors | ||
) | ||
end | ||
end | ||
|
||
def update(conn, params) do | ||
id = Map.get(params, "id") | ||
new_text = Map.get(params, "text") | ||
|
||
item = Item.get_item!(id) | ||
|
||
case Item.update_item(item, %{text: new_text}) do | ||
|
||
# Successfully updates item | ||
{:ok, item} -> | ||
json(conn, item) | ||
|
||
# Error creating item | ||
{:error, %Ecto.Changeset{} = changeset} -> | ||
errors = make_changeset_errors_readable(changeset) | ||
|
||
json( | ||
conn |> put_status(400), | ||
errors | ||
) | ||
end | ||
end | ||
|
||
defp make_changeset_errors_readable(changeset) do | ||
errors = %{ | ||
code: 400, | ||
message: "Malformed request", | ||
} | ||
|
||
changeset_errors = traverse_errors(changeset, fn {msg, opts} -> | ||
Regex.replace(~r"%{(\w+)}", msg, fn _, key -> | ||
opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string() | ||
end) | ||
end) | ||
|
||
Map.put(errors, :errors, changeset_errors) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters