add tag fetch endpoint

This commit is contained in:
byte[] 2019-12-01 12:26:14 -05:00
parent 25319f1c76
commit ea29cef2a1
3 changed files with 42 additions and 2 deletions

View file

@ -43,9 +43,9 @@ function fetchAndPersistTags(tagIds) {
for (let i = 0; i < tagIds.length; i += chunk) {
const ids = tagIds.slice(i, i + chunk);
/*fetch(`${window.booru.apiEndpoint}tags/fetch_many.json?ids[]=${ids.join('&ids[]=')}`)
fetch(`/tags/fetch?ids[]=${ids.join('&ids[]=')}`)
.then(response => response.json())
.then(data => data.tags.forEach(tag => persistTag(tag)));*/
.then(data => data.tags.forEach(tag => persistTag(tag)));
}
}

View file

@ -0,0 +1,39 @@
defmodule PhilomenaWeb.Tag.FetchController do
use PhilomenaWeb, :controller
alias Philomena.Tags.Tag
alias Philomena.Repo
import Ecto.Query
def index(conn, %{"ids" => ids}) when is_list(ids) do
# limit amount to 50
ids = Enum.take(ids, 50)
tags =
Tag
|> where([t], t.id in ^ids)
|> Repo.all()
|> Enum.map(&tag_json/1)
conn
|> json(%{tags: tags})
end
defp tag_json(tag) do
%{
id: tag.id,
name: tag.name,
images: tag.images_count,
spoiler_image_uri: tag_image(tag)
}
end
defp tag_image(%{image: image}) when image not in [nil, ""],
do: tag_url_root() <> "/" <> image
defp tag_image(_other),
do: nil
defp tag_url_root do
Application.get_env(:philomena, :tag_url_root)
end
end

View file

@ -123,6 +123,7 @@ defmodule PhilomenaWeb.Router do
end
scope "/tags", Tag, as: :tag do
resources "/autocomplete", AutocompleteController, only: [:show], singleton: true
resources "/fetch", FetchController, only: [:index]
end
resources "/tags", TagController, only: [:index, :show]
scope "/search", Search, as: :search do