Last active
December 18, 2015 23:59
-
-
Save guilleiguaran/5865671 to your computer and use it in GitHub Desktop.
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
defmodule PostsRouter do | |
use Dynamo.Router | |
alias Ecto.SQL | |
import Ecto.Query | |
prepare do | |
conn.fetch :params | |
end | |
# GET /posts | |
get "/" do | |
conn = conn.assign(:posts, posts) | |
render conn, "posts/index.html" | |
end | |
# GET /posts/:id | |
get "/:id" do | |
[post] = posts(conn.params[:id]) | |
conn = conn.assign(:post, post) | |
render conn, "posts/show.html" | |
end | |
defp posts do | |
from(p in Posts) | |
|> select({p.id, p.title, p.body, p.created_at}) | |
|> exec | |
end | |
defp posts(id) do | |
from(p in Posts) | |
|> where(p.id == id) | |
|> select({p.title, p.body, p.created_at}) | |
|> exec | |
end | |
# helpers | |
# TODO: Fix this | |
defp exec(query) do | |
{:ok, conn} = DBI.PostgreSQL.connect([{:database, "blog_development"}]) | |
{:ok, result} = DBI.query(conn, SQL.compile(query)) | |
result.rows | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment