Skip to content

Commit

Permalink
feat: add search series
Browse files Browse the repository at this point in the history
Closes #104
  • Loading branch information
trueChazza committed Jun 10, 2022
1 parent 7d30c2c commit 080a343
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/media_server_web/live/search_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule MediaServerWeb.SearchLive.Index do
use MediaServerWeb, :live_view

alias MediaServerWeb.Repositories.Movies
alias MediaServerWeb.Repositories.Series

@impl true
def handle_params(%{"query" => query}, _url, socket) do
Expand All @@ -10,6 +11,7 @@ defmodule MediaServerWeb.SearchLive.Index do
socket
|> assign(:page_title, query)
|> assign(:movies, Movies.search(query))
|> assign(:series, Series.search(query))
}
end
end
10 changes: 10 additions & 0 deletions lib/media_server_web/live/search_live/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,15 @@
}) %>

<% end %>

<%= for item <- @series do %>

<%= MediaServerWeb.Components.PosterComponent.render(%{
img_src: MediaServerWeb.Repositories.Series.get_poster(item),
title: item["title"],
link: Routes.series_show_path(@socket, :show, item["id"])
}) %>

<% end %>
</div>
</div>
2 changes: 1 addition & 1 deletion lib/media_server_web/repositories/movies.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule MediaServerWeb.Repositories.Movies do
Jason.decode!(body)
end

def handle_response({:error, %HTTPoison.Error{id: nil, reason: :nxdomain}}) do
def handle_response(_) do
[]
end

Expand Down
9 changes: 8 additions & 1 deletion lib/media_server_web/repositories/series.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule MediaServerWeb.Repositories.Series do
Jason.decode!(body)
end

def handle_response({:error, %HTTPoison.Error{id: nil, reason: :nxdomain}}) do
def handle_response(_) do
[]
end

Expand Down Expand Up @@ -47,4 +47,11 @@ defmodule MediaServerWeb.Repositories.Series do
(Stream.filter(serie["images"], fn item -> item["coverType"] === "fanart" end)
|> Enum.at(0))["remoteUrl"]
end

def search(query) do
HTTPoison.get("#{get_url("series/lookup")}&term=#{URI.encode(query)}")
|> handle_response()
|> Stream.filter(fn item -> item["seasonFolder"] end)
|> Enum.sort_by(& &1["title"], :asc)
end
end
20 changes: 19 additions & 1 deletion test/media_server_web/live/search_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule MediaServerWeb.SearchLiveTest do

alias MediaServer.AccountsFixtures
alias MediaServer.MoviesFixtures
alias MediaServer.SeriesFixtures

defp create_fixtures() do
%{
Expand All @@ -17,7 +18,7 @@ defmodule MediaServerWeb.SearchLiveTest do
create_fixtures()
end

test "it can search", %{conn: conn, user: user} do
test "it can search movies", %{conn: conn, user: user} do
conn =
post(conn, Routes.user_session_path(conn, :create), %{
"user" => %{
Expand All @@ -33,5 +34,22 @@ defmodule MediaServerWeb.SearchLiveTest do
assert html =~ "Caminandes Llama Drama"
assert html =~ Routes.movies_show_path(conn, :show, movie["id"])
end

test "it can search series", %{conn: conn, user: user} do
conn =
post(conn, Routes.user_session_path(conn, :create), %{
"user" => %{
"email" => user.email,
"password" => AccountsFixtures.valid_user_password()
}
})

{:ok, _index_live, html} = live(conn, Routes.search_index_path(conn, :index, query: "tvdb:170551"))

serie = SeriesFixtures.get_serie()

assert html =~ "Pioneer One"
assert html =~ Routes.series_show_path(conn, :show, serie["id"])
end
end
end

0 comments on commit 080a343

Please sign in to comment.