diff --git a/lib/music_library_web/controllers/collection_controller.ex b/lib/music_library_web/controllers/collection_controller.ex index 74e4324e..2c6b1aa9 100644 --- a/lib/music_library_web/controllers/collection_controller.ex +++ b/lib/music_library_web/controllers/collection_controller.ex @@ -14,4 +14,20 @@ defmodule MusicLibraryWeb.CollectionController do render(conn, :show, record: random_record) end + + def index(conn, params) do + limit = + Map.get(params, "limit", "20") + |> String.to_integer() + + offset = + Map.get(params, "offset", "0") + |> String.to_integer() + + total = Collection.search_records_count("") + + records = Collection.search_records("", limit: limit, offset: offset) + + render(conn, :index, total: total, limit: limit, offset: offset, records: records) + end end diff --git a/lib/music_library_web/controllers/collection_json.ex b/lib/music_library_web/controllers/collection_json.ex index cadb3b22..68cf0fd0 100644 --- a/lib/music_library_web/controllers/collection_json.ex +++ b/lib/music_library_web/controllers/collection_json.ex @@ -2,7 +2,21 @@ defmodule MusicLibraryWeb.CollectionJSON do use MusicLibraryWeb, :json def show(%{record: record}) do + record(record) + end + + def index(%{records: records, total: total, limit: limit, offset: offset}) do %{ + total: total, + limit: limit, + offset: offset, + records: Enum.map(records, &record/1) + } + end + + defp record(record) do + %{ + id: record.id, artists: Enum.map(record.artists, & &1.name), title: record.title, cover_url: url(~p"/api/covers/#{record.id}?#{[vsn: record.cover_hash]}"), diff --git a/lib/music_library_web/router.ex b/lib/music_library_web/router.ex index b078cb9e..ebefdb6c 100644 --- a/lib/music_library_web/router.ex +++ b/lib/music_library_web/router.ex @@ -61,6 +61,7 @@ defmodule MusicLibraryWeb.Router do get "/collection/latest", CollectionController, :latest get "/collection/random", CollectionController, :random + get "/collection", CollectionController, :index get "/covers/:record_id", CoverController, :show get "/backup", ArchiveController, :backup end diff --git a/test/music_library_web/controllers/collection_controller_test.exs b/test/music_library_web/controllers/collection_controller_test.exs index 6631ea76..a5e867fb 100644 --- a/test/music_library_web/controllers/collection_controller_test.exs +++ b/test/music_library_web/controllers/collection_controller_test.exs @@ -28,6 +28,7 @@ defmodule MusicLibraryWeb.CollectionControllerTest do |> get(~p"/api/collection/latest") assert json_response(conn, 200) == %{ + "id" => record.id, "artists" => ["Steven Wilson"], "title" => record.title, "cover_url" => @@ -55,6 +56,7 @@ defmodule MusicLibraryWeb.CollectionControllerTest do |> get(~p"/api/collection/random") assert json_response(conn, 200) == %{ + "id" => record.id, "artists" => ["Steven Wilson"], "title" => record.title, "cover_url" => @@ -64,4 +66,38 @@ defmodule MusicLibraryWeb.CollectionControllerTest do } end end + + describe "GET /api/collection" do + setup [:create_record] + + test "it requires authentication", %{conn: conn} do + conn = get(conn, ~p"/api/collection") + + assert conn.status == 401 + end + + test "it returns a paginated list of records", %{conn: conn, record: record} do + conn = + conn + |> put_req_header("authorization", "Bearer #{api_token()}") + |> get(~p"/api/collection") + + assert json_response(conn, 200) == %{ + "total" => 1, + "limit" => 20, + "offset" => 0, + "records" => [ + %{ + "id" => record.id, + "artists" => ["Steven Wilson"], + "title" => record.title, + "cover_url" => + "http://localhost:4002/api/covers/#{record.id}?vsn=#{record.cover_hash}", + "thumb_url" => + "http://localhost:4002/api/covers/#{record.id}?vsn=#{record.cover_hash}&size=480" + } + ] + } + end + end end