2018-01-17 10:39:01 +00:00
|
|
|
defmodule EventosWeb.AddressController do
|
|
|
|
@moduledoc """
|
|
|
|
A controller for addresses
|
|
|
|
"""
|
|
|
|
|
|
|
|
use EventosWeb, :controller
|
|
|
|
|
|
|
|
alias Eventos.Addresses
|
|
|
|
alias Eventos.Addresses.Address
|
|
|
|
|
2018-07-27 08:45:35 +00:00
|
|
|
action_fallback(EventosWeb.FallbackController)
|
2018-01-17 10:39:01 +00: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 18:43:16 +00:00
|
|
|
with {:ok, geom} <- Addresses.process_geom(address_params["geom"]) do
|
|
|
|
address_params = %{address_params | "geom" => geom}
|
2018-07-27 08:45:35 +00:00
|
|
|
|
2018-01-21 18:43:16 +00: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 10:39:01 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-06 15:42:53 +00:00
|
|
|
def process_geom(%{"type" => type, "data" => data}) do
|
|
|
|
import Logger
|
|
|
|
Logger.debug("Process geom")
|
2018-07-27 08:45:35 +00:00
|
|
|
Logger.debug(inspect(data))
|
|
|
|
Logger.debug(inspect(type))
|
2018-06-06 15:42:53 +00:00
|
|
|
types = [:point]
|
2018-07-27 08:45:35 +00:00
|
|
|
|
2018-06-06 15:42:53 +00:00
|
|
|
unless is_atom(type) do
|
|
|
|
type = String.to_existing_atom(type)
|
|
|
|
end
|
2018-07-27 08:45:35 +00:00
|
|
|
|
2018-06-06 15:42:53 +00:00
|
|
|
case type do
|
|
|
|
:point ->
|
|
|
|
%Geo.Point{coordinates: {data["latitude"], data["longitude"]}, srid: 4326}
|
2018-07-27 08:45:35 +00:00
|
|
|
|
2018-06-06 15:42:53 +00:00
|
|
|
nil ->
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_geom(nil) do
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2018-01-17 10:39:01 +00: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 18:43:16 +00: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 10:39:01 +00:00
|
|
|
|
2018-01-21 18:43:16 +00:00
|
|
|
with {:ok, %Address{} = address} <- Addresses.update_address(address, address_params) do
|
|
|
|
render(conn, "show.json", address: address)
|
|
|
|
end
|
2018-01-17 10:39:01 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(conn, %{"id" => id}) do
|
|
|
|
address = Addresses.get_address!(id)
|
2018-07-27 08:45:35 +00:00
|
|
|
|
2018-01-17 10:39:01 +00:00
|
|
|
with {:ok, %Address{}} <- Addresses.delete_address(address) do
|
|
|
|
send_resp(conn, :no_content, "")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|