From da98cee97bd9158351fa1ab0ca289c109774662a Mon Sep 17 00:00:00 2001 From: Douglas Lutz Date: Wed, 18 Mar 2020 13:50:05 -0300 Subject: [PATCH] Remove get_by_slug! methot/change get! method to accept slugs --- lib/companies/companies.ex | 36 +++++-------------- .../controllers/company_controller.ex | 8 ++--- test/companies/companies_test.exs | 35 +++++------------- 3 files changed, 20 insertions(+), 59 deletions(-) diff --git a/lib/companies/companies.ex b/lib/companies/companies.ex index 407ea9c0..f7984649 100644 --- a/lib/companies/companies.ex +++ b/lib/companies/companies.ex @@ -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 """ diff --git a/lib/companies_web/controllers/company_controller.ex b/lib/companies_web/controllers/company_controller.ex index de4ddc88..bc5bb539 100644 --- a/lib/companies_web/controllers/company_controller.ex +++ b/lib/companies_web/controllers/company_controller.ex @@ -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} -> @@ -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 diff --git a/test/companies/companies_test.exs b/test/companies/companies_test.exs index e6d1b422..99218f72 100644 --- a/test/companies/companies_test.exs +++ b/test/companies/companies_test.exs @@ -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 @@ -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