forked from potsda.mn/mobilizon
Add adresses
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
parent
bfcdc38076
commit
07382d6aca
|
@ -223,9 +223,12 @@
|
|||
description: null,
|
||||
floor: null,
|
||||
geo: {
|
||||
type: null,
|
||||
data: {
|
||||
latitude: null,
|
||||
longitude: null,
|
||||
},
|
||||
},
|
||||
addressCountry: null,
|
||||
addressLocality: null,
|
||||
addressRegion: null,
|
||||
|
@ -312,10 +315,13 @@
|
|||
getAddressData: function (addressData) {
|
||||
console.log(addressData);
|
||||
this.event.address = {
|
||||
geo: {
|
||||
geom: {
|
||||
data: {
|
||||
latitude: addressData.latitude,
|
||||
longitude: addressData.longitude,
|
||||
},
|
||||
type: "point",
|
||||
},
|
||||
addressCountry: addressData.country,
|
||||
addressLocality: addressData.locality,
|
||||
addressRegion: addressData.administrative_area_level_1,
|
||||
|
|
31
lib/eventos/addresses/address.ex
Normal file
31
lib/eventos/addresses/address.ex
Normal file
|
@ -0,0 +1,31 @@
|
|||
defmodule Eventos.Addresses.Address do
|
||||
@moduledoc "An address for an event or a group"
|
||||
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
alias Eventos.Addresses.Address
|
||||
alias Eventos.Events.Event
|
||||
alias Eventos.Groups.Group
|
||||
|
||||
schema "addresses" do
|
||||
field :addressCountry, :string
|
||||
field :addressLocality, :string
|
||||
field :addressRegion, :string
|
||||
field :description, :string
|
||||
field :floor, :string
|
||||
field :geom, Geo.Geometry
|
||||
field :postalCode, :string
|
||||
field :streetAddress, :string
|
||||
has_one :event, Event
|
||||
has_one :group, Group
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(%Address{} = address, attrs) do
|
||||
address
|
||||
|> cast(attrs, [:description, :floor, :geom, :addressCountry, :addressLocality, :addressRegion, :postalCode, :streetAddress])
|
||||
|> validate_required([:geom])
|
||||
end
|
||||
end
|
104
lib/eventos/addresses/addresses.ex
Normal file
104
lib/eventos/addresses/addresses.ex
Normal file
|
@ -0,0 +1,104 @@
|
|||
defmodule Eventos.Addresses do
|
||||
@moduledoc """
|
||||
The Addresses context.
|
||||
"""
|
||||
|
||||
import Ecto.Query, warn: false
|
||||
alias Eventos.Repo
|
||||
|
||||
alias Eventos.Addresses.Address
|
||||
|
||||
@doc """
|
||||
Returns the list of addresses.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list_addresses()
|
||||
[%Address{}, ...]
|
||||
|
||||
"""
|
||||
def list_addresses do
|
||||
Repo.all(Address)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a single address.
|
||||
|
||||
Raises `Ecto.NoResultsError` if the Address does not exist.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_address!(123)
|
||||
%Address{}
|
||||
|
||||
iex> get_address!(456)
|
||||
** (Ecto.NoResultsError)
|
||||
|
||||
"""
|
||||
def get_address!(id), do: Repo.get!(Address, id)
|
||||
|
||||
@doc """
|
||||
Creates a address.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> create_address(%{field: value})
|
||||
{:ok, %Address{}}
|
||||
|
||||
iex> create_address(%{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def create_address(attrs \\ %{}) do
|
||||
%Address{}
|
||||
|> Address.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates a address.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> update_address(address, %{field: new_value})
|
||||
{:ok, %Address{}}
|
||||
|
||||
iex> update_address(address, %{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def update_address(%Address{} = address, attrs) do
|
||||
address
|
||||
|> Address.changeset(attrs)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a Address.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> delete_address(address)
|
||||
{:ok, %Address{}}
|
||||
|
||||
iex> delete_address(address)
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def delete_address(%Address{} = address) do
|
||||
Repo.delete(address)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an `%Ecto.Changeset{}` for tracking address changes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_address(address)
|
||||
%Ecto.Changeset{source: %Address{}}
|
||||
|
||||
"""
|
||||
def change_address(%Address{} = address) do
|
||||
Address.changeset(address, %{})
|
||||
end
|
||||
end
|
|
@ -36,13 +36,13 @@ defmodule Eventos.Events.Event do
|
|||
alias Eventos.Events.Event.TitleSlug
|
||||
alias Eventos.Accounts.Account
|
||||
alias Eventos.Groups.Group
|
||||
alias Eventos.Addresses.Address
|
||||
|
||||
schema "events" do
|
||||
field :begins_on, Timex.Ecto.DateTimeWithTimezone
|
||||
field :description, :string
|
||||
field :ends_on, Timex.Ecto.DateTimeWithTimezone
|
||||
field :title, :string
|
||||
field :geom, Geo.Geometry
|
||||
field :slug, TitleSlug.Type
|
||||
field :state, :integer, default: 0
|
||||
field :status, :integer, default: 0
|
||||
|
@ -58,6 +58,7 @@ defmodule Eventos.Events.Event do
|
|||
has_many :event_request, Request
|
||||
has_many :tracks, Track
|
||||
has_many :sessions, Session
|
||||
belongs_to :address, Address
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
@ -65,8 +66,9 @@ defmodule Eventos.Events.Event do
|
|||
@doc false
|
||||
def changeset(%Event{} = event, attrs) do
|
||||
event
|
||||
|> cast(attrs, [:title, :description, :begins_on, :ends_on, :organizer_account_id, :organizer_group_id, :category_id, :state, :geom, :status, :public, :thumbnail, :large_image, :publish_at])
|
||||
|> cast(attrs, [:title, :description, :begins_on, :ends_on, :organizer_account_id, :organizer_group_id, :category_id, :state, :status, :public, :thumbnail, :large_image, :publish_at])
|
||||
|> cast_assoc(:tags)
|
||||
|> cast_assoc(:address)
|
||||
|> validate_required([:title, :description, :begins_on, :ends_on, :organizer_account_id, :category_id])
|
||||
|> TitleSlug.maybe_generate_slug()
|
||||
|> TitleSlug.unique_constraint()
|
||||
|
|
|
@ -43,7 +43,7 @@ defmodule Eventos.Events do
|
|||
"""
|
||||
def get_event_full!(id) do
|
||||
event = Repo.get!(Event, id)
|
||||
Repo.preload(event, [:organizer_account, :organizer_group, :category, :sessions, :tracks, :tags, :participants])
|
||||
Repo.preload(event, [:organizer_account, :organizer_group, :category, :sessions, :tracks, :tags, :participants, :address])
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
|
|
@ -36,6 +36,7 @@ defmodule Eventos.Groups.Group do
|
|||
alias Eventos.Groups.{Group, Member, Request}
|
||||
alias Eventos.Accounts.Account
|
||||
alias Eventos.Groups.Group.TitleSlug
|
||||
alias Eventos.Addresses.Address
|
||||
|
||||
schema "groups" do
|
||||
field :description, :string
|
||||
|
@ -47,6 +48,7 @@ defmodule Eventos.Groups.Group do
|
|||
many_to_many :members, Account, join_through: Member
|
||||
has_many :organized_events, Event, [foreign_key: :organizer_group_id]
|
||||
has_many :requests, Request
|
||||
belongs_to :address, Address
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
@ -54,7 +56,7 @@ defmodule Eventos.Groups.Group do
|
|||
@doc false
|
||||
def changeset(%Group{} = group, attrs) do
|
||||
group
|
||||
|> cast(attrs, [:title, :description, :suspended, :url, :uri])
|
||||
|> cast(attrs, [:title, :description, :suspended, :url, :uri, :address_id])
|
||||
|> validate_required([:title, :description, :suspended, :url, :uri])
|
||||
|> TitleSlug.maybe_generate_slug()
|
||||
|> TitleSlug.unique_constraint()
|
||||
|
|
65
lib/eventos_web/controllers/address_controller.ex
Normal file
65
lib/eventos_web/controllers/address_controller.ex
Normal file
|
@ -0,0 +1,65 @@
|
|||
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
|
||||
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)
|
||||
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, %Address{} = address} <- Addresses.update_address(address, address_params) do
|
||||
render(conn, "show.json", address: address)
|
||||
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
|
|
@ -16,6 +16,7 @@ defmodule EventosWeb.EventController do
|
|||
end
|
||||
|
||||
def create(conn, %{"event" => event_params}) do
|
||||
event_params = %{event_params | "address" => process_address(event_params["address"])}
|
||||
with {:ok, %Event{} = event} <- Events.create_event(event_params) do
|
||||
conn
|
||||
|> put_status(:created)
|
||||
|
@ -24,6 +25,16 @@ defmodule EventosWeb.EventController do
|
|||
end
|
||||
end
|
||||
|
||||
defp process_address(address) do
|
||||
geom = EventosWeb.AddressController.process_geom(address["geom"])
|
||||
case geom do
|
||||
nil ->
|
||||
address
|
||||
_ ->
|
||||
%{address | "geom" => geom}
|
||||
end
|
||||
end
|
||||
|
||||
def show(conn, %{"id" => id}) do
|
||||
event = Events.get_event_full!(id)
|
||||
render(conn, "show.json", event: event)
|
||||
|
|
|
@ -36,6 +36,7 @@ defmodule EventosWeb.Router do
|
|||
resources "/categories", CategoryController, only: [:index, :show]
|
||||
resources "/sessions", SessionController, only: [:index, :show]
|
||||
resources "/tracks", TrackController, only: [:index, :show]
|
||||
resources "/addresses", AddressController, only: [:index, :show]
|
||||
end
|
||||
|
||||
# Other scopes may use custom stacks.
|
||||
|
@ -59,6 +60,7 @@ defmodule EventosWeb.Router do
|
|||
get "/tracks/:id/sessions", SessionController, :show_sessions_for_track
|
||||
resources "/categories", CategoryController
|
||||
resources "/tags", TagController
|
||||
resources "/addresses", AddressController, except: [:index, :show]
|
||||
end
|
||||
|
||||
scope "/", EventosWeb do
|
||||
|
|
40
lib/eventos_web/views/address_view.ex
Normal file
40
lib/eventos_web/views/address_view.ex
Normal file
|
@ -0,0 +1,40 @@
|
|||
defmodule EventosWeb.AddressView do
|
||||
@moduledoc """
|
||||
View for addresses
|
||||
"""
|
||||
|
||||
use EventosWeb, :view
|
||||
alias EventosWeb.AddressView
|
||||
|
||||
def render("index.json", %{addresses: addresses}) do
|
||||
%{data: render_many(addresses, AddressView, "address.json")}
|
||||
end
|
||||
|
||||
def render("show.json", %{address: address}) do
|
||||
%{data: render_one(address, AddressView, "address.json")}
|
||||
end
|
||||
|
||||
def render("address.json", %{address: address}) do
|
||||
%{id: address.id,
|
||||
description: address.description,
|
||||
floor: address.floor,
|
||||
addressCountry: address.addressCountry,
|
||||
addressLocality: address.addressLocality,
|
||||
addressRegion: address.addressRegion,
|
||||
postalCode: address.postalCode,
|
||||
streetAddress: address.streetAddress,
|
||||
geom: render_one(address.geom, AddressView, "geom.json")
|
||||
}
|
||||
end
|
||||
|
||||
def render("geom.json", %{address: %Geo.Point{} = point}) do
|
||||
[lat, lon] = Tuple.to_list(point.coordinates)
|
||||
%{
|
||||
type: "point",
|
||||
data: %{
|
||||
"latitude": lat,
|
||||
"longitude": lon,
|
||||
}
|
||||
}
|
||||
end
|
||||
end
|
|
@ -3,7 +3,7 @@ defmodule EventosWeb.EventView do
|
|||
View for Events
|
||||
"""
|
||||
use EventosWeb, :view
|
||||
alias EventosWeb.{EventView, AccountView, GroupView}
|
||||
alias EventosWeb.{EventView, AccountView, GroupView, AddressView}
|
||||
|
||||
def render("index.json", %{events: events}) do
|
||||
%{data: render_many(events, EventView, "event_simple.json")}
|
||||
|
@ -35,6 +35,7 @@ defmodule EventosWeb.EventView do
|
|||
organizer: render_one(event.organizer_account, AccountView, "acccount_basic.json"),
|
||||
group: render_one(event.organizer_group, GroupView, "group_basic.json"),
|
||||
participants: render_many(event.participants, AccountView, "show_basic.json"),
|
||||
address: render_one(event.address, AddressView, "address.json"),
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
19
priv/repo/migrations/20180110092400_create_addresses.exs
Normal file
19
priv/repo/migrations/20180110092400_create_addresses.exs
Normal file
|
@ -0,0 +1,19 @@
|
|||
defmodule Eventos.Repo.Migrations.CreateAddresses do
|
||||
use Ecto.Migration
|
||||
|
||||
def change do
|
||||
create table(:addresses) do
|
||||
add :description, :string
|
||||
add :floor, :string
|
||||
add :addressCountry, :string
|
||||
add :addressLocality, :string
|
||||
add :addressRegion, :string
|
||||
add :postalCode, :string
|
||||
add :streetAddress, :string
|
||||
add :geom, :geometry
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
end
|
||||
end
|
|
@ -9,6 +9,7 @@ defmodule Eventos.Repo.Migrations.CreateGroups do
|
|||
add :suspended, :boolean, default: false, null: false
|
||||
add :url, :string
|
||||
add :uri, :string
|
||||
add :address_id, references(:addresses, on_delete: :delete_all)
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
|
|
@ -8,7 +8,6 @@ defmodule Eventos.Repo.Migrations.CreateEvents do
|
|||
add :description, :string, null: true
|
||||
add :begins_on, :datetimetz
|
||||
add :ends_on, :datetimetz
|
||||
add :geom, :geometry
|
||||
add :state, :integer, null: false
|
||||
add :public, :boolean, null: false
|
||||
add :status, :integer, null: false
|
||||
|
@ -18,6 +17,7 @@ defmodule Eventos.Repo.Migrations.CreateEvents do
|
|||
add :organizer_account_id, references(:accounts, on_delete: :nothing)
|
||||
add :organizer_group_id, references(:groups, on_delete: :nothing)
|
||||
add :category_id, references(:categories, on_delete: :nothing), null: false
|
||||
add :address_id, references(:addresses, on_delete: :delete_all)
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
|
77
test/eventos/addresses/addresses_test.exs
Normal file
77
test/eventos/addresses/addresses_test.exs
Normal file
|
@ -0,0 +1,77 @@
|
|||
defmodule Eventos.AddressesTest do
|
||||
use Eventos.DataCase
|
||||
|
||||
alias Eventos.Addresses
|
||||
|
||||
describe "addresses" do
|
||||
alias Eventos.Addresses.Address
|
||||
|
||||
@valid_attrs %{addressCountry: "some addressCountry", addressLocality: "some addressLocality", addressRegion: "some addressRegion", description: "some description", floor: "some floor", postalCode: "some postalCode", streetAddress: "some streetAddress", geom: %Geo.Point{coordinates: {10, -10}, srid: 4326}}
|
||||
@update_attrs %{addressCountry: "some updated addressCountry", addressLocality: "some updated addressLocality", addressRegion: "some updated addressRegion", description: "some updated description", floor: "some updated floor", postalCode: "some updated postalCode", streetAddress: "some updated streetAddress", geom: %Geo.Point{coordinates: {20, -20}, srid: 4326}}
|
||||
@invalid_attrs %{addressCountry: nil, addressLocality: nil, addressRegion: nil, description: nil, floor: nil, postalCode: nil, streetAddress: nil, geom: nil}
|
||||
|
||||
def address_fixture(attrs \\ %{}) do
|
||||
{:ok, address} =
|
||||
attrs
|
||||
|> Enum.into(@valid_attrs)
|
||||
|> Addresses.create_address()
|
||||
|
||||
address
|
||||
end
|
||||
|
||||
test "list_addresses/0 returns all addresses" do
|
||||
address = address_fixture()
|
||||
assert Addresses.list_addresses() == [address]
|
||||
end
|
||||
|
||||
test "get_address!/1 returns the address with given id" do
|
||||
address = address_fixture()
|
||||
assert Addresses.get_address!(address.id) == address
|
||||
end
|
||||
|
||||
test "create_address/1 with valid data creates a address" do
|
||||
assert {:ok, %Address{} = address} = Addresses.create_address(@valid_attrs)
|
||||
assert address.addressCountry == "some addressCountry"
|
||||
assert address.addressLocality == "some addressLocality"
|
||||
assert address.addressRegion == "some addressRegion"
|
||||
assert address.description == "some description"
|
||||
assert address.floor == "some floor"
|
||||
assert address.postalCode == "some postalCode"
|
||||
assert address.streetAddress == "some streetAddress"
|
||||
end
|
||||
|
||||
test "create_address/1 with invalid data returns error changeset" do
|
||||
assert {:error, %Ecto.Changeset{}} = Addresses.create_address(@invalid_attrs)
|
||||
end
|
||||
|
||||
test "update_address/2 with valid data updates the address" do
|
||||
address = address_fixture()
|
||||
assert {:ok, address} = Addresses.update_address(address, @update_attrs)
|
||||
assert %Address{} = address
|
||||
assert address.addressCountry == "some updated addressCountry"
|
||||
assert address.addressLocality == "some updated addressLocality"
|
||||
assert address.addressRegion == "some updated addressRegion"
|
||||
assert address.description == "some updated description"
|
||||
assert address.floor == "some updated floor"
|
||||
assert address.postalCode == "some updated postalCode"
|
||||
assert address.streetAddress == "some updated streetAddress"
|
||||
end
|
||||
|
||||
test "update_address/2 with invalid data returns error changeset" do
|
||||
address = address_fixture()
|
||||
assert {:error, %Ecto.Changeset{}} = Addresses.update_address(address, @invalid_attrs)
|
||||
assert address == Addresses.get_address!(address.id)
|
||||
end
|
||||
|
||||
test "delete_address/1 deletes the address" do
|
||||
address = address_fixture()
|
||||
assert {:ok, %Address{}} = Addresses.delete_address(address)
|
||||
assert_raise Ecto.NoResultsError, fn -> Addresses.get_address!(address.id) end
|
||||
end
|
||||
|
||||
test "change_address/1 returns a address changeset" do
|
||||
address = address_fixture()
|
||||
assert %Ecto.Changeset{} = Addresses.change_address(address)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -13,8 +13,12 @@ defmodule Eventos.EventsTest do
|
|||
insert(:account)
|
||||
end
|
||||
|
||||
def address_fixture do
|
||||
insert(:address)
|
||||
end
|
||||
|
||||
def event_fixture do
|
||||
insert(:event, organizer_account: account_fixture())
|
||||
insert(:event)
|
||||
end
|
||||
|
||||
def category_fixture do
|
||||
|
@ -42,9 +46,12 @@ defmodule Eventos.EventsTest do
|
|||
test "create_event/1 with valid data creates a event" do
|
||||
{:ok, account} = Accounts.create_account(@account_valid_attrs)
|
||||
category = category_fixture()
|
||||
valid_attrs_with_account_id = Map.put(@event_valid_attrs, :organizer_account_id, account.id)
|
||||
valid_attrs_with_account_id = Map.put(valid_attrs_with_account_id, :category_id, category.id)
|
||||
assert {:ok, %Event{} = event} = Events.create_event(valid_attrs_with_account_id)
|
||||
address = address_fixture()
|
||||
valid_attrs = Map.put(@event_valid_attrs, :organizer_account_id, account.id)
|
||||
valid_attrs = valid_attrs
|
||||
|> Map.put(:category_id, category.id)
|
||||
|> Map.put(:address_id, address.id)
|
||||
assert {:ok, %Event{} = event} = Events.create_event(valid_attrs)
|
||||
assert event.begins_on == DateTime.from_naive!(~N[2010-04-17 14:00:00.000000Z], "Etc/UTC")
|
||||
assert event.description == "some description"
|
||||
assert event.ends_on == DateTime.from_naive!(~N[2010-04-17 14:00:00.000000Z], "Etc/UTC")
|
||||
|
|
111
test/eventos_web/controllers/address_controller_test.exs
Normal file
111
test/eventos_web/controllers/address_controller_test.exs
Normal file
|
@ -0,0 +1,111 @@
|
|||
defmodule EventosWeb.AddressControllerTest do
|
||||
use EventosWeb.ConnCase
|
||||
|
||||
import Eventos.Factory
|
||||
|
||||
alias Eventos.Addresses
|
||||
alias Eventos.Addresses.Address
|
||||
|
||||
@create_attrs %{addressCountry: "some addressCountry", addressLocality: "some addressLocality", addressRegion: "some addressRegion", description: "some description", floor: "some floor", postalCode: "some postalCode", streetAddress: "some streetAddress", geom: %{type: :point, data: %{latitude: -20, longitude: 30}}}
|
||||
@update_attrs %{addressCountry: "some updated addressCountry", addressLocality: "some updated addressLocality", addressRegion: "some updated addressRegion", description: "some updated description", floor: "some updated floor", postalCode: "some updated postalCode", streetAddress: "some updated streetAddress", geom: %{type: :point, data: %{latitude: -40, longitude: 40}}}
|
||||
@invalid_attrs %{addressCountry: nil, addressLocality: nil, addressRegion: nil, description: nil, floor: nil, postalCode: nil, streetAddress: nil, geom: %{type: nil, data: %{latitude: nil, longitude: nil}}}
|
||||
|
||||
def fixture(:address) do
|
||||
{:ok, address} = Addresses.create_address(@create_attrs)
|
||||
address
|
||||
end
|
||||
|
||||
setup %{conn: conn} do
|
||||
account = insert(:account)
|
||||
user = insert(:user, account: account)
|
||||
{:ok, conn: conn, user: user}
|
||||
end
|
||||
|
||||
describe "index" do
|
||||
test "lists all addresses", %{conn: conn, user: user} do
|
||||
conn = auth_conn(conn, user)
|
||||
conn = get conn, address_path(conn, :index)
|
||||
assert json_response(conn, 200)["data"] == []
|
||||
end
|
||||
end
|
||||
|
||||
describe "create address" do
|
||||
test "renders address when data is valid", %{conn: conn, user: user} do
|
||||
conn = auth_conn(conn, user)
|
||||
conn = post conn, address_path(conn, :create), address: @create_attrs
|
||||
assert %{"id" => id} = json_response(conn, 201)["data"]
|
||||
|
||||
conn = get conn, address_path(conn, :show, id)
|
||||
assert json_response(conn, 200)["data"] == %{
|
||||
"id" => id,
|
||||
"addressCountry" => "some addressCountry",
|
||||
"addressLocality" => "some addressLocality",
|
||||
"addressRegion" => "some addressRegion",
|
||||
"description" => "some description",
|
||||
"floor" => "some floor",
|
||||
"postalCode" => "some postalCode",
|
||||
"streetAddress" => "some streetAddress",
|
||||
"geom" => %{"data" => %{"latitude" => -20.0, "longitude" => 30.0}, "type" => "point"}
|
||||
}
|
||||
end
|
||||
|
||||
test "renders errors when data is invalid", %{conn: conn, user: user} do
|
||||
conn = auth_conn(conn, user)
|
||||
conn = post conn, address_path(conn, :create), address: @invalid_attrs
|
||||
assert json_response(conn, 422)["errors"] != %{}
|
||||
end
|
||||
end
|
||||
|
||||
describe "update address" do
|
||||
setup [:create_address]
|
||||
|
||||
test "renders address when data is valid", %{conn: conn, address: %Address{id: id} = address, user: user} do
|
||||
conn = auth_conn(conn, user)
|
||||
conn = put conn, address_path(conn, :update, address), address: @update_attrs
|
||||
assert %{"id" => ^id} = json_response(conn, 200)["data"]
|
||||
|
||||
conn = get conn, address_path(conn, :show, id)
|
||||
assert json_response(conn, 200)["data"] == %{
|
||||
"id" => id,
|
||||
"addressCountry" => "some updated addressCountry",
|
||||
"addressLocality" => "some updated addressLocality",
|
||||
"addressRegion" => "some updated addressRegion",
|
||||
"description" => "some updated description",
|
||||
"floor" => "some updated floor",
|
||||
"postalCode" => "some updated postalCode",
|
||||
"streetAddress" => "some updated streetAddress",
|
||||
"geom" => %{"data" => %{"latitude" => -40.0, "longitude" => 40.0}, "type" => "point"}
|
||||
}
|
||||
end
|
||||
|
||||
test "renders errors when data is invalid", %{conn: conn, address: address, user: user} do
|
||||
conn = auth_conn(conn, user)
|
||||
conn = put conn, address_path(conn, :update, address), address: @invalid_attrs
|
||||
assert json_response(conn, 422)["errors"] != %{}
|
||||
end
|
||||
end
|
||||
|
||||
describe "delete address" do
|
||||
setup [:create_address]
|
||||
|
||||
test "deletes chosen address", %{conn: conn, address: address, user: user} do
|
||||
conn = auth_conn(conn, user)
|
||||
conn = delete conn, address_path(conn, :delete, address)
|
||||
assert response(conn, 204)
|
||||
assert_error_sent 404, fn ->
|
||||
get conn, address_path(conn, :show, address)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp create_address(_) do
|
||||
{:ok, address: insert(:address)}
|
||||
end
|
||||
|
||||
defp auth_conn(conn, %Eventos.Accounts.User{} = user) do
|
||||
{:ok, token, _claims} = EventosWeb.Guardian.encode_and_sign(user)
|
||||
conn
|
||||
|> put_req_header("authorization", "Bearer #{token}")
|
||||
|> put_req_header("accept", "application/json")
|
||||
end
|
||||
end
|
|
@ -8,13 +8,18 @@ defmodule EventosWeb.EventControllerTest do
|
|||
|
||||
@create_attrs %{begins_on: "2010-04-17 14:00:00.000000Z", description: "some description", ends_on: "2010-04-17 14:00:00.000000Z", title: "some title"}
|
||||
@update_attrs %{begins_on: "2011-05-18 15:01:01.000000Z", description: "some updated description", ends_on: "2011-05-18 15:01:01.000000Z", title: "some updated title"}
|
||||
@invalid_attrs %{begins_on: nil, description: nil, ends_on: nil, title: nil}
|
||||
@invalid_attrs %{begins_on: nil, description: nil, ends_on: nil, title: nil, address_id: nil}
|
||||
@create_address_attrs %{addressCountry: "some addressCountry", addressLocality: "some addressLocality", addressRegion: "some addressRegion", description: "some description", floor: "some floor", postalCode: "some postalCode", streetAddress: "some streetAddress", geom: %{type: :point, data: %{latitude: -20, longitude: 30}}}
|
||||
|
||||
def fixture(:event) do
|
||||
{:ok, event} = Events.create_event(@create_attrs)
|
||||
event
|
||||
end
|
||||
|
||||
def address_fixture do
|
||||
insert(:address)
|
||||
end
|
||||
|
||||
setup %{conn: conn} do
|
||||
account = insert(:account)
|
||||
user = insert(:user, account: account)
|
||||
|
@ -31,6 +36,7 @@ defmodule EventosWeb.EventControllerTest do
|
|||
describe "create event" do
|
||||
test "renders event when data is valid", %{conn: conn, user: user} do
|
||||
attrs = Map.put(@create_attrs, :organizer_account_id, user.account.id)
|
||||
attrs = Map.put(attrs, :address, @create_address_attrs)
|
||||
|
||||
category = insert(:category)
|
||||
attrs = Map.put(attrs, :category_id, category.id)
|
||||
|
@ -39,8 +45,7 @@ defmodule EventosWeb.EventControllerTest do
|
|||
assert %{"id" => id} = json_response(conn, 201)["data"]
|
||||
|
||||
conn = get conn, event_path(conn, :show, id)
|
||||
assert json_response(conn, 200)["data"] == %{
|
||||
"id" => id,
|
||||
assert %{
|
||||
"begins_on" => "2010-04-17T14:00:00Z",
|
||||
"description" => "some description",
|
||||
"ends_on" => "2010-04-17T14:00:00Z",
|
||||
|
@ -50,19 +55,19 @@ defmodule EventosWeb.EventControllerTest do
|
|||
"description" => nil,
|
||||
"display_name" => nil,
|
||||
"domain" => nil,
|
||||
"id" => user.account.id,
|
||||
"suspended" => false,
|
||||
"uri" => "https://",
|
||||
"url" => "https://",
|
||||
"username" => user.account.username
|
||||
},
|
||||
"participants" => []
|
||||
}
|
||||
"participants" => [],
|
||||
"address" => %{"addressCountry" => "some addressCountry", "addressLocality" => "some addressLocality", "addressRegion" => "some addressRegion", "floor" => "some floor", "geom" => %{"data" => %{"latitude" => -20.0, "longitude" => 30.0}, "type" => "point"}, "postalCode" => "some postalCode", "streetAddress" => "some streetAddress"}
|
||||
} = json_response(conn, 200)["data"]
|
||||
end
|
||||
|
||||
test "renders errors when data is invalid", %{conn: conn, user: user} do
|
||||
conn = auth_conn(conn, user)
|
||||
attrs = Map.put(@invalid_attrs, :organizer_account_id, user.account.id)
|
||||
attrs = Map.put(attrs, :address, @create_address_attrs)
|
||||
conn = post conn, event_path(conn, :create), event: attrs
|
||||
assert json_response(conn, 422)["errors"] != %{}
|
||||
end
|
||||
|
@ -84,13 +89,14 @@ defmodule EventosWeb.EventControllerTest do
|
|||
|
||||
test "renders event when data is valid", %{conn: conn, event: %Event{id: id} = event, user: user} do
|
||||
conn = auth_conn(conn, user)
|
||||
address = address_fixture()
|
||||
attrs = Map.put(@update_attrs, :organizer_account_id, user.account.id)
|
||||
attrs = Map.put(attrs, :address_id, address.id)
|
||||
conn = put conn, event_path(conn, :update, event), event: attrs
|
||||
assert %{"id" => ^id} = json_response(conn, 200)["data"]
|
||||
|
||||
conn = get conn, event_path(conn, :show, id)
|
||||
assert json_response(conn, 200)["data"] == %{
|
||||
"id" => id,
|
||||
assert %{
|
||||
"begins_on" => "2011-05-18T15:01:01Z",
|
||||
"description" => "some updated description",
|
||||
"ends_on" => "2011-05-18T15:01:01Z",
|
||||
|
@ -100,14 +106,13 @@ defmodule EventosWeb.EventControllerTest do
|
|||
"description" => nil,
|
||||
"display_name" => nil,
|
||||
"domain" => nil,
|
||||
"id" => user.account.id,
|
||||
"suspended" => false,
|
||||
"uri" => "https://",
|
||||
"url" => "https://",
|
||||
"username" => user.account.username
|
||||
},
|
||||
"participants" => []
|
||||
}
|
||||
"participants" => [],
|
||||
"address" => %{"addressCountry" => "My Country", "addressLocality" => "My Locality", "addressRegion" => "My Region", "floor" => "Myfloor", "geom" => %{"data" => %{"latitude" => 30.0, "longitude" => -90.0}, "type" => "point"}, "postalCode" => "My Postal Code", "streetAddress" => "My Street Address"}
|
||||
} = json_response(conn, 200)["data"]
|
||||
end
|
||||
|
||||
test "renders errors when data is invalid", %{conn: conn, event: event, user: user} do
|
||||
|
|
|
@ -32,6 +32,19 @@ defmodule Eventos.Factory do
|
|||
}
|
||||
end
|
||||
|
||||
def address_factory do
|
||||
%Eventos.Addresses.Address{
|
||||
description: sequence("MyAddress"),
|
||||
geom: %Geo.Point{coordinates: {30, -90}, srid: 4326},
|
||||
floor: "Myfloor",
|
||||
addressCountry: "My Country",
|
||||
addressLocality: "My Locality",
|
||||
addressRegion: "My Region",
|
||||
postalCode: "My Postal Code",
|
||||
streetAddress: "My Street Address"
|
||||
}
|
||||
end
|
||||
|
||||
def event_factory do
|
||||
%Eventos.Events.Event{
|
||||
title: sequence("MyEvent"),
|
||||
|
@ -40,7 +53,8 @@ defmodule Eventos.Factory do
|
|||
begins_on: nil,
|
||||
ends_on: nil,
|
||||
organizer_account: build(:account),
|
||||
category: build(:category)
|
||||
category: build(:category),
|
||||
address: build(:address)
|
||||
}
|
||||
end
|
||||
|
||||
|
@ -58,4 +72,15 @@ defmodule Eventos.Factory do
|
|||
event: build(:event)
|
||||
}
|
||||
end
|
||||
|
||||
def group_factory do
|
||||
%Eventos.Groups.Group{
|
||||
title: sequence("My Group"),
|
||||
description: "My group",
|
||||
suspended: false,
|
||||
url: "https://",
|
||||
uri: "https://",
|
||||
address: build(:address)
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue