Skip to content

Commit

Permalink
refactor: add fetch functions to Repo module
Browse files Browse the repository at this point in the history
they work like get functions but always return a tuple {:ok, item} or
{:error, :not_found}

Signed-off-by: Francesco Noacco <francesco.noacco@secomind.com>
  • Loading branch information
noaccOS committed Jul 11, 2023
1 parent 951608b commit 6bb87c0
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 55 deletions.
12 changes: 4 additions & 8 deletions backend/lib/edgehog/astarte.ex
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,7 @@ defmodule Edgehog.Astarte do
"""
def fetch_realm_by_name(realm_name) do
case Repo.get_by(Realm, name: realm_name) do
%Realm{} = realm -> {:ok, realm}
nil -> {:error, :realm_not_found}
end
Repo.fetch_by(Realm, [name: realm_name], empty_return: :realm_not_found)
end

@doc """
Expand Down Expand Up @@ -399,10 +396,9 @@ defmodule Edgehog.Astarte do
"""
def fetch_realm_device(%Realm{id: realm_id}, device_id) do
case Repo.get_by(Device, realm_id: realm_id, device_id: device_id) do
%Device{} = device -> {:ok, device}
nil -> {:error, :device_not_found}
end
Repo.fetch_by(Device, [realm_id: realm_id, device_id: device_id],
empty_return: :device_not_found
)
end

@doc """
Expand Down
13 changes: 4 additions & 9 deletions backend/lib/edgehog/base_images.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,8 @@ defmodule Edgehog.BaseImages do
"""
def fetch_base_image_collection(id) do
case Repo.get(BaseImageCollection, id) do
%BaseImageCollection{} = base_image_collection ->
{:ok, preload_defaults_for_base_image_collection(base_image_collection)}

nil ->
{:error, :not_found}
with {:ok, base_image_collection} <- Repo.fetch(BaseImageCollection, id) do
{:ok, preload_defaults_for_base_image_collection(base_image_collection)}
end
end

Expand Down Expand Up @@ -214,9 +210,8 @@ defmodule Edgehog.BaseImages do
"""
def fetch_base_image(id) do
case Repo.get(BaseImage, id) do
nil -> {:error, :not_found}
%BaseImage{} = base_image -> {:ok, preload_defaults_for_base_image(base_image)}
with {:ok, base_image} <- Repo.fetch(BaseImage, id) do
{:ok, preload_defaults_for_base_image(base_image)}
end
end

Expand Down
16 changes: 4 additions & 12 deletions backend/lib/edgehog/devices.ex
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,8 @@ defmodule Edgehog.Devices do
"""
def fetch_hardware_type(id) do
case Repo.get(HardwareType, id) do
%HardwareType{} = hardware_type ->
{:ok, Repo.preload(hardware_type, :part_numbers)}

nil ->
{:error, :not_found}
with {:ok, hardware_type} <- Repo.fetch(HardwareType, id) do
{:ok, Repo.preload(hardware_type, :part_numbers)}
end
end

Expand Down Expand Up @@ -483,12 +479,8 @@ defmodule Edgehog.Devices do
"""
def fetch_system_model(id) do
case Repo.get(SystemModel, id) do
%SystemModel{} = system ->
{:ok, Repo.preload(system, [:part_numbers, :hardware_type])}

nil ->
{:error, :not_found}
with {:ok, system} <- Repo.fetch(SystemModel, id) do
{:ok, Repo.preload(system, [:part_numbers, :hardware_type])}
end
end

Expand Down
5 changes: 1 addition & 4 deletions backend/lib/edgehog/groups.ex
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@ defmodule Edgehog.Groups do
"""
def fetch_device_group(id) do
case Repo.get(DeviceGroup, id) do
%DeviceGroup{} = device_group -> {:ok, device_group}
nil -> {:error, :not_found}
end
Repo.fetch(DeviceGroup, id)
end

@doc """
Expand Down
18 changes: 18 additions & 0 deletions backend/lib/edgehog/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ defmodule Edgehog.Repo do
Process.get(@tenant_key)
end

def fetch(queryable, id, opts \\ []) do
empty = Keyword.get(opts, :empty_return, :not_found)

case get(queryable, id, opts) do
nil -> {:error, empty}
item -> {:ok, item}
end
end

def fetch_by(queryable, clauses, opts \\ []) do
empty = Keyword.get(opts, :empty_return, :not_found)

case get_by(queryable, clauses, opts) do
nil -> {:error, empty}
item -> {:ok, item}
end
end

@impl true
def prepare_query(_operation, query, opts) do
cond do
Expand Down
5 changes: 1 addition & 4 deletions backend/lib/edgehog/tenants.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ defmodule Edgehog.Tenants do
"""
def fetch_tenant_by_slug(slug) do
case Repo.get_by(Tenant, [slug: slug], skip_tenant_id: true) do
%Tenant{} = tenant -> {:ok, tenant}
nil -> {:error, :not_found}
end
Repo.fetch_by(Tenant, [slug: slug], skip_tenant_id: true)
end

@doc """
Expand Down
24 changes: 6 additions & 18 deletions backend/lib/edgehog/update_campaigns.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,8 @@ defmodule Edgehog.UpdateCampaigns do
"""
def fetch_update_channel(id) do
case Repo.get(UpdateChannel, id) do
nil ->
{:error, :not_found}

%UpdateChannel{} = update_channel ->
{:ok, preload_defaults_for_update_channel(update_channel)}
with {:ok, update_channel} <- Repo.fetch(UpdateChannel, id) do
{:ok, preload_defaults_for_update_channel(update_channel)}
end
end

Expand Down Expand Up @@ -368,12 +364,8 @@ defmodule Edgehog.UpdateCampaigns do
"""
def fetch_update_campaign(id) do
case Repo.get(UpdateCampaign, id) do
nil ->
{:error, :not_found}

%UpdateCampaign{} = update_campaign ->
{:ok, preload_defaults_for_update_campaign(update_campaign)}
with {:ok, update_campaign} <- Repo.fetch(UpdateCampaign, id) do
{:ok, preload_defaults_for_update_campaign(update_campaign)}
end
end

Expand Down Expand Up @@ -492,12 +484,8 @@ defmodule Edgehog.UpdateCampaigns do
"""
def fetch_target(id) do
case Repo.get(Target, id) do
nil ->
{:error, :not_found}

%Target{} = target ->
{:ok, preload_defaults_for_target(target)}
with {:ok, target} <- Repo.fetch(Target, id) do
{:ok, preload_defaults_for_target(target)}
end
end
end

0 comments on commit 6bb87c0

Please sign in to comment.