Skip to content

Commit

Permalink
Merge pull request #428 from midarrlabs/feature/add-api-search
Browse files Browse the repository at this point in the history
Add API search
  • Loading branch information
trueChazza authored Nov 24, 2023
2 parents 2d7bdd0 + 345ce82 commit 54fced8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/media_server_web/controllers/search_controller.ex
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
2 changes: 2 additions & 0 deletions lib/media_server_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ defmodule MediaServerWeb.Router do

get "/subtitle", SubtitleController, :index

get "/search", SearchController, :index

post "/webhooks/:id", WebhooksController, :create
end

Expand Down
17 changes: 17 additions & 0 deletions test/media_server_web/controllers/search_controller_test.exs
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

0 comments on commit 54fced8

Please sign in to comment.