-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #428 from midarrlabs/feature/add-api-search
Add API search
- Loading branch information
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
defmodule MediaServerWeb.SearchController do | ||
use MediaServerWeb, :controller | ||
|
||
def index(conn, %{"query" => query}) do | ||
movies = | ||
Scrivener.paginate( | ||
MediaServer.MoviesIndex.search(MediaServer.MoviesIndex.all(), query), | ||
%{ | ||
"page" => "1", | ||
"page_size" => "50" | ||
} | ||
) | ||
|
||
conn | ||
|> put_resp_header("content-type", "application/json") | ||
|> send_resp( | ||
200, | ||
Jason.encode!(%{ | ||
"total" => movies.total_entries, | ||
"items" => | ||
Enum.map(movies.entries, fn x -> | ||
%{ | ||
"title" => x["title"], | ||
"overview" => x["overview"], | ||
"year" => x["year"], | ||
"poster" => ~p"/api/images?movie=#{x["id"]}&type=poster", | ||
"background" => ~p"/api/images?movie=#{x["id"]}&type=background", | ||
"stream" => ~p"/api/stream?movie=#{x["id"]}" | ||
} | ||
end) | ||
}) | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
test/media_server_web/controllers/search_controller_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule SearchControllerTest do | ||
use MediaServerWeb.ConnCase | ||
|
||
use Plug.Test | ||
|
||
setup do | ||
%{user: MediaServer.AccountsFixtures.user_fixture()} | ||
end | ||
|
||
test "it should search", %{conn: conn, user: user} do | ||
conn = get(conn, ~p"/api/search?query=Caminandes&token=#{user.api_token.token}") | ||
|
||
assert conn.status === 200 | ||
|
||
assert conn.resp_body === "{\"items\":[{\"background\":\"/api/images?movie=3&type=background\",\"overview\":\"In this episode of the Caminandes cartoon series we learn to know our hero Koro even better! It's winter in Patagonia, food is getting scarce. Koro the Llama engages with Oti the pesky penguin in an epic fight over that last tasty berry. This short animation film was made with Blender and funded by the subscribers of the Blender Cloud platform. Already since 2007, Blender Institute successfully combines film and media production with improving a free and open source 3D creation pipeline.\",\"poster\":\"/api/images?movie=3&type=poster\",\"stream\":\"/api/stream?movie=3\",\"title\":\"Caminandes: Llamigos\",\"year\":2016},{\"background\":\"/api/images?movie=2&type=background\",\"overview\":\"A young llama named Koro discovers that the grass is always greener on the other side (of the fence).\",\"poster\":\"/api/images?movie=2&type=poster\",\"stream\":\"/api/stream?movie=2\",\"title\":\"Caminandes: Gran Dillama\",\"year\":2013},{\"background\":\"/api/images?movie=1&type=background\",\"overview\":\"Koro wants to get to the other side of the road.\",\"poster\":\"/api/images?movie=1&type=poster\",\"stream\":\"/api/stream?movie=1\",\"title\":\"Caminandes: Llama Drama\",\"year\":2013}],\"total\":3}" | ||
end | ||
end |