API additions for user interactions on single images (#63)

* API additions for user interactions on single images

* removed ability to render without supplying interactions
This commit is contained in:
SomewhatDamaged 2020-03-30 11:51:35 +11:00 committed by GitHub
parent b1de5389eb
commit 196c5e14b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 5 deletions

View file

@ -3,10 +3,13 @@ defmodule PhilomenaWeb.Api.Json.Image.FeaturedController do
alias Philomena.ImageFeatures.ImageFeature
alias Philomena.Images.Image
alias Philomena.Interactions
alias Philomena.Repo
import Ecto.Query
def show(conn, _params) do
user = conn.assigns.current_user
featured_image =
Image
|> join(:inner, [i], f in ImageFeature, on: [image_id: i.id])
@ -22,9 +25,11 @@ defmodule PhilomenaWeb.Api.Json.Image.FeaturedController do
|> text("")
_ ->
interactions = Interactions.user_interactions([featured_image], user)
conn
|> put_view(PhilomenaWeb.Api.Json.ImageView)
|> render("show.json", image: featured_image)
|> render("show.json", image: featured_image, interactions: interactions)
end
end
end

View file

@ -2,10 +2,13 @@ defmodule PhilomenaWeb.Api.Json.ImageController do
use PhilomenaWeb, :controller
alias Philomena.Images.Image
alias Philomena.Interactions
alias Philomena.Repo
import Ecto.Query
def show(conn, %{"id" => id}) do
user = conn.assigns.current_user
image =
Image
|> where(id: ^id)
@ -19,7 +22,9 @@ defmodule PhilomenaWeb.Api.Json.ImageController do
|> text("")
_ ->
render(conn, "show.json", image: image)
interactions = Interactions.user_interactions([image], user)
render(conn, "show.json", image: image, interactions: interactions)
end
end
end

View file

@ -10,11 +10,16 @@ defmodule PhilomenaWeb.Api.Json.ImageView do
}
end
def render("show.json", %{image: image} = assigns) do
%{image: render_one(image, PhilomenaWeb.Api.Json.ImageView, "image.json", assigns)}
def render("show.json", %{image: image, interactions: interactions} = assigns) do
%{
image: render_one(image, PhilomenaWeb.Api.Json.ImageView, "image.json", assigns),
interactions: interactions
}
end
def render("image.json", %{image: %{hidden_from_users: true, duplicate_id: duplicate_id} = image})
def render("image.json", %{
image: %{hidden_from_users: true, duplicate_id: duplicate_id} = image
})
when not is_nil(duplicate_id) do
%{
id: image.id,