mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-01-19 14:17:59 +01:00
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:
parent
b1de5389eb
commit
196c5e14b6
3 changed files with 20 additions and 5 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue