Bootstrap the /api namespace

This commit is contained in:
Claudio Ortolina
2024-12-21 14:38:39 +00:00
parent 89c9c4bf74
commit 4f456322fc
4 changed files with 40 additions and 0 deletions
@@ -0,0 +1,11 @@
defmodule MusicLibraryWeb.CollectionController do
use MusicLibraryWeb, :controller
alias MusicLibrary.Collection
def latest(conn, _params) do
latest_record = Collection.get_latest_record!()
render(conn, :show, record: latest_record)
end
end
@@ -0,0 +1,5 @@
defmodule MusicLibraryWeb.CollectionJSON do
def show(%{record: record}) do
Map.take(record, [:title])
end
end
+6
View File
@@ -50,6 +50,12 @@ defmodule MusicLibraryWeb.Router do
end end
end end
scope "/api", MusicLibraryWeb do
pipe_through :api
get "/collection/latest", CollectionController, :latest
end
# Other scopes may use custom stacks. # Other scopes may use custom stacks.
# scope "/api", MusicLibraryWeb do # scope "/api", MusicLibraryWeb do
# pipe_through :api # pipe_through :api
@@ -0,0 +1,18 @@
defmodule MusicLibraryWeb.CollectionControllerTest do
use MusicLibraryWeb.ConnCase
import MusicLibrary.RecordsFixtures
defp create_record(_) do
%{record: record_fixture()}
end
describe "GET /api/collection/latest" do
setup [:create_record]
test "it returns the latest record", %{conn: conn, record: record} do
conn = get(conn, ~p"/api/collection/latest")
assert json_response(conn, 200) == %{"title" => record.title}
end
end
end