Change ImageJson to view (#62)

This commit is contained in:
liamwhite 2020-03-29 19:39:36 -04:00 committed by GitHub
parent 21df6bc307
commit b1de5389eb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 17 deletions

View file

@ -1,7 +1,6 @@
defmodule PhilomenaWeb.Api.Json.Image.FeaturedController do
use PhilomenaWeb, :controller
alias PhilomenaWeb.ImageJson
alias Philomena.ImageFeatures.ImageFeature
alias Philomena.Images.Image
alias Philomena.Repo
@ -23,7 +22,9 @@ defmodule PhilomenaWeb.Api.Json.Image.FeaturedController do
|> text("")
_ ->
json(conn, %{image: ImageJson.as_json(conn, featured_image)})
conn
|> put_view(PhilomenaWeb.Api.Json.ImageView)
|> render("show.json", image: featured_image)
end
end
end

View file

@ -1,7 +1,6 @@
defmodule PhilomenaWeb.Api.Json.ImageController do
use PhilomenaWeb, :controller
alias PhilomenaWeb.ImageJson
alias Philomena.Images.Image
alias Philomena.Repo
import Ecto.Query
@ -20,7 +19,7 @@ defmodule PhilomenaWeb.Api.Json.ImageController do
|> text("")
_ ->
json(conn, %{image: ImageJson.as_json(conn, image)})
render(conn, "show.json", image: image)
end
end
end

View file

@ -2,7 +2,6 @@ defmodule PhilomenaWeb.Api.Json.Search.ImageController do
use PhilomenaWeb, :controller
alias PhilomenaWeb.ImageLoader
alias PhilomenaWeb.ImageJson
alias Philomena.ImageSorter
alias Philomena.Interactions
alias Philomena.Images.Image
@ -22,11 +21,8 @@ defmodule PhilomenaWeb.Api.Json.Search.ImageController do
interactions = Interactions.user_interactions(images, user)
conn
|> json(%{
images: Enum.map(images, &ImageJson.as_json(conn, &1)),
total: images.total_entries,
interactions: interactions
})
|> put_view(PhilomenaWeb.Api.Json.ImageView)
|> render("index.json", images: images, total: images.total_entries, interactions: interactions)
{:error, msg} ->
conn

View file

@ -2,20 +2,23 @@ defmodule PhilomenaWeb.Api.Json.Search.ReverseController do
use PhilomenaWeb, :controller
alias PhilomenaWeb.ImageReverse
alias PhilomenaWeb.ImageJson
alias Philomena.Interactions
plug :set_scraper_cache
plug PhilomenaWeb.ScraperPlug, params_key: "image", params_name: "image"
def create(conn, %{"image" => image_params}) do
user = conn.assigns.current_user
images =
image_params
|> Map.put("distance", conn.params["distance"])
|> ImageReverse.images()
|> Enum.map(&ImageJson.as_json(conn, &1))
interactions = Interactions.user_interactions(images, user)
conn
|> json(%{images: images})
|> put_view(PhilomenaWeb.Api.Json.ImageView)
|> render("index.json", images: images, total: length(images), interactions: interactions)
end
defp set_scraper_cache(conn, _opts) do

View file

@ -1,7 +1,20 @@
defmodule PhilomenaWeb.ImageJson do
defmodule PhilomenaWeb.Api.Json.ImageView do
use PhilomenaWeb, :view
alias PhilomenaWeb.ImageView
def as_json(_conn, %{hidden_from_users: true, duplicate_id: duplicate_id} = image)
def render("index.json", %{images: images, interactions: interactions, total: total} = assigns) do
%{
images: render_many(images, PhilomenaWeb.Api.Json.ImageView, "image.json", assigns),
interactions: interactions,
total: total
}
end
def render("show.json", %{image: image} = assigns) do
%{image: render_one(image, PhilomenaWeb.Api.Json.ImageView, "image.json", assigns)}
end
def render("image.json", %{image: %{hidden_from_users: true, duplicate_id: duplicate_id} = image})
when not is_nil(duplicate_id) do
%{
id: image.id,
@ -14,7 +27,7 @@ defmodule PhilomenaWeb.ImageJson do
}
end
def as_json(_conn, %{hidden_from_users: true} = image) do
def render("image.json", %{image: %{hidden_from_users: true} = image}) do
%{
id: image.id,
created_at: image.created_at,
@ -26,7 +39,7 @@ defmodule PhilomenaWeb.ImageJson do
}
end
def as_json(conn, %{hidden_from_users: false} = image) do
def render("image.json", %{conn: conn, image: %{hidden_from_users: false} = image}) do
%{
id: image.id,
created_at: image.created_at,