Asimov

Asimov

Hi all,

I am learning how to create a basic API with Phoenix (I know, I do not need Phoenix, but I wanted to see how it is actually done). Everything works well but I have not been able to wrap my head around the process to filter results. For instance, my API JSON return is like this:

{
  "data": {
    "active": true,
    "carmaker": "Simca",
    "id": 1,
    "owner": "James Bon",
    "plate": 345671
  }
}

It all goes well if I simply do:

GET /api/customers/ or GET /api/customers/1. But I want to be able to do GET /api/customers?carmaker=carmaker_name

What I have so far is:

router.ex:

defmodule NukakApiWeb.Router do

  use NukakApiWeb, :router
  # use Pow.Phoenix.Router

  pipeline :api do
    plug :accepts, ["json"]
    plug NukakApiWeb.ApiAuthPlug, otp_app:  :nukak_api
  end

scope "/api", NukakApiWeb do
    pipe_through [:api]
    resources "/customers", CustomerController, only: [:show, :index]
    # resources "/customers/:id", CustomerController, :show
 end

end

customer_controller.ex:

defmodule NukakApiWeb.CustomerController do
  use NukakApiWeb, :controller
# I know the following line should better go on a different file. Just adding it here for simplicity.
 def get_customer!(id), do: Repo.get!(Customer, id)
 def list_customers do
   Repo.all(Customer)
end

  def index(conn, _params) do
    customers = list_customers()
    render(conn, "index.json", customers: customers)
  end

  def show(conn, %{"id" => id}) do
    customer = get_customer!(id)
    render(conn, "show.json", customer: customer)
  end

customer_view.ex:

defmodule NukakApiWeb.CustomerView do
  use NukakApiWeb, :view
  alias NukakApiWeb.CustomerView

  def render("index.json", %{customers: customers}) do
    %{data: render_many(customers, CustomerView, "customer.json")}
  end

  def render("show.json", %{customer: customer}) do
    %{data: render_one(customer, CustomerView, "customer.json")}
  end

  def render("customer.json", %{customer: customer}) do
    %{id: customer.id,
      name: customer.name,
      plate: customer.plate,
      carmaker: customer.carmaker,
      active: customer.active}
  end
  end

I tried following this guy idea but I got lost in the assign() function:

def valid_filters(conn, params) do
  filters = Enum.filter(conn.params, fn({k, v}) ->
    Enum.member?(params, k)
  end)
  conn |> assign(:filters, filters)
end

There is the json_api_query_builder package but it does not seem to be compatible with ecto 3.

Anyway, if anybody can give me a hint on what is it that I have to do I would really appreciate it.

Thanks very much!

Showing Posts 1 to 4

crisefd

crisefd

So I think in the controller a actions you can access both body params and query params using the conn value.
Take a look at some of the answers here

Asimov

Asimov OP

Thanks @crisefd. I am trying to understand how I can use it. Due to my lacke of experience, still many concepts are very unclear to me. Actually, I think the concept is quite clear, the actual implementation (to me) is a bit more challenging. I’ll work on it and report if successful. Thanks again.

chasers

chasers

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 :man_shrugging:

Asimov

Asimov OP

Thanks very much @chasers, this is of great help!

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews