diff --git a/apps/app/lib/app/books.ex b/apps/app/lib/app/books.ex index 09f3af3a..e28ac208 100644 --- a/apps/app/lib/app/books.ex +++ b/apps/app/lib/app/books.ex @@ -14,6 +14,14 @@ defmodule App.Books do ## Database getters + @doc """ + Gets a single book. + + Raises `Ecto.NoResultsError` if the book does not exist. + """ + @spec get_book!(Book.id()) :: Book.t() + def get_book!(id), do: Repo.get!(Book, id) + @doc """ Gets a single book if it belongs to the user. diff --git a/apps/app/test/app/books_test.exs b/apps/app/test/app/books_test.exs index a9f4874b..66f1b7b9 100644 --- a/apps/app/test/app/books_test.exs +++ b/apps/app/test/app/books_test.exs @@ -13,6 +13,19 @@ defmodule App.BooksTest do ## Database getters + describe "get_book!/1" do + test "returns the book" do + %{id: id} = book_fixture() + assert %{id: ^id} = Books.get_book!(id) + end + + test "raises if the book doesn't exist" do + assert_raise Ecto.NoResultsError, fn -> + Books.get_book!(0) + end + end + end + describe "get_book_of_user/2" do setup :book_with_creator_context