defp locations(conn,
%{"id": id, "lat": lat, "lng": lng) do
tablename = from(
v in "vehicle",
select: [:tablename], Repo.get()
)
query =
from(
p in tablename,
select: %{
name: p.name,
distance:
fragment(
"ST_Distance(
the_geom,
ST_MakePoint(?, ?)::geography)",
^lng,
^lat
)
},
where: not is_nil(p.name),
order_by: [asc: 2],
limit: ^limit
)
locations = query |> Repo.all()
render(conn, "locations.json", locations: locations)
end
def location(conn, %{"id": id}, _current_user) do
locations = id
|> LocationService.get_vehicle()
|> LocationService.get_locations_for()
render(conn, "locations.json", locations: locations)
end
defmodule Wltx.DB.Vehicle do
@moduledoc """
Schema for the vehicle/asset table
"""
use Ecto.Schema
schema "asset_vehicle" do
field(:license_plate, :string)
field(:vehicle_type_id, :integer)
field(:group_id, :integer)
field(:tag_id, :integer)
field(:code, :string)
field(:model, :string)
field(:color, :string)
field(:device_id, :integer)
field(:tablename
, :string)
end
end
defmodule Wltx.Vehicle do
defstruct
license_plate: "",
attributes: %AssetAttributes{},
related_table: "",
group: %Group{},
device: %Device{},
can_be_tracked: false,
is_whitelabelled: false
end