Corrects failure and redirect to main page (#12)

This stops a bug I noticed when a slug that doesn't match is given. This will correctly 404 rather than redirecting to front page.
This commit is contained in:
SomewhatDamaged 2020-01-01 01:01:14 +11:00 committed by liamwhite
parent f5bd8e7614
commit 6ccb3f645a

View file

@ -3,11 +3,24 @@ defmodule PhilomenaWeb.Api.Json.TagController do
alias PhilomenaWeb.TagJson
alias Philomena.Tags.Tag
alias Philomena.Repo
import Ecto.Query
plug PhilomenaWeb.RecodeParameterPlug, [name: "id"] when action in [:show]
plug :load_resource, model: Tag, id_field: "slug", persisted: true, preload: [:aliased_tag, :aliases, :implied_tags, :implied_by_tags, :dnp_entries]
def show(conn, %{"id" => slug}) do
tag =
Tag
|> where(slug: ^slug)
|> preload([:aliased_tag, :aliases, :implied_tags, :implied_by_tags, :dnp_entries])
|> Repo.one()
def show(conn, _params) do
json(conn, %{tag: TagJson.as_json(conn.assigns.tag)})
case tag do
nil ->
conn
|> put_status(:not_found)
|> text("")
_ ->
json(conn, %{tag: TagJson.as_json(tag)})
end
end
end
end