Featured image API route (#18)

* Add route

`/api/v1/json/features`

Shows current featured image

* Add route

`/api/v1/json/features`

Shows current featured image.

* Removed binding

* Moving this file to a more appropriate name.

* Correctly named and placed

Correctly named and placed to match its module name

* Updated per review

* Changed as per review

Renamed to `PhilomenaWeb.Api.Json.FeaturedController`
Moved to `lib/philomena_web/controllers/api/json/featured_controller.ex`

* Updated route

Route `/api/v1/json/images/featured` now uses `PhilomenaWeb.Api.Json.FeaturedController`

* like this

Co-authored-by: liamwhite <liamwhite@users.noreply.github.com>
This commit is contained in:
SomewhatDamaged 2020-01-06 12:09:45 +11:00 committed by liamwhite
parent a98e268195
commit 24b9863699
2 changed files with 32 additions and 0 deletions

View file

@ -0,0 +1,29 @@
defmodule PhilomenaWeb.Api.Json.Image.FeaturedController do
use PhilomenaWeb, :controller
alias PhilomenaWeb.ImageJson
alias Philomena.ImageFeatures.ImageFeature
alias Philomena.Images.Image
alias Philomena.Repo
import Ecto.Query
def show(conn, _params) do
featured_image =
Image
|> join(:inner, [i], f in ImageFeature, on: [image_id: i.id])
|> order_by([_i, f], desc: f.created_at)
|> limit(1)
|> preload([:tags, :user, :intensity])
|> Repo.one()
case featured_image do
nil ->
conn
|> put_status(:not_found)
|> text("")
_ ->
json(conn, %{image: ImageJson.as_json(conn, featured_image)})
end
end
end

View file

@ -95,6 +95,9 @@ defmodule PhilomenaWeb.Router do
scope "/api/v1/json", PhilomenaWeb.Api.Json, as: :api_json do scope "/api/v1/json", PhilomenaWeb.Api.Json, as: :api_json do
pipe_through [:accepts_json, :api, :ensure_tor_authorized] pipe_through [:accepts_json, :api, :ensure_tor_authorized]
scope "/images", Image, as: :image do
resources "/featured", FeaturedController, only: [:show], singleton: true
end
resources "/images", ImageController, only: [:show] resources "/images", ImageController, only: [:show]
scope "/search", Search, as: :search do scope "/search", Search, as: :search do