this repo has no description

Base for API controller

hauleth.dev 12ee1da6 33ba8b0d

verified
Changed files
+31 -1
lib
+19
lib/esl_hn_web/api/controller.ex
···
+
defmodule EslHnWeb.API.Controller do
+
use EslHnWeb, :controller
+
+
def index(conn, _) do
+
render(conn, items: Enum.map([], &one/1))
+
end
+
+
def show(conn, %{"id" => _id}) do
+
render(conn, item: one(%EslHn.Hn.Story{}))
+
end
+
+
defp one(%EslHn.Hn.Story{} = story) do
+
%{
+
title: story.title,
+
url: story.url,
+
score: story.score
+
}
+
end
+
end
+9
lib/esl_hn_web/api/json.ex
···
+
defmodule EslHnWeb.API.JSON do
+
def index(attrs) do
+
attrs.items
+
end
+
+
def show(attrs) do
+
attrs.item
+
end
+
end
+3 -1
lib/esl_hn_web/router.ex
···
plug :accepts, ["json"]
end
-
scope "/api", EslHnWeb do
+
scope "/", EslHnWeb do
pipe_through :api
+
+
get "/", API.Controller, :index
end
if Application.compile_env(:esl_hn, :dev_routes) do