Allow direct access to draft and scheduled posts by slug
This commit is contained in:
@@ -105,13 +105,13 @@ defmodule Blogex.Blog do
|
||||
|
||||
@doc "Returns a single post by slug/id, or raises."
|
||||
def get_post!(id) do
|
||||
Enum.find(all_posts(), &(&1.id == id)) ||
|
||||
Enum.find(unfiltered_posts(), &(&1.id == id)) ||
|
||||
raise Blogex.NotFoundError, "post #{inspect(id)} not found in #{@blog_id}"
|
||||
end
|
||||
|
||||
@doc "Returns a single post by slug/id, or nil."
|
||||
def get_post(id) do
|
||||
Enum.find(all_posts(), &(&1.id == id))
|
||||
Enum.find(unfiltered_posts(), &(&1.id == id))
|
||||
end
|
||||
|
||||
@doc "Returns paginated posts. Page is 1-indexed."
|
||||
|
||||
@@ -125,10 +125,14 @@ defmodule Blogex.BlogTest do
|
||||
end
|
||||
end
|
||||
|
||||
test "raises for draft post id", %{blog: blog} do
|
||||
assert_raise Blogex.NotFoundError, fn ->
|
||||
blog.get_post!("draft-post")
|
||||
end
|
||||
test "returns a future-dated published post by slug", %{blog: blog} do
|
||||
post = blog.get_post!("future-post")
|
||||
assert post.id == "future-post"
|
||||
end
|
||||
|
||||
test "returns a draft post by slug", %{blog: blog} do
|
||||
post = blog.get_post!("draft-post")
|
||||
assert post.id == "draft-post"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -136,6 +140,14 @@ defmodule Blogex.BlogTest do
|
||||
test "returns nil for unknown id", %{blog: blog} do
|
||||
assert blog.get_post("nope") == nil
|
||||
end
|
||||
|
||||
test "returns a future-dated post", %{blog: blog} do
|
||||
assert %{id: "future-post"} = blog.get_post("future-post")
|
||||
end
|
||||
|
||||
test "returns a draft post", %{blog: blog} do
|
||||
assert %{id: "draft-post"} = blog.get_post("draft-post")
|
||||
end
|
||||
end
|
||||
|
||||
describe "paginate/2" do
|
||||
|
||||
@@ -70,10 +70,10 @@ defmodule Blogex.RouterTest do
|
||||
assert conn.status == 404
|
||||
end
|
||||
|
||||
test "returns 404 for draft post" do
|
||||
test "returns 200 for draft post accessed by slug" do
|
||||
conn = call(:get, "/draft")
|
||||
|
||||
assert conn.status == 404
|
||||
assert conn.status == 200
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -78,12 +78,12 @@ defmodule Blogex.Test.FakeBlog do
|
||||
end
|
||||
|
||||
def get_post!(id) do
|
||||
Enum.find(all_posts(), &(&1.id == id)) ||
|
||||
Enum.find(unfiltered_posts(), &(&1.id == id)) ||
|
||||
raise Blogex.NotFoundError, "post #{inspect(id)} not found"
|
||||
end
|
||||
|
||||
def get_post(id) do
|
||||
Enum.find(all_posts(), &(&1.id == id))
|
||||
Enum.find(unfiltered_posts(), &(&1.id == id))
|
||||
end
|
||||
|
||||
def paginate(page \\ 1, per_page \\ 10) do
|
||||
|
||||
Reference in New Issue
Block a user