2018-10-11 17:37:39 +02:00
|
|
|
defmodule MobilizonWeb.AddressController do
|
2018-01-17 11:39:01 +01:00
|
|
|
@moduledoc """
|
|
|
|
A controller for addresses
|
|
|
|
"""
|
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
use MobilizonWeb, :controller
|
2018-01-17 11:39:01 +01:00
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Addresses
|
|
|
|
alias Mobilizon.Addresses.Address
|
2018-01-17 11:39:01 +01:00
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
action_fallback(MobilizonWeb.FallbackController)
|
2018-01-17 11:39:01 +01:00
|
|
|
|
|
|
|
def index(conn, _params) do
|
|
|
|
addresses = Addresses.list_addresses()
|
|
|
|
render(conn, "index.json", addresses: addresses)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create(conn, %{"address" => address_params}) do
|
2018-01-21 19:43:16 +01:00
|
|
|
with {:ok, geom} <- Addresses.process_geom(address_params["geom"]) do
|
|
|
|
address_params = %{address_params | "geom" => geom}
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-01-21 19:43:16 +01:00
|
|
|
with {:ok, %Address{} = address} <- Addresses.create_address(address_params) do
|
|
|
|
conn
|
|
|
|
|> put_status(:created)
|
|
|
|
|> put_resp_header("location", address_path(conn, :show, address))
|
|
|
|
|> render("show.json", address: address)
|
|
|
|
end
|
2018-01-17 11:39:01 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-06 17:42:53 +02:00
|
|
|
def process_geom(%{"type" => type, "data" => data}) do
|
|
|
|
import Logger
|
|
|
|
Logger.debug("Process geom")
|
2018-07-27 10:45:35 +02:00
|
|
|
Logger.debug(inspect(data))
|
|
|
|
Logger.debug(inspect(type))
|
2018-06-06 17:42:53 +02:00
|
|
|
types = [:point]
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-06-06 17:42:53 +02:00
|
|
|
unless is_atom(type) do
|
|
|
|
type = String.to_existing_atom(type)
|
|
|
|
end
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-06-06 17:42:53 +02:00
|
|
|
case type do
|
|
|
|
:point ->
|
|
|
|
%Geo.Point{coordinates: {data["latitude"], data["longitude"]}, srid: 4326}
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-06-06 17:42:53 +02:00
|
|
|
nil ->
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_geom(nil) do
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2018-01-17 11:39:01 +01:00
|
|
|
def show(conn, %{"id" => id}) do
|
|
|
|
address = Addresses.get_address!(id)
|
|
|
|
render(conn, "show.json", address: address)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update(conn, %{"id" => id, "address" => address_params}) do
|
2018-01-21 19:43:16 +01:00
|
|
|
with {:ok, geom} <- Addresses.process_geom(address_params["geom"]) do
|
|
|
|
address = Addresses.get_address!(id)
|
|
|
|
address_params = %{address_params | "geom" => geom}
|
2018-01-17 11:39:01 +01:00
|
|
|
|
2018-01-21 19:43:16 +01:00
|
|
|
with {:ok, %Address{} = address} <- Addresses.update_address(address, address_params) do
|
|
|
|
render(conn, "show.json", address: address)
|
|
|
|
end
|
2018-01-17 11:39:01 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(conn, %{"id" => id}) do
|
|
|
|
address = Addresses.get_address!(id)
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-01-17 11:39:01 +01:00
|
|
|
with {:ok, %Address{}} <- Addresses.delete_address(address) do
|
|
|
|
send_resp(conn, :no_content, "")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|