Skip to content

Commit

Permalink
Remove get_by_slug! methot/change get! method to accept slugs
Browse files Browse the repository at this point in the history
  • Loading branch information
DouglasLutz committed Mar 18, 2020
1 parent 014a304 commit 6288667
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 59 deletions.
36 changes: 8 additions & 28 deletions lib/companies/companies.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,38 +90,18 @@ defmodule Companies.Companies do
** (Ecto.NoResultsError)
"""
def get!(id, opts \\ []) do
def get!(key, opts \\ []) do
preloads = Keyword.get(opts, :preloads, [])

from(c in Company)
|> preload(^preloads)
|> from()
|> where([c], is_nil(c.removed_pending_change_id))
|> Repo.get!(id)
end

@doc """
Gets a single company.
Raises `Ecto.NoResultsError` if the Company does not exist.
## Examples
iex> get!("Valid slug")
%Company{}
iex> get!("Invalid slug")
** (Ecto.NoResultsError)
query = from(c in Company) |> preload(^preloads) |> from() |> where([c], is_nil(c.removed_pending_change_id))

"""
def get_by_slug!(slug, opts \\ []) do
preloads = Keyword.get(opts, :preloads, [])
final_query =
case Integer.parse(key) do
:error -> where(query, [c], c.slug == ^key)
{int_id, _remainder} -> where(query, [c], c.id == ^int_id or c.slug == ^key)
end

from(c in Company)
|> preload(^preloads)
|> from()
|> where([c], is_nil(c.removed_pending_change_id))
|> Repo.get_by!(slug: slug)
Repo.one!(final_query)
end

@doc """
Expand Down
8 changes: 4 additions & 4 deletions lib/companies_web/controllers/company_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ defmodule CompaniesWeb.CompanyController do
end

def show(conn, %{"slug" => slug}) do
company = Companies.get_by_slug!(slug, preloads: [:jobs, :industry])
company = Companies.get!(slug, preloads: [:jobs, :industry])
render(conn, "show.html", company: company)
end

def edit(conn, %{"slug" => slug}) do
company = Companies.get_by_slug!(slug)
company = Companies.get!(slug)
changeset = Companies.change(company)
industries = Industries.all()
render(conn, "edit.html", company: company, changeset: changeset, industries: industries)
end

def update(conn, %{"slug" => slug, "company" => company_params}) do
company = Companies.get_by_slug!(slug)
company = Companies.get!(slug)

case Companies.update(company, company_params, current_user(conn)) do
{:ok, _company} ->
Expand All @@ -65,7 +65,7 @@ defmodule CompaniesWeb.CompanyController do
end

def delete(conn, %{"slug" => slug}) do
company = Companies.get_by_slug!(slug)
company = Companies.get!(slug)
{:ok, _company} = Companies.delete(company, current_user(conn))

conn
Expand Down
35 changes: 8 additions & 27 deletions test/companies/companies_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -66,41 +66,22 @@ defmodule Companies.CompaniesTest do
end

describe "get!/2" do
test "retrieves a company by id" do
%{id: id} = insert(:company, name: "ZULU")

assert %{id: ^id} = Companies.get!(id)
end

test "preloads given associations" do
company = insert(:company, name: "ZULU")

assert %{jobs: []} = Companies.get!(company.id, preloads: [:jobs])
end

test "raises for unknown id" do
assert_raise Ecto.NoResultsError, fn ->
Companies.get!(1000, preloads: [:jobs])
end
end
end

describe "get_by_slug!/2" do
test "retrieves a company by it's name" do
test "retrieves a company by given key" do
%{id: id, slug: slug} = insert(:company, name: "ZULU")

assert %{id: ^id} = Companies.get_by_slug!(slug)
assert %{id: ^id} = Companies.get!("#{id}")
assert %{id: ^id} = Companies.get!(slug)
end

test "preloads given associations" do
company = insert(:company, name: "ZULU")

assert %{jobs: []} = Companies.get_by_slug!(company.slug, preloads: [:jobs])
assert %{jobs: []} = Companies.get!("#{company.id}", preloads: [:jobs])
end

test "raises for unknown slug" do
test "raises for unknown id" do
assert_raise Ecto.NoResultsError, fn ->
Companies.get_by_slug!("invalid-slug", preloads: [:jobs])
Companies.get!("#{1000}", preloads: [:jobs])
end
end
end
Expand All @@ -109,13 +90,13 @@ defmodule Companies.CompaniesTest do
test "retrieves by id" do
%{id: company_id} = insert(:company)

assert %{id: ^company_id} = Companies.get!(company_id)
assert %{id: ^company_id} = Companies.get!("#{company_id}")
end

test "does not retrieve deleted record" do
company = insert(:company, %{removed_pending_change: build(:pending_change)})

assert_raise Ecto.NoResultsError, fn -> Companies.get!(company.id) end
assert_raise Ecto.NoResultsError, fn -> Companies.get!("#{company.id}") end
end
end

Expand Down

0 comments on commit 6288667

Please sign in to comment.