Mafinar Khan ◦ Planswell ◦ @mafinar
def vehicle_locations(params) do
with {:ok, vehicles} <- ClientManager.vehicles_for_group(params) do
case vehicles
|> Enum.map(&transform_into_map/1)
|> Enum.map(&fetch_locations/1)
|> Enum.filter(&(&1 != nil))
|> Enum.map(&populate_landmark/1) do
[] -> {:error, :no_location}
locations -> {:ok, locations}
end
else
{:error, _} -> {:error, :no_vehicles}
end
defmodule Wltx.Accounts.User do
@moduledoc """
Module for the authentication table and abstraction
of the Django user model
"""
use Ecto.Schema
schema "setup_appuser" do
field(:username, :string)
field(:password, :string)
field(:first_name, :string)
field(:last_name, :string)
field(:email, :string)
field(:is_superuser, :boolean)
field(:client_group_id, :integer)
field(:tags, {:array, :integer}, virtual: true)
end
end
from(
loc in related_table,
where: loc.lat > 0 and loc.lng > 0,
group_by: [
loc.sysdatetime,
loc.lat,
loc.lng,
loc.speed,
loc.gpstat,
loc.courseval,
loc.engine
],
select: %{
vehicle: fragment("? ::integer", ^id),
actual_time: loc.sysdatetime,
latest_time: fragment("max(sysdatetime)"),
lat: loc.lat,
lng: loc.lng,
speed: fragment("case when max(sysdatetime) = sysdatetime then speed else 0 end"),
gpstat: loc.gpstat,
courseval: loc.courseval,
time_delta:
fragment("extract ('epoch' from (max(sysdatetime) - sysdatetime)::interval)"),
engine: loc.engine
},
limit: 1
)
def signup(struct, params \\ %{}) do
struct
|> cast(params, [:email, :password, :password_confirmation])
|> validate_required([email, :password, :password_confirmation])
|> validate_format(:email, ~r/@/)
|> validate_length(:password, min: 5)
|> password_and_confirmation_matches()
|> generate_password_hash()
end
defp password_and_confirmation_matches(changeset) do
password = get_change(changeset, :password)
password_confirmation = get_change(changeset, :password_confirmation)
if password == password_confirmation do
changeset
else
changeset
|> add_error(:password_confirmation,
"password_confirmation does not match password!")
end
end
defp generate_password_hash(changeset) do
password = get_change(changeset, :password)
hash = Comeonin.Bcrypt.hashpwsalt(password)
changeset |> put_change(:password_hash, hash)
end