Skip to content

Commit

Permalink
feat: As a user, I should be able to search a resource by the primary id
Browse files Browse the repository at this point in the history
  • Loading branch information
staylorwr committed May 7, 2021
1 parent ff2cca4 commit c129c97
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/ex_teal/search/simple_search.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ defmodule ExTeal.Search.SimpleSearch do
dynamic([q], ilike(field(q, ^field_name), ^"%#{term}%") or ^dynamic)
end)

dynamic =
case Integer.parse(term) do
{id, ""} ->
dynamic([q], q.id == ^id or ^dynamic)

{_integer, _remainder} ->
dynamic

:error ->
dynamic
end

from(query, where: ^dynamic)
end
end
11 changes: 11 additions & 0 deletions test/ex_teal/resource/index_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ defmodule ExTeal.Resource.IndexTest do
assert u.id == user.id
end

test "can search by primary id" do
[u1, _u2] = insert_pair(:user)

conn = prep_conn(:get, "users", %{"search" => "#{u1.id}"})
response = Index.call(UserResource, conn)
assert 200 == response.status
json = Jason.decode!(response.resp_body, keys: :atoms!)
assert [u] = json[:data]
assert u.id == u1.id
end

@tag manifest: TestExTeal.DefaultManifest
test "can use a many to many relationship to return fields correctly" do
[t1, _t2] = insert_pair(:tag)
Expand Down

0 comments on commit c129c97

Please sign in to comment.