Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(books): add get_book!/1 #354

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions apps/app/lib/app/books.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
13 changes: 13 additions & 0 deletions apps/app/test/app/books_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down