You’re close! Add these to your CustomerController:
import Ecto.Query
# Correct, this should go in the `Customer` context along with the `Ecto.Query` import.
def get_customers_by(keyword) do
query =
from c in Customer,
where: keyword,
select: c
Repo.all(query)
end
def show(conn, %{"carmaker" => carmaker}) do
customers = get_customers_by(carmaker: carmaker)
response = Jason.encode!(customers)
# This is what I've done, but I don't know views at all.
conn
|> put_resp_content_type("application/json")
|> send_resp(200, response)
end
Validating filters is a good idea but random filters just won’t match anything currently. Do you want people to be able to build queries dynamically via the API?
Looks cool but once you get the hang of it building an API like this is fun and quick to do yourself, plus you’ll get to know Phoenix and Ecto much better. Plus you may not want people doing dynamic queries ![]()






















