Skip to content

Commit

Permalink
Merge b57f3e1 into 882ff2a
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandruBurlacu committed Sep 25, 2017
2 parents 882ff2a + b57f3e1 commit 6cd9431
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 48 deletions.
10 changes: 5 additions & 5 deletions lib/elixir_docker.ex → lib/main.ex
@@ -1,16 +1,16 @@
defmodule ElixirDocker do
@moduledoc """
Documentation for ElixirDocker.
Entry point and handler for ElixirDocker app.
"""

use Application
alias Users
require Logger
import Supervisor.Spec

def start(_type, _args) do
children = [
Supervisor.Spec.worker(ElixirDocker.Repo, []),
Supervisor.Spec.worker(ElixirDocker.Router, [])
worker(ElixirDocker.Repo, []),
worker(ElixirDocker.Router, [])
]

Logger.info "Started application"
Expand Down
13 changes: 10 additions & 3 deletions lib/record.ex
@@ -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
4 changes: 4 additions & 0 deletions lib/repo.ex
@@ -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
30 changes: 12 additions & 18 deletions lib/router.ex
@@ -1,10 +1,18 @@
defmodule ElixirDocker.Router do
@moduledoc """
Top level router of the TODO API.
"""

use Plug.Router
import Plug.Conn

alias ElixirDocker.Record
alias ElixirDocker.Repo
alias Plug.Adapters.Cowboy
alias ElixirDocker.Router.TasksRouter

if Mix.env == :dev do
use Plug.Debugger
plug Plug.Logger, log: :debug
end

plug :match
plug :dispatch
Expand All @@ -17,25 +25,11 @@ defmodule ElixirDocker.Router do
{:ok, _} = Cowboy.http(ElixirDocker.Router, [], port: 8080)
end

get "/status" do
if System.get_env("MIX_ENV") == "test" do
timestamp = Ecto.DateTime.from_date_and_time(%Ecto.Date{year: 2017, month: 09, day: 03},
%Ecto.Time{hour: 12, min: 42, sec: 50, usec: 00})
last_run = %{inserted_at: timestamp, count: 10}
else
last_run = Record |> Ecto.Query.last |> Repo.one
end

data = Poison.encode!(%{last_run: last_run.inserted_at,
result_count: last_run.count})

conn
|> put_resp_content_type("application/json")
|> send_resp(200, data)
end
forward "/v1/tasks", to: TasksRouter

match _ do
conn
|> put_resp_content_type("application/json")
|> send_resp(404, Poison.encode!(%{endpoint: "not implemented!"}))
end
end
124 changes: 124 additions & 0 deletions lib/task_router.ex
@@ -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
20 changes: 0 additions & 20 deletions lib/users.ex

This file was deleted.

4 changes: 2 additions & 2 deletions mix.exs
Expand Up @@ -26,9 +26,9 @@ defmodule ElixirDocker.Mixfile do
{:cowboy, "~> 1.1.2"},
{:plug, "~> 1.3.4"},
{:postgrex, ">= 0.0.0"},
{:ecto, "~> 2.0.0-beta"},
{:ecto, "~> 2.1.0"},
{:httpotion, "~> 2.1.0"},
{:poison, "~> 1.5"},
{:poison, "~> 2.2"},
{:ibrowse, github: "cmullaparthi/ibrowse", tag: "v4.1.2"},
# for dev and test purpose only
{:excoveralls, github: "parroty/excoveralls", only: :test},
Expand Down
16 changes: 16 additions & 0 deletions priv/repo/migrations/20170923195206_change_schema.exs
@@ -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

0 comments on commit 6cd9431

Please sign in to comment.