Repo.all sorting and adding data

Hello!

I am having trouble sorting and displaying data that I get from Repo.all query.
I am getting that kind on info from Repo.all query:

[
  %Parking.Park.ParkingPlace{
    __meta__: #Ecto.Schema.Metadata<:loaded, "parking_places">,
    available_spots: 10,
    id: 1,
    inserted_at: ~N[2020-12-01 19:07:55],
    lat: "xxx",
    location: "zzz",
    long: "yy",
    price_zone: "A",
    reservations: #Ecto.Association.NotLoaded<association :reservations is not loaded>,
    total_spots: 200,
    updated_at: ~N[2020-12-01 19:07:55]
  },
  %Parking.Park.ParkingPlace{
    __meta__: #Ecto.Schema.Metadata<:loaded, "parking_places">,
    available_spots: 50,
    id: 2,
    inserted_at: ~N[2020-12-01 19:07:55],
    lat: "yyy",
    location: "yyy",
    long: "xxx",
    price_zone: "B",
    reservations: #Ecto.Association.NotLoaded<association :reservations is not loaded>,
    total_spots: 50,
    updated_at: ~N[2020-12-01 19:07:55]
  },
  %Parking.Park.ParkingPlace{
    __meta__: #Ecto.Schema.Metadata<:loaded, "parking_places">,
    available_spots: 100,
    id: 3,
    inserted_at: ~N[2020-12-01 19:07:55],
    lat: "ttt",
    location: "uuu",
    long: "vvv",
    price_zone: "A",
    reservations: #Ecto.Association.NotLoaded<association :reservations is not loaded>,
    total_spots: 300,
    updated_at: ~N[2020-12-01 19:07:55]
  },
]

I can get that data on phoenix page with no problem.
Now I would like to add to the query list items distance from my location.
I could add to list end some item with ++[].

Would I need to calculate distance and create in another list or can I add distance to that list item so I could get it in phoenix easily?

Hello and welcome,

Please don’t forget to add code fence ``` around your code… to be more readable. I made the change for this post.

I would not add column, as it can be calculated on the fly.

I would also consider using postgis extension for postgresql to make such calculation trivial :slight_smile:

Define a transient field in your schema to include fields that don’t map to columns in the database

Something like this:

field :distance, :decimal, virtual: true

Afte Repo.all, Iterate over the list replacing this field in each element with the calculated value

Thanks!

For distance I use another api, that gets the data from server.
I got it working like that:

Map.put(:distance_to, distcalc_comes_from_api)
Enum.sort - By distance_to

I don’t know if it is a pretty solution, but it works and I get the distance value to phonenix page.

I’d suggest this as well, as this will also support related concerns like pagination. Doing distance calculation in a second pass outside the db won’t easily do that.

I have one more question about sorting the elements from the list.
I have the same list as above, how could I maintain same structure, but filter out the parkingplaces that have less than 20 available_spots.

I tried to do something like that, but it doesn’t work:

filtered = parking_places |> Enum.filter(&(&1["available_spots"] > 20))

You are mixing string keys with atom keys… it should be

filtered = parking_places |> Enum.filter(&(&1.available_spots > 20))