Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
173 additions
and
48 deletions.
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 |
---|---|---|
@@ -1,9 +1,16 @@ | ||
defmodule ElixirDocker.Record do | ||
@moduledoc """ | ||
Describes the 'tasks' table in the Database. | ||
""" | ||
|
||
use Ecto.Schema | ||
|
||
schema "records" do | ||
field :count, :integer | ||
schema "tasks" do | ||
field :title, :string | ||
field :description, :string | ||
field :due_date, :date | ||
field :priority, :string | ||
|
||
timestamps | ||
timestamps() | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
defmodule ElixirDocker.Repo do | ||
@moduledoc """ | ||
I think it binds the Ecto Repository to the app. | ||
""" | ||
|
||
use Ecto.Repo, otp_app: :elixir_docker | ||
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
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,124 @@ | ||
defmodule ElixirDocker.Router.TasksRouter do | ||
@moduledoc """ | ||
Special purpose router to handle | ||
CRUD operations over 'tasks' in the TODO API. | ||
""" | ||
|
||
use Plug.Router | ||
import Ecto.Query | ||
|
||
alias ElixirDocker.Record | ||
alias ElixirDocker.Repo | ||
|
||
plug :match | ||
plug Plug.Parsers, parsers: [:json], | ||
pass: ["application/json"], | ||
json_decoder: Poison | ||
plug :dispatch | ||
|
||
defp response(connection, code, data) do | ||
connection | ||
|> put_resp_content_type("application/json") | ||
|> send_resp(code, Poison.encode!(data)) | ||
end | ||
|
||
defp valid_data?(data) do | ||
["title", "description", "due_date", "priority"] | ||
|> Enum.map(fn k -> | ||
data | ||
|> Map.has_key?(k) | ||
end) | ||
|> Enum.all? | ||
end | ||
|
||
defp stringify_dates(raw_data) do | ||
raw_data |> Enum.map(fn record -> | ||
{{y, m, d}, _} = record.inserted_at | ||
{due_y, due_m, due_d} = record.due_date | ||
|
||
record | ||
|> Map.put(:inserted_at, "#{y}-#{m}-#{d}") | ||
|> Map.put(:due_date, "#{due_y}-#{due_m}-#{due_d}") | ||
end) | ||
end | ||
|
||
post "/" do | ||
data = conn.body_params | ||
|
||
case (valid_data?(data)) do | ||
true -> record = %Record{title: data["title"], | ||
description: data["description"], | ||
due_date: Date.from_iso8601!(data["due_date"]), | ||
priority: data["priority"]} | ||
|
||
resp = Repo.insert! record | ||
|
||
conn |> response(201, %{info: "Task created", task_id: resp.id}) | ||
false -> conn |> response(400, %{info: "Invalid body"}) | ||
end | ||
end | ||
|
||
post "/:id" do | ||
conn |> response(405, %{info: "Impossible to update tasks via POST, use PUT instead"}) | ||
end | ||
|
||
get "/" do | ||
data = Repo.all(from t in "tasks", | ||
select: [:title, :description, | ||
:due_date, :priority, | ||
:inserted_at, :id]) | ||
case (data) do | ||
[] -> conn |> response(404, %{info: "No available tasks"}) | ||
_ -> conn |> response(200, %{info: "The available tasks", | ||
data: stringify_dates(data)}) | ||
end | ||
end | ||
|
||
get "/:id" do | ||
data = Repo.all(from t in "tasks", | ||
where: t.id == ^String.to_integer(id), | ||
select: [:title, :description, | ||
:due_date, :priority, | ||
:inserted_at]) | ||
|
||
case (data) do | ||
[] -> conn |> response(404, %{info: "No information about tasks ##{id}"}) | ||
_ -> conn |> response(200, %{info: "Information about tasks ##{id}", | ||
data: stringify_dates(data)}) | ||
end | ||
end | ||
|
||
put "/" do | ||
conn |> response(405, %{info: "Impossible to update all tasks"}) | ||
end | ||
|
||
put "/:id" do | ||
data = conn.body_params | ||
|
||
case (valid_data?(data)) do | ||
true -> resp = from(t in "tasks", where: t.id == ^String.to_integer(id), | ||
update: [set: [title: ^data["title"], | ||
description: ^data["description"], | ||
due_date: ^Date.from_iso8601!(data["due_date"]), | ||
priority: ^data["priority"]]]) | ||
|> Repo.update_all([]) | ||
|
||
conn |> response(200, %{info: "Task ##{id} updated"}) | ||
false -> conn |> response(400, %{info: "Invalid body"}) | ||
end | ||
end | ||
|
||
delete "/" do | ||
conn |> response(405, %{info: "Impossible to delete all tasks"}) | ||
end | ||
|
||
delete "/:id" do | ||
stats = Ecto.Adapters.SQL.query(Repo, | ||
"DELETE FROM tasks WHERE id = $1", [String.to_integer id]) | ||
|
||
case (stats) do | ||
{:ok, _} -> conn |> response(200, %{info: "Task ##{id} deleted"}) | ||
{:error, _} -> conn |> response(700, %{info: "Task ##{id} not deleted"}) | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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,16 @@ | ||
defmodule ElixirDocker.Repo.Migrations.ChangeSchema do | ||
use Ecto.Migration | ||
|
||
def change do | ||
drop table(:records) | ||
|
||
create table(:tasks) do | ||
add :title, :string | ||
add :description, :string | ||
add :due_date, :date | ||
add :priority, :string | ||
|
||
timestamps | ||
end | ||
end | ||
end |