22093b8ca4
Signed-off-by: Thomas Citharel <tcit@tcit.fr> fix error Signed-off-by: Thomas Citharel <tcit@tcit.fr> Fix tests and warnings Move process_geom/2 to addresses where it belongs Create a special error view for invalid requests Signed-off-by: Thomas Citharel <tcit@tcit.fr> credo fix Signed-off-by: Thomas Citharel <tcit@tcit.fr>
53 lines
1.5 KiB
Elixir
53 lines
1.5 KiB
Elixir
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
|
|
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
|
|
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
|
|
with {:ok, geom} <- Addresses.process_geom(address_params["geom"]) do
|
|
address = Addresses.get_address!(id)
|
|
address_params = %{address_params | "geom" => geom}
|
|
|
|
with {:ok, %Address{} = address} <- Addresses.update_address(address, address_params) do
|
|
render(conn, "show.json", address: address)
|
|
end
|
|
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
|