Somewhat damaged patch 1 (#19)

* Fix for module placement

Correcting my own mistake when I first made these routes

* Correcting module location

* Correcting module location

* Fix

Fixes a potential crash when no posts are returned.
This commit is contained in:
SomewhatDamaged 2020-01-07 18:02:49 +11:00 committed by liamwhite
parent 0c5e0d7fa7
commit e918375c83
3 changed files with 11 additions and 5 deletions

View file

@ -1,4 +1,4 @@
defmodule PhilomenaWeb.Api.Json.PostController do
defmodule PhilomenaWeb.Api.Json.Forum.Topic.PostController do
use PhilomenaWeb, :controller
alias PhilomenaWeb.PostJson
@ -44,6 +44,12 @@ defmodule PhilomenaWeb.Api.Json.PostController do
|> preload([_p, t, _f], topic: t)
|> Repo.all()
json(conn, %{posts: Enum.map(posts, &PostJson.as_json/1), page: page, total: hd(posts).topic.post_count})
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})
end
end
end

View file

@ -1,4 +1,4 @@
defmodule PhilomenaWeb.Api.Json.TopicController do
defmodule PhilomenaWeb.Api.Json.Forum.TopicController do
use PhilomenaWeb, :controller
alias PhilomenaWeb.TopicJson

View file

@ -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