Sakthibalan

Sakthibalan

How to create custom AshJsonApi to get a user by two parameter

How do create custom api to get a user by their :first_name and :date_of_birth?

read :get_user do
  argument :first_name, :string do
    allow_nil? false
  end

  argument :date_of_birth, :date do
    allow_nil? false
  end
end

Most Liked

shankardevy

shankardevy

In my Repo module, I have the following:

defmodule Hello.Repo do
  use AshPostgres.Repo,
    otp_app: :hello

  # Installs Postgres extensions that ash commonly uses
  def installed_extensions do
    ["ash-functions"]
  end
end

Are you using Ash 3 or 2? I think this got added recently in Ash 3. Official instructions are here: Get Started With Postgres — ash_postgres v2.10.0

shankardevy

shankardevy

There isn’t much information in your post so I’m assuming you have setup the AshJsonAPI as described here: Getting started with AshJsonApi — ash_json_api v1.7.0 . With that assumption, I’m guessing this is what you need on the User resource to get you the API you need.

defmodule Hello.User do
  use Ash.Resource,
    data_layer: AshPostgres.DataLayer,
    domain: Hello.Users,
    extensions: [AshJsonApi.Resource]

  postgres do
    table "users"
    repo Hello.Repo
  end

  attributes do
    uuid_primary_key :id

    attribute :first_name, :string, public?: true
    attribute :date_of_birth, :date, public?: true
    .... other attributes
  end

  json_api do
    type "user"

    routes do
      base "/users"

      get :get_user, route: "/find"
    end
  end

  actions do
    read :get_user do
      argument :first_name, :string do
        allow_nil? false
      end

      argument :date_of_birth, :date do
        allow_nil? false
      end

      filter expr(first_name == ^arg(:first_name) && date_of_birth == ^arg(:date_of_birth))
    end
  end

end

Basically, you are missing the filter macro in your read action. In the get route, the first argument :get_user is the read action name defined inside actions and the route option is given to make url as GET /users/find, otherwise the default route for a get path will result in /users/:id where :id is a placeholder and is not relevant in your case.

The API will return all the fields marked as public?: true in the attributes section.

Last Post!

Sakthibalan

Sakthibalan

Thank you so much.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42533 114
New

We're in Beta

About us Mission Statement