Add /api/collection/on_this_day endpoint
This commit is contained in:
@@ -15,6 +15,18 @@ defmodule MusicLibraryWeb.CollectionController do
|
|||||||
render(conn, :show, record: random_record)
|
render(conn, :show, record: random_record)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def on_this_day(conn, params) do
|
||||||
|
current_date =
|
||||||
|
case Map.get(params, "date") do
|
||||||
|
nil -> Date.utc_today()
|
||||||
|
date_string -> Date.from_iso8601!(date_string)
|
||||||
|
end
|
||||||
|
|
||||||
|
records_on_this_day = Collection.get_records_on_this_day(current_date)
|
||||||
|
|
||||||
|
render(conn, :on_this_day, records: records_on_this_day)
|
||||||
|
end
|
||||||
|
|
||||||
def index(conn, params) do
|
def index(conn, params) do
|
||||||
limit =
|
limit =
|
||||||
params
|
params
|
||||||
|
|||||||
@@ -16,6 +16,12 @@ defmodule MusicLibraryWeb.CollectionJSON do
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def on_this_day(%{records: records}) do
|
||||||
|
%{
|
||||||
|
records: Enum.map(records, &record/1)
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
defp record(record) do
|
defp record(record) do
|
||||||
%{
|
%{
|
||||||
id: record.id,
|
id: record.id,
|
||||||
|
|||||||
@@ -84,6 +84,7 @@ defmodule MusicLibraryWeb.Router do
|
|||||||
|
|
||||||
get "/collection/latest", CollectionController, :latest
|
get "/collection/latest", CollectionController, :latest
|
||||||
get "/collection/random", CollectionController, :random
|
get "/collection/random", CollectionController, :random
|
||||||
|
get "/collection/on_this_day", CollectionController, :on_this_day
|
||||||
get "/collection", CollectionController, :index
|
get "/collection", CollectionController, :index
|
||||||
get "/assets/:transform_payload", AssetController, :show
|
get "/assets/:transform_payload", AssetController, :show
|
||||||
get "/backup", ArchiveController, :backup
|
get "/backup", ArchiveController, :backup
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ defmodule MusicLibraryWeb.CollectionControllerTest do
|
|||||||
import MusicLibrary.Fixtures.Records
|
import MusicLibrary.Fixtures.Records
|
||||||
|
|
||||||
defp create_record(_) do
|
defp create_record(_) do
|
||||||
%{record: record_with_artist("Steven Wilson")}
|
%{record: record_with_artist("Steven Wilson", %{release_date: "2025-06-21"})}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp api_token do
|
defp api_token do
|
||||||
@@ -100,4 +100,35 @@ defmodule MusicLibraryWeb.CollectionControllerTest do
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "GET /api/collection/on_this_day" do
|
||||||
|
setup [:create_record]
|
||||||
|
|
||||||
|
test "it requires authentication", %{conn: conn} do
|
||||||
|
conn = get(conn, ~p"/api/collection/on_this_day")
|
||||||
|
|
||||||
|
assert conn.status == 401
|
||||||
|
end
|
||||||
|
|
||||||
|
test "it returns a list of records", %{conn: conn, record: record} do
|
||||||
|
conn =
|
||||||
|
conn
|
||||||
|
|> put_req_header("authorization", "Bearer #{api_token()}")
|
||||||
|
|> get(~p"/api/collection/on_this_day?date=2025-06-21")
|
||||||
|
|
||||||
|
assert json_response(conn, 200) == %{
|
||||||
|
"records" => [
|
||||||
|
%{
|
||||||
|
"id" => record.id,
|
||||||
|
"artists" => ["Steven Wilson"],
|
||||||
|
"title" => record.title,
|
||||||
|
"cover_url" =>
|
||||||
|
"http://localhost:4002/api/assets/eyJoYXNoIjoiNTk5NDA3RERGNjk5MDdENEE2MEZFMTNDQ0FBODI0RDI1Q0YwOERDMTI0RkQ2QUEzRThFN0VDRDk4Qzg4NUZGRSIsIndpZHRoIjpudWxsfQ",
|
||||||
|
"thumb_url" =>
|
||||||
|
"http://localhost:4002/api/assets/eyJoYXNoIjoiNTk5NDA3RERGNjk5MDdENEE2MEZFMTNDQ0FBODI0RDI1Q0YwOERDMTI0RkQ2QUEzRThFN0VDRDk4Qzg4NUZGRSIsIndpZHRoIjo0ODB9"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user