Adds API to return a random record from the collection
This commit is contained in:
@@ -50,6 +50,17 @@ defmodule MusicLibrary.Collection do
|
||||
Repo.one!(q)
|
||||
end
|
||||
|
||||
def get_random_record! do
|
||||
q =
|
||||
from r in Record,
|
||||
where: not is_nil(r.purchased_at),
|
||||
order_by: fragment("RANDOM()"),
|
||||
limit: 1,
|
||||
select: ^Records.essential_fields()
|
||||
|
||||
Repo.one!(q)
|
||||
end
|
||||
|
||||
def collected_releases(release_ids) do
|
||||
q =
|
||||
from r in fragment("records, json_each(records.release_ids)"),
|
||||
|
||||
@@ -8,4 +8,10 @@ defmodule MusicLibraryWeb.CollectionController do
|
||||
|
||||
render(conn, :show, record: latest_record)
|
||||
end
|
||||
|
||||
def random(conn, _params) do
|
||||
random_record = Collection.get_random_record!()
|
||||
|
||||
render(conn, :show, record: random_record)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -58,6 +58,7 @@ defmodule MusicLibraryWeb.Router do
|
||||
pipe_through :api
|
||||
|
||||
get "/collection/latest", CollectionController, :latest
|
||||
get "/collection/random", CollectionController, :random
|
||||
get "/covers/:record_id", CoverController, :show
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user