From b1de5389ebd7a8d0858bcd061e4a149cb6b12521 Mon Sep 17 00:00:00 2001 From: liamwhite Date: Sun, 29 Mar 2020 19:39:36 -0400 Subject: [PATCH] Change ImageJson to view (#62) --- .../api/json/image/featured_controller.ex | 5 +++-- .../controllers/api/json/image_controller.ex | 3 +-- .../api/json/search/image_controller.ex | 8 ++----- .../api/json/search/reverse_controller.ex | 9 +++++--- .../api/json/image_view.ex} | 21 +++++++++++++++---- 5 files changed, 29 insertions(+), 17 deletions(-) rename lib/philomena_web/{image_json.ex => views/api/json/image_view.ex} (74%) diff --git a/lib/philomena_web/controllers/api/json/image/featured_controller.ex b/lib/philomena_web/controllers/api/json/image/featured_controller.ex index 498d9c13..a35ff00c 100644 --- a/lib/philomena_web/controllers/api/json/image/featured_controller.ex +++ b/lib/philomena_web/controllers/api/json/image/featured_controller.ex @@ -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 diff --git a/lib/philomena_web/controllers/api/json/image_controller.ex b/lib/philomena_web/controllers/api/json/image_controller.ex index ac056cf0..df924a11 100644 --- a/lib/philomena_web/controllers/api/json/image_controller.ex +++ b/lib/philomena_web/controllers/api/json/image_controller.ex @@ -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 diff --git a/lib/philomena_web/controllers/api/json/search/image_controller.ex b/lib/philomena_web/controllers/api/json/search/image_controller.ex index 67d55581..a136e022 100644 --- a/lib/philomena_web/controllers/api/json/search/image_controller.ex +++ b/lib/philomena_web/controllers/api/json/search/image_controller.ex @@ -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 diff --git a/lib/philomena_web/controllers/api/json/search/reverse_controller.ex b/lib/philomena_web/controllers/api/json/search/reverse_controller.ex index f86f48cd..fff51950 100644 --- a/lib/philomena_web/controllers/api/json/search/reverse_controller.ex +++ b/lib/philomena_web/controllers/api/json/search/reverse_controller.ex @@ -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 diff --git a/lib/philomena_web/image_json.ex b/lib/philomena_web/views/api/json/image_view.ex similarity index 74% rename from lib/philomena_web/image_json.ex rename to lib/philomena_web/views/api/json/image_view.ex index 219a45ca..a3bad216 100644 --- a/lib/philomena_web/image_json.ex +++ b/lib/philomena_web/views/api/json/image_view.ex @@ -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,