From f6d2bcf3fde20172be6b9ff358568744ed77622e Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Tue, 24 Sep 2024 22:44:37 +0100 Subject: [PATCH] Add function to search for record --- lib/music_library/records.ex | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/music_library/records.ex b/lib/music_library/records.ex index 66307950..18387efc 100644 --- a/lib/music_library/records.ex +++ b/lib/music_library/records.ex @@ -17,6 +17,20 @@ defmodule MusicLibrary.Records do Repo.all(q) end + def search_records(query, opts \\ []) do + limit = Keyword.get(opts, :limit, 20) + offset = Keyword.get(opts, :offset, 0) + + q = + from r in Record, + where: like(r.title, ^"%#{query}%") or like(r.artists[0]["name"], ^"%#{query}%"), + order_by: [r.artists[0]["sort_name"], r.title], + limit: ^limit, + offset: ^offset + + Repo.all(q) + end + def count_records do Repo.aggregate(Record, :count) end