2018-01-17 11:39:01 +01:00
|
|
|
defmodule EventosWeb.AddressController do
|
|
|
|
@moduledoc """
|
|
|
|
A controller for addresses
|
|
|
|
"""
|
|
|
|
|
|
|
|
use EventosWeb, :controller
|
|
|
|
|
|
|
|
alias Eventos.Addresses
|
|
|
|
alias Eventos.Addresses.Address
|
|
|
|
|
|
|
|
action_fallback EventosWeb.FallbackController
|
|
|
|
|
|
|
|
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}
|
|
|
|
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
|
|
|
|
|
|
|
|
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)
|
|
|
|
with {:ok, %Address{}} <- Addresses.delete_address(address) do
|
|
|
|
send_resp(conn, :no_content, "")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|