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 alias Eventos.Addresses.Address
import Logger
@geom_types [:point]
@doc """ @doc """
Returns the list of addresses. Returns the list of addresses.
@ -101,4 +105,36 @@ defmodule Eventos.Addresses do
def change_address(%Address{} = address) do def change_address(%Address{} = address) do
Address.changeset(address, %{}) Address.changeset(address, %{})
end 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 end

View file

@ -16,7 +16,8 @@ defmodule EventosWeb.AddressController do
end end
def create(conn, %{"address" => address_params}) do def create(conn, %{"address" => address_params}) do
address_params = %{address_params | "geom" => process_geom(address_params["geom"])} 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 with {:ok, %Address{} = address} <- Addresses.create_address(address_params) do
conn conn
|> put_status(:created) |> put_status(:created)
@ -24,22 +25,6 @@ defmodule EventosWeb.AddressController do
|> render("show.json", address: address) |> render("show.json", address: address)
end 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 end
def show(conn, %{"id" => id}) do def show(conn, %{"id" => id}) do
@ -48,13 +33,15 @@ defmodule EventosWeb.AddressController do
end end
def update(conn, %{"id" => id, "address" => address_params}) do 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 = Addresses.get_address!(id)
address_params = %{address_params | "geom" => process_geom(address_params["geom"])} address_params = %{address_params | "geom" => geom}
with {:ok, %Address{} = address} <- Addresses.update_address(address, address_params) do with {:ok, %Address{} = address} <- Addresses.update_address(address, address_params) do
render(conn, "show.json", address: address) render(conn, "show.json", address: address)
end end
end end
end
def delete(conn, %{"id" => id}) do def delete(conn, %{"id" => id}) do
address = Addresses.get_address!(id) address = Addresses.get_address!(id)

View file

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

View file

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

View file

@ -12,6 +12,12 @@ defmodule EventosWeb.FallbackController do
|> render(EventosWeb.ChangesetView, "error.json", changeset: changeset) |> render(EventosWeb.ChangesetView, "error.json", changeset: changeset)
end 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 def call(conn, {:error, :not_found}) do
conn conn
|> put_status(:not_found) |> put_status(:not_found)

View file

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

View file

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

View file

@ -73,5 +73,15 @@ defmodule Eventos.AddressesTest do
address = address_fixture() address = address_fixture()
assert %Ecto.Changeset{} = Addresses.change_address(address) assert %Ecto.Changeset{} = Addresses.change_address(address)
end 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
end end