mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 13:47:58 +01:00
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:
parent
f5bd8e7614
commit
6ccb3f645a
1 changed files with 18 additions and 5 deletions
|
@ -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
|
Loading…
Reference in a new issue