Skip to content

Commit

Permalink
✨ Able to search albums by event_id
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewvy committed Dec 9, 2018
1 parent 9932ac6 commit 5e14123
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/dojinlist/albums.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ defmodule Dojinlist.Albums do
|> where([o, g], g.genre_id in ^ids)
end

def build_query_from_attr(query, {:event_id, id}) do
id = id |> Utility.parse_integer()

query
|> where([a], a.event_id == ^id)
end

def build_query_from_attr(query, _), do: query

def create_album(attrs) do
Expand Down
2 changes: 2 additions & 0 deletions lib/dojinlist_web/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ defmodule DojinlistWeb.Schema do
arg(:genre_ids, list_of(:id))
arg(:artist_names, list_of(:string))
arg(:genre_names, list_of(:string))
arg(:event_id, :id)

middleware(Absinthe.Relay.Node.ParseIDs, artist_ids: :artist)
middleware(Absinthe.Relay.Node.ParseIDs, genre_ids: :genre)
middleware(Absinthe.Relay.Node.ParseIDs, event_id: :event)

resolve(&Resolvers.Album.all/2)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/dojinlist_web/types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule DojinlistWeb.Types do
field :release_date, :datetime
field :sample_url, :string
field :purchase_url, :string
field :event, :event
field :event, :event, resolve: dataloader(Dojinlist.Source)

field :cover_art_url, :string do
resolve(fn album, _, _ ->
Expand Down
43 changes: 43 additions & 0 deletions test/dojinlist_web/resolvers/album.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
defmodule DojinlistWeb.Resolvers.AlbumTest do
use DojinlistWeb.ConnCase

alias Dojinlist.Fixtures

@query """
query SearchAlbums($eventId: ID) {
albums(first: 25, event_id: $eventId) {
edges {
node {
id
name
event {
id
name
}
}
}
}
}
"""

test "Can search for an album by event_id" do
{:ok, event} = Fixtures.event()
{:ok, _} = Fixtures.album(%{is_verified: true})
{:ok, _} = Fixtures.album(%{event_id: event.id, is_verified: true})

event_id = Absinthe.Relay.Node.to_global_id(:event, event.id, DojinlistWeb.Schema)

variables = %{
eventId: event_id
}

response =
build_conn()
|> Fixtures.create_and_login_as_admin()
|> post(@endpoint, %{query: @query, variables: variables})
|> json_response(200)

assert %{"data" => %{"albums" => %{"edges" => [edge]}}} = response
assert %{"node" => %{"event" => %{"id" => ^event_id}}} = edge
end
end

0 comments on commit 5e14123

Please sign in to comment.