Merge branch 'fix-some-warnings' into 'master'

Fix some warnings

See merge request tcit/eventos!8
This commit is contained in:
Benjamin Bouvier 2018-01-27 18:35:32 +01:00
commit 6d893504f6
8 changed files with 77 additions and 34 deletions

View File

@ -8,6 +8,10 @@ defmodule Eventos.Addresses do
alias Eventos.Addresses.Address
import Logger
@geom_types [:point]
@doc """
Returns the list of addresses.
@ -101,4 +105,36 @@ defmodule Eventos.Addresses do
def change_address(%Address{} = address) do
Address.changeset(address, %{})
end
@doc """
Processes raw geo data informations and return a `Geo` geometry which can be one of `Geo.Point`.
"""
def process_geom(%{"type" => type_input, "data" => data}) do
type =
if !is_atom(type_input) && type_input != nil do
try do
String.to_existing_atom(type_input)
rescue
e in ArgumentError ->
Logger.error("#{type_input} is not an existing atom : #{inspect e}")
nil
end
else
type_input
end
if Enum.member?(@geom_types, type) do
case type do
:point ->
{:ok, %Geo.Point{coordinates: {data["latitude"], data["longitude"]}, srid: 4326}}
end
else
{:error, nil}
end
end
@doc false
def process_geom(nil) do
{:error, nil}
end
end

View File

@ -16,43 +16,30 @@ defmodule EventosWeb.AddressController do
end
def create(conn, %{"address" => address_params}) do
address_params = %{address_params | "geom" => process_geom(address_params["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)
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 process_geom(%{"type" => type, "data" => data}) do
types = [:point]
unless is_atom(type) do
type = String.to_existing_atom(type)
end
case type do
:point ->
%Geo.Point{coordinates: {data["latitude"], data["longitude"]}, srid: 4326}
nil ->
nil
end
end
def process_geom(nil) do
nil
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
address = Addresses.get_address!(id)
address_params = %{address_params | "geom" => process_geom(address_params["geom"])}
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)
with {:ok, %Address{} = address} <- Addresses.update_address(address, address_params) do
render(conn, "show.json", address: address)
end
end
end

View File

@ -7,6 +7,7 @@ defmodule EventosWeb.EventController do
alias Eventos.Events
alias Eventos.Events.Event
alias Eventos.Export.ICalendar
alias Eventos.Addresses
action_fallback EventosWeb.FallbackController
@ -26,12 +27,11 @@ defmodule EventosWeb.EventController do
end
defp process_address(address) do
geom = EventosWeb.AddressController.process_geom(address["geom"])
case geom do
nil ->
address
_ ->
case Addresses.process_geom(address["geom"]) do
{:ok, geom} ->
%{address | "geom" => geom}
_ ->
address
end
end

View File

@ -5,7 +5,7 @@ defmodule EventosWeb.EventRequestController do
use EventosWeb, :controller
alias Eventos.Events
alias Eventos.Events.{Event, Request}
alias Eventos.Events.Request
action_fallback EventosWeb.FallbackController

View File

@ -12,6 +12,12 @@ defmodule EventosWeb.FallbackController do
|> render(EventosWeb.ChangesetView, "error.json", changeset: changeset)
end
def call(conn, {:error, nil}) do
conn
|> put_status(:unprocessable_entity)
|> render(EventosWeb.ErrorView, "invalid_request.json")
end
def call(conn, {:error, :not_found}) do
conn
|> put_status(:not_found)

View File

@ -49,7 +49,7 @@ defmodule EventosWeb.Router do
resources "/accounts", AccountController, except: [:new, :edit]
resources "/events", EventController
post "/events/:id/request", EventRequestController, :create_for_event
resources "/participant", ParticipantController
resources "/participants", ParticipantController
resources "/requests", EventRequestController
resources "/groups", GroupController, except: [:index, :show]
post "/groups/:id/request", GroupRequestController, :create_for_group

View File

@ -8,6 +8,10 @@ defmodule EventosWeb.ErrorView do
"Page not found"
end
def render("invalid_request.json", _assigns) do
%{errors: "Invalid request"}
end
def render("500.html", _assigns) do
"Internal server error"
end

View File

@ -73,5 +73,15 @@ defmodule Eventos.AddressesTest do
address = address_fixture()
assert %Ecto.Changeset{} = Addresses.change_address(address)
end
test "process_geom/2 with valid data returns a Point element" do
attrs = %{"type" => "point", "data" => %{"latitude" => 10, "longitude" => -10}}
assert {:ok, %Geo.Point{}} = Addresses.process_geom(attrs)
end
test "process_geom/2 with invalid data returns nil" do
attrs = %{"type" => "linfdfsfe", "data" => %{"latitude" => 10, "longitude" => -10}}
assert {:error, nil} = Addresses.process_geom(attrs)
end
end
end