diff --git a/lib/philomena_web/controllers/api/json/posts_controller.ex b/lib/philomena_web/controllers/api/json/forum/topic/post_controller.ex similarity index 83% rename from lib/philomena_web/controllers/api/json/posts_controller.ex rename to lib/philomena_web/controllers/api/json/forum/topic/post_controller.ex index 014f567e..c1a1d8ff 100644 --- a/lib/philomena_web/controllers/api/json/posts_controller.ex +++ b/lib/philomena_web/controllers/api/json/forum/topic/post_controller.ex @@ -1,4 +1,4 @@ -defmodule PhilomenaWeb.Api.Json.PostController do +defmodule PhilomenaWeb.Api.Json.Forum.Topic.PostController do use PhilomenaWeb, :controller alias PhilomenaWeb.PostJson @@ -43,7 +43,13 @@ defmodule PhilomenaWeb.Api.Json.PostController do |> preload([:user, :topic]) |> preload([_p, t, _f], topic: t) |> Repo.all() + + case posts do + [] -> + json(conn, %{posts: Enum.map(posts, &PostJson.as_json/1), page: page}) - json(conn, %{posts: Enum.map(posts, &PostJson.as_json/1), page: page, total: hd(posts).topic.post_count}) + _ -> + json(conn, %{posts: Enum.map(posts, &PostJson.as_json/1), page: page, total: hd(posts).topic.post_count}) + end end end diff --git a/lib/philomena_web/controllers/api/json/topics_controller.ex b/lib/philomena_web/controllers/api/json/forum/topic_controller.ex similarity index 95% rename from lib/philomena_web/controllers/api/json/topics_controller.ex rename to lib/philomena_web/controllers/api/json/forum/topic_controller.ex index 5b182ec6..a7cca2f7 100644 --- a/lib/philomena_web/controllers/api/json/topics_controller.ex +++ b/lib/philomena_web/controllers/api/json/forum/topic_controller.ex @@ -1,4 +1,4 @@ -defmodule PhilomenaWeb.Api.Json.TopicController do +defmodule PhilomenaWeb.Api.Json.Forum.TopicController do use PhilomenaWeb, :controller alias PhilomenaWeb.TopicJson diff --git a/lib/philomena_web/router.ex b/lib/philomena_web/router.ex index f94afb3a..db6ec4ce 100644 --- a/lib/philomena_web/router.ex +++ b/lib/philomena_web/router.ex @@ -116,8 +116,8 @@ defmodule PhilomenaWeb.Router do resources "/tags", TagController, only: [:show] resources "/comments", CommentController, only: [:show] resources "/forums", ForumController, only: [:show, :index] do - resources "/topics", TopicController, only: [:show, :index] do - resources "/posts", PostController, only: [:show, :index] + resources "/topics", Forum.TopicController, only: [:show, :index] do + resources "/posts", Forum.Topic.PostController, only: [:show, :index] end end