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

remove adapter specific bang function #667

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
9 changes: 1 addition & 8 deletions lib/backpex/adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,7 @@ defmodule Backpex.Adapter do

Should return `nil` if no result was found.
"""
@callback get(term(), term(), term()) :: term()

@doc """
Gets a database record with the given primary key value.

Should raise an exception if no result was found.
"""
@callback get!(term(), term(), term()) :: term()
@callback get(term(), map(), module()) :: {:ok, struct() | nil} | {:error, term()}

@doc """
Returns a list of items by given criteria.
Expand Down
15 changes: 0 additions & 15 deletions lib/backpex/adapters/ash.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,6 @@ if Code.ensure_loaded?(Ash) do
|> Ash.read_one()
end

@doc """
Gets a database record with the given primary key value.

Raises an error if no record was found.
"""
@impl Backpex.Adapter
def get!(primary_value, _assigns, live_resource) do
config = live_resource.config(:adapter_config)
primary_key = live_resource.config(:primary_key)

config[:resource]
|> Ash.Query.filter(^Ash.Expr.ref(primary_key) == ^primary_value)
|> Ash.read_one!()
end

@doc """
Returns a list of items by given criteria.
"""
Expand Down
17 changes: 1 addition & 16 deletions lib/backpex/adapters/ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ defmodule Backpex.Adapters.Ecto do

@doc """
Gets a database record with the given primary key value.

Returns `nil` if no result was found.
"""
@impl Backpex.Adapter
def get(primary_value, assigns, live_resource) do
Expand All @@ -76,20 +74,7 @@ defmodule Backpex.Adapters.Ecto do

record_query(primary_value, config[:schema], item_query, live_resource)
|> config[:repo].one()
end

@doc """
Gets a database record with the given primary key value.

Raises `Ecto.NoResultsError` if no record was found.
"""
@impl Backpex.Adapter
def get!(primary_value, assigns, live_resource) do
config = live_resource.config(:adapter_config)
item_query = prepare_item_query(config, assigns)

record_query(primary_value, config[:schema], item_query, live_resource)
|> config[:repo].one!()
|> then(fn result -> {:ok, result} end)
end

@doc """
Expand Down
13 changes: 3 additions & 10 deletions lib/backpex/exceptions.ex
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
defmodule Backpex.NoResourceError do
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NoResourceError is not used anymore, as far as I can see.

defmodule Backpex.NoResultsError do
@moduledoc """
Raised when resource can not be found.

If you are seeing this error, you should check if you provided the correct identifier for the requested resource.
Raised when no results can be found.
"""

defexception message: "Resource not found", plug_status: 404

def exception(opts) do
name = Keyword.fetch!(opts, :name)
%__MODULE__{message: "no resource found for:\n\n#{inspect(name)}"}
end
defexception message: "No results could be found", plug_status: 404
end

defmodule Backpex.ForbiddenError do
Expand Down
2 changes: 1 addition & 1 deletion lib/backpex/live_resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,7 @@ defmodule Backpex.LiveResource do
%{live_resource: live_resource, live_action: live_action} = socket.assigns

item_primary_value = primary_value(socket, item)
item = Resource.get(item_primary_value, socket.assigns, live_resource)
{:ok, item} = Resource.get(item_primary_value, socket.assigns, live_resource)

socket =
cond do
Expand Down
18 changes: 10 additions & 8 deletions lib/backpex/resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,37 @@ defmodule Backpex.Resource do
@doc """
Gets a database record with the given `fields` by the given `primary_value`.

Raises `Ecto.NoResultsError` if no record was found.
Returns `{:ok, nil}` if no result was found.

## Parameters

* `primary_value`: The identifier for the specific item to be fetched.
* `assigns` (map): The current assigns of the socket.
* `live_resource` (module): The `Backpex.LiveResource` module.
"""
def get!(primary_value, assigns, live_resource) do
def get(primary_value, assigns, live_resource) do
adapter = live_resource.config(:adapter)

adapter.get!(primary_value, assigns, live_resource)
adapter.get(primary_value, assigns, live_resource)
end

@doc """
Gets a database record with the given `fields` by the given `primary_value`.

Returns `nil` if no result was found.
Raises `Backpex.NoResultsError` if no record was found.

## Parameters

* `primary_value`: The identifier for the specific item to be fetched.
* `assigns` (map): The current assigns of the socket.
* `live_resource` (module): The `Backpex.LiveResource` module.
"""
def get(primary_value, assigns, live_resource) do
adapter = live_resource.config(:adapter)

adapter.get!(primary_value, assigns, live_resource)
def get!(primary_value, assigns, live_resource) do
case get(primary_value, assigns, live_resource) do
{:ok, nil} -> raise Backpex.NoResultsError
{:ok, result} -> result
{:error, _error} -> raise Backpex.NoResultsError
end
Comment on lines +69 to +73
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Flo0807 Like this? I think we should work with result tuples everywhere (like Ash for example).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

end

@doc """
Expand Down