From 6ccb3f645ad80a68e1d9b8049218b10e0a6ad824 Mon Sep 17 00:00:00 2001 From: SomewhatDamaged Date: Wed, 1 Jan 2020 01:01:14 +1100 Subject: [PATCH] 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. --- .../controllers/api/json/tag_controller.ex | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/philomena_web/controllers/api/json/tag_controller.ex b/lib/philomena_web/controllers/api/json/tag_controller.ex index c045661f..c761d5e6 100644 --- a/lib/philomena_web/controllers/api/json/tag_controller.ex +++ b/lib/philomena_web/controllers/api/json/tag_controller.ex @@ -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 \ No newline at end of file +end