Fix front-end, allow events to be created by a group, allow to get sessions from an event

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2018-01-16 19:45:09 +01:00
parent 7a98674e59
commit 67ef32432e
29 changed files with 278 additions and 82 deletions

View File

@ -24,16 +24,6 @@ export default function eventFetch(url, store, optionsarg = {}) {
return fetch(link, options).then((response) => { return fetch(link, options).then((response) => {
if (response.ok) return response; if (response.ok) return response;
return response throw response.text();
.json()
.then((json) => {
const error = json['hydra:description'] ? json['hydra:description'] : response.statusText;
if (!json.violations) throw Error(error);
// const errors = { _error: error };
// json.violations.map(violation => errors[violation.propertyPath] = violation.message);
// throw errors;
});
}); });
} }

View File

@ -261,7 +261,8 @@
// '@type': 'Tag', // '@type': 'Tag',
}); });
}); });
this.event.organizer_id = this.$store.state.user.account.id; this.event.category_id = this.event.category.id;
this.event.organizer_account_id = this.$store.state.user.account.id;
this.event.participants = [this.$store.state.user.account.id]; this.event.participants = [this.$store.state.user.account.id];
this.event.price = parseFloat(this.event.price); this.event.price = parseFloat(this.event.price);

View File

@ -2,8 +2,9 @@
<v-container> <v-container>
<v-layout row> <v-layout row>
<v-flex xs12 sm6 offset-sm3> <v-flex xs12 sm6 offset-sm3>
<span v-if="error">Error : event not found</span>
<v-progress-circular v-if="loading" indeterminate color="primary"></v-progress-circular> <v-progress-circular v-if="loading" indeterminate color="primary"></v-progress-circular>
<v-card v-if="!loading"> <v-card v-if="!loading && !error">
<v-layout column class="media"> <v-layout column class="media">
<v-card-title> <v-card-title>
<v-btn icon @click="$router.go(-1)"> <v-btn icon @click="$router.go(-1)">
@ -32,12 +33,27 @@
<v-card-title class="pl-5 pt-5"> <v-card-title class="pl-5 pt-5">
<div class="display-1 pl-5 pt-5">{{ event.title }}</div> <div class="display-1 pl-5 pt-5">{{ event.title }}</div>
</v-card-title> </v-card-title>
<p><router-link :to="{ name: 'Account', params: {id: event.organizer.id} }"><span class="grey--text">{{ event.organizer.username }}</span></router-link> organises {{ event.title }} <span v-if="event.address.addressLocality">in {{ event.address.addressLocality }}</span> on the {{ event.startDate | formatDate }}.</p> <!--<p><router-link :to="{ name: 'Account', params: {id: event.organizer.id} }"><span class="grey&#45;&#45;text">{{ event.organizer.username }}</span></router-link> organises {{ event.title }} <span v-if="event.address.addressLocality">in {{ event.address.addressLocality }}</span> on the {{ event.startDate | formatDate }}.</p>
<v-card-text v-if="event.description"><vue-markdown :source="event.description"></vue-markdown></v-card-text> <v-card-text v-if="event.description"><vue-markdown :source="event.description"></vue-markdown></v-card-text>-->
</div> </div>
<v-container fluid grid-list-md v-if="event.participants.length > 0"> <v-container fluid grid-list-md>
<v-subheader>Membres</v-subheader> <v-subheader>Membres</v-subheader>
<v-layout row> <v-layout row>
<v-flex xs2>
<router-link :to="{name: 'Account', params: {'id': event.organizer.id}}">
<v-avatar size="75px">
<img v-if="!event.organizer.avatarRemoteUrl"
class="img-circle elevation-7 mb-1"
src="http://lorempixel.com/125/125/"
>
<img v-else
class="img-circle elevation-7 mb-1"
:src="event.organizer.avatarRemoteUrl"
>
</v-avatar>
</router-link>
Organisateur <span>{{ event.organizer.username }}</span>
</v-flex>
<v-flex xs2 v-for="account in event.participants" :key="account.id"> <v-flex xs2 v-for="account in event.participants" :key="account.id">
<router-link :to="{name: 'Account', params: {'id': account.id}}"> <router-link :to="{name: 'Account', params: {'id': account.id}}">
<v-avatar size="75px"> <v-avatar size="75px">
@ -79,6 +95,7 @@
data() { data() {
return { return {
loading: true, loading: true,
error: false,
event: { event: {
id: this.id, id: this.id,
title: '', title: '',
@ -95,7 +112,6 @@
deleteEvent() { deleteEvent() {
const router = this.$router; const router = this.$router;
eventFetch(`/events/${this.id}`, this.$store, { method: 'DELETE' }) eventFetch(`/events/${this.id}`, this.$store, { method: 'DELETE' })
.then(response => response.json())
.then(() => router.push({'name': 'EventList'})); .then(() => router.push({'name': 'EventList'}));
}, },
fetchData() { fetchData() {
@ -103,8 +119,14 @@
.then(response => response.json()) .then(response => response.json())
.then((data) => { .then((data) => {
this.loading = false; this.loading = false;
this.event = data; this.event = data.data;
}); }).catch((res) => {
Promise.resolve(res).then((data) => {
console.log(data);
this.error = true;
this.loading = false;
})
});
}, },
joinEvent() { joinEvent() {
eventFetch(`/events/${this.id}/join`, this.$store) eventFetch(`/events/${this.id}/join`, this.$store)
@ -121,7 +143,7 @@
}); });
}, },
downloadIcsEvent() { downloadIcsEvent() {
eventFetch('/events/' + this.event.id + '/export', this.$store, {responseType: 'arraybuffer'}) eventFetch('/events/' + this.event.id + '/ics', this.$store, {responseType: 'arraybuffer'})
.then((response) => response.text()) .then((response) => response.text())
.then(response => { .then(response => {
const blob = new Blob([response],{type: 'text/calendar'}); const blob = new Blob([response],{type: 'text/calendar'});

View File

@ -42,7 +42,7 @@
<v-select <v-select
v-bind:items="categories" v-bind:items="categories"
v-model="group.category" v-model="group.category"
item-text="name" item-text="title"
item-value="@id" item-value="@id"
label="Categories" label="Categories"
single-line single-line
@ -86,7 +86,7 @@
create() { create() {
// this.group.organizer = "/accounts/" + this.$store.state.user.id; // this.group.organizer = "/accounts/" + this.$store.state.user.id;
eventFetch('/groups', this.$store, { method: 'POST', body: JSON.stringify(this.group) }) eventFetch('/groups', this.$store, { method: 'POST', body: JSON.stringify({group: this.group}) })
.then(response => response.json()) .then(response => response.json())
.then((data) => { .then((data) => {
this.loading = false; this.loading = false;
@ -98,7 +98,7 @@
.then(response => response.json()) .then(response => response.json())
.then((data) => { .then((data) => {
this.loading = false; this.loading = false;
this.categories = data; this.categories = data.data;
}); });
}, },
getAddressData: function (addressData) { getAddressData: function (addressData) {

View File

@ -69,21 +69,21 @@
</v-list-tile-content> </v-list-tile-content>
</v-list-tile> </v-list-tile>
</v-list> </v-list>
<v-container fluid grid-list-md v-if="group.groupAccounts.length > 0"> <v-container fluid grid-list-md v-if="group.members.length > 0">
<v-subheader>Membres</v-subheader> <v-subheader>Membres</v-subheader>
<v-layout row> <v-layout row>
<v-flex xs2 v-for="groupAccount in group.groupAccounts" :key="groupAccount.id"> <v-flex xs2 v-for="member in group.members" :key="member.id">
<router-link :to="{name: 'Account', params: {'id': groupAccount.account.id}}"> <router-link :to="{name: 'Account', params: {'id': member.account.id}}">
<v-badge overlap> <v-badge overlap>
<span slot="badge" v-if="groupAccount.role == 3"><v-icon>stars</v-icon></span> <span slot="badge" v-if="member.role == 3"><v-icon>stars</v-icon></span>
<v-avatar size="75px"> <v-avatar size="75px">
<img v-if="!groupAccount.account.avatarRemoteUrl" <img v-if="!member.account.avatarRemoteUrl"
class="img-circle elevation-7 mb-1" class="img-circle elevation-7 mb-1"
src="http://lorempixel.com/125/125/" src="http://lorempixel.com/125/125/"
> >
<img v-else <img v-else
class="img-circle elevation-7 mb-1" class="img-circle elevation-7 mb-1"
:src="groupAccount.account.avatarRemoteUrl" :src="member.account.avatarRemoteUrl"
> >
</v-avatar> </v-avatar>
</v-badge> </v-badge>
@ -162,7 +162,7 @@
.then(response => response.json()) .then(response => response.json())
.then((data) => { .then((data) => {
this.loading = false; this.loading = false;
this.group = data; this.group = data.data;
}); });
}, },
deleteGroup() { deleteGroup() {

View File

@ -58,7 +58,7 @@
.then(response => response.json()) .then(response => response.json())
.then((data) => { .then((data) => {
this.loading = false; this.loading = false;
this.groups = data; this.groups = data.data;
}); });
}, },
deleteEvent(id) { deleteEvent(id) {

View File

@ -18,7 +18,7 @@ defmodule Eventos.Accounts.Account do
field :uri, :string field :uri, :string
field :url, :string field :url, :string
field :username, :string field :username, :string
has_many :organized_events, Event, [foreign_key: :organizer_id] has_many :organized_events, Event, [foreign_key: :organizer_account_id]
many_to_many :groups, Group, join_through: Member many_to_many :groups, Group, join_through: Member
has_many :group_request, Request has_many :group_request, Request
has_one :user, User has_one :user, User

View File

@ -35,6 +35,7 @@ defmodule Eventos.Events.Event do
alias Eventos.Events.{Event, Participant, Request, Tag, Category, Session, Track} alias Eventos.Events.{Event, Participant, Request, Tag, Category, Session, Track}
alias Eventos.Events.Event.TitleSlug alias Eventos.Events.Event.TitleSlug
alias Eventos.Accounts.Account alias Eventos.Accounts.Account
alias Eventos.Groups.Group
schema "events" do schema "events" do
field :begins_on, Timex.Ecto.DateTimeWithTimezone field :begins_on, Timex.Ecto.DateTimeWithTimezone
@ -49,7 +50,8 @@ defmodule Eventos.Events.Event do
field :thumbnail, :string field :thumbnail, :string
field :large_image, :string field :large_image, :string
field :publish_at, Timex.Ecto.DateTimeWithTimezone field :publish_at, Timex.Ecto.DateTimeWithTimezone
belongs_to :organizer, Account, [foreign_key: :organizer_id] belongs_to :organizer_account, Account, [foreign_key: :organizer_account_id]
belongs_to :organizer_group, Group, [foreign_key: :organizer_group_id]
many_to_many :tags, Tag, join_through: "events_tags" many_to_many :tags, Tag, join_through: "events_tags"
belongs_to :category, Category belongs_to :category, Category
many_to_many :participants, Account, join_through: Participant many_to_many :participants, Account, join_through: Participant
@ -63,9 +65,9 @@ defmodule Eventos.Events.Event do
@doc false @doc false
def changeset(%Event{} = event, attrs) do def changeset(%Event{} = event, attrs) do
event event
|> cast(attrs, [:title, :description, :begins_on, :ends_on, :organizer_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, :geom, :status, :public, :thumbnail, :large_image, :publish_at])
|> cast_assoc(:tags) |> cast_assoc(:tags)
|> validate_required([:title, :description, :begins_on, :ends_on, :organizer_id, :category_id]) |> validate_required([:title, :description, :begins_on, :ends_on, :organizer_account_id, :category_id])
|> TitleSlug.maybe_generate_slug() |> TitleSlug.maybe_generate_slug()
|> TitleSlug.unique_constraint() |> TitleSlug.unique_constraint()
end end

View File

@ -7,6 +7,7 @@ defmodule Eventos.Events do
alias Eventos.Repo alias Eventos.Repo
alias Eventos.Events.Event alias Eventos.Events.Event
alias Eventos.Accounts.Account
@doc """ @doc """
Returns the list of events. Returns the list of events.
@ -37,6 +38,14 @@ defmodule Eventos.Events do
""" """
def get_event!(id), do: Repo.get!(Event, id) def get_event!(id), do: Repo.get!(Event, id)
@doc """
Gets a single event, with all associations loaded.
"""
def get_event_full!(id) do
event = Repo.get!(Event, id)
Repo.preload(event, [:organizer_account, :organizer_group, :category, :sessions, :tracks, :tags, :participants])
end
@doc """ @doc """
Creates a event. Creates a event.
@ -407,6 +416,10 @@ defmodule Eventos.Events do
Repo.all(Request) Repo.all(Request)
end end
def list_requests_for_account(%Account{} = account) do
Repo.all(from r in Request, where: r.account_id == ^account.id)
end
@doc """ @doc """
Gets a single request. Gets a single request.
@ -503,6 +516,20 @@ defmodule Eventos.Events do
Repo.all(Session) Repo.all(Session)
end end
@doc """
Returns the list of sessions for an event
"""
def list_sessions_for_event(event_id) do
Repo.all(from s in Session, where: s.event_id == ^event_id)
end
@doc """
Returns the list of sessions for a track
"""
def list_sessions_for_track(track_id) do
Repo.all(from s in Session, where: s.track_id == ^track_id)
end
@doc """ @doc """
Gets a single session. Gets a single session.

View File

@ -32,6 +32,7 @@ defmodule Eventos.Groups.Group do
""" """
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
alias Eventos.Events.Event
alias Eventos.Groups.{Group, Member, Request} alias Eventos.Groups.{Group, Member, Request}
alias Eventos.Accounts.Account alias Eventos.Accounts.Account
alias Eventos.Groups.Group.TitleSlug alias Eventos.Groups.Group.TitleSlug
@ -43,7 +44,8 @@ defmodule Eventos.Groups.Group do
field :slug, TitleSlug.Type field :slug, TitleSlug.Type
field :uri, :string field :uri, :string
field :url, :string field :url, :string
many_to_many :accounts, Account, join_through: Member many_to_many :members, Account, join_through: Member
has_many :organized_events, Event, [foreign_key: :organizer_group_id]
has_many :requests, Request has_many :requests, Request
timestamps() timestamps()

View File

@ -37,6 +37,14 @@ defmodule Eventos.Groups do
""" """
def get_group!(id), do: Repo.get!(Group, id) def get_group!(id), do: Repo.get!(Group, id)
@doc """
Gets a single group, with all associations loaded.
"""
def get_group_full!(id) do
group = Repo.get!(Group, id)
Repo.preload(group, [:members, :organized_events])
end
@doc """ @doc """
Creates a group. Creates a group.

View File

@ -20,12 +20,12 @@ defmodule EventosWeb.EventController do
conn conn
|> put_status(:created) |> put_status(:created)
|> put_resp_header("location", event_path(conn, :show, event)) |> put_resp_header("location", event_path(conn, :show, event))
|> render("show.json", event: event) |> render("show_simple.json", event: event)
end end
end end
def show(conn, %{"id" => id}) do def show(conn, %{"id" => id}) do
event = Events.get_event!(id) event = Events.get_event_full!(id)
render(conn, "show.json", event: event) render(conn, "show.json", event: event)
end end
@ -40,7 +40,7 @@ defmodule EventosWeb.EventController do
event = Events.get_event!(id) event = Events.get_event!(id)
with {:ok, %Event{} = event} <- Events.update_event(event, event_params) do with {:ok, %Event{} = event} <- Events.update_event(event, event_params) do
render(conn, "show.json", event: event) render(conn, "show_simple.json", event: event)
end end
end end

View File

@ -0,0 +1,52 @@
defmodule EventosWeb.EventRequestController do
@moduledoc """
Controller for Event requests
"""
use EventosWeb, :controller
alias Eventos.Events
alias Eventos.Events.{Event, Request}
action_fallback EventosWeb.FallbackController
def index_for_user(conn, _params) do
account = Guardian.Plug.current_resource(conn).account
requests = Events.list_requests_for_account(account)
render(conn, "index.json", requests: requests)
end
def create(conn, %{"request" => request_params}) do
request_params = Map.put(request_params, "account_id", Guardian.Plug.current_resource(conn).account.id)
with {:ok, %Request{} = request} <- Events.create_request(request_params) do
conn
|> put_status(:created)
|> put_resp_header("location", event_request_path(conn, :show, request))
|> render("show.json", request: request)
end
end
def create_for_event(conn, %{"request" => request_params, "id" => event_id}) do
request_params = Map.put(request_params, "event_id", event_id)
create(conn, request_params)
end
def show(conn, %{"id" => id}) do
request = Events.get_request!(id)
render(conn, "show.json", request: request)
end
def update(conn, %{"id" => id, "request" => request_params}) do
request = Events.get_request!(id)
with {:ok, %Request{} = request} <- Events.update_request(request, request_params) do
render(conn, "show.json", request: request)
end
end
def delete(conn, %{"id" => id}) do
request = Events.get_request!(id)
with {:ok, %Request{}} <- Events.delete_request(request) do
send_resp(conn, :no_content, "")
end
end
end

View File

@ -15,16 +15,18 @@ defmodule EventosWeb.GroupController do
end end
def create(conn, %{"group" => group_params}) do def create(conn, %{"group" => group_params}) do
group_params = Map.put(group_params, "uri", "h")
group_params = Map.put(group_params, "url", "h")
with {:ok, %Group{} = group} <- Groups.create_group(group_params) do with {:ok, %Group{} = group} <- Groups.create_group(group_params) do
conn conn
|> put_status(:created) |> put_status(:created)
|> put_resp_header("location", group_path(conn, :show, group)) |> put_resp_header("location", group_path(conn, :show, group))
|> render("show.json", group: group) |> render("show_simple.json", group: group)
end end
end end
def show(conn, %{"id" => id}) do def show(conn, %{"id" => id}) do
group = Groups.get_group!(id) group = Groups.get_group_full!(id)
render(conn, "show.json", group: group) render(conn, "show.json", group: group)
end end
@ -32,7 +34,7 @@ defmodule EventosWeb.GroupController do
group = Groups.get_group!(id) group = Groups.get_group!(id)
with {:ok, %Group{} = group} <- Groups.update_group(group, group_params) do with {:ok, %Group{} = group} <- Groups.update_group(group, group_params) do
render(conn, "show.json", group: group) render(conn, "show_simple.json", group: group)
end end
end end

View File

@ -28,6 +28,16 @@ defmodule EventosWeb.SessionController do
render(conn, "show.json", session: session) render(conn, "show.json", session: session)
end end
def show_sessions_for_event(conn, %{"id" => event_id}) do
sessions = Events.list_sessions_for_event(event_id)
render(conn, "index.json", sessions: sessions)
end
def show_sessions_for_track(conn, %{"id" => track}) do
sessions = Events.list_sessions_for_track(track)
render(conn, "index.json", sessions: sessions)
end
def update(conn, %{"id" => id, "session" => session_params}) do def update(conn, %{"id" => id, "session" => session_params}) do
session = Events.get_session!(id) session = Events.get_session!(id)

View File

@ -28,6 +28,9 @@ defmodule EventosWeb.Router do
post "/login", UserSessionController, :sign_in post "/login", UserSessionController, :sign_in
resources "/groups", GroupController, only: [:index, :show] resources "/groups", GroupController, only: [:index, :show]
resources "/events", EventController, only: [:index, :show] resources "/events", EventController, only: [:index, :show]
get "/events/:id/ics", EventController, :export_to_ics
get "/events/:id/tracks", TrackController, :show_tracks_for_event
get "/events/:id/sessions", SessionController, :show_sessions_for_event
resources "/accounts", AccountController, only: [:index, :show] resources "/accounts", AccountController, only: [:index, :show]
resources "/tags", TagController, only: [:index, :show] resources "/tags", TagController, only: [:index, :show]
resources "/categories", CategoryController, only: [:index, :show] resources "/categories", CategoryController, only: [:index, :show]
@ -44,16 +47,18 @@ defmodule EventosWeb.Router do
resources "/users", UserController, except: [:new, :edit, :show] resources "/users", UserController, except: [:new, :edit, :show]
resources "/accounts", AccountController, except: [:new, :edit] resources "/accounts", AccountController, except: [:new, :edit]
resources "/events", EventController resources "/events", EventController
get "/events/:id/ics", EventController, :export_to_ics post "/events/:id/request", EventRequestController, :create_for_event
resources "/participant", ParticipantController
resources "/requests", EventRequestController
resources "/groups", GroupController, except: [:index, :show]
post "/groups/:id/request", GroupRequestController, :create_for_group
resources "/members", MemberController
resources "/requests", GroupRequestController
resources "/sessions", SessionController, except: [:index, :show]
resources "/tracks", TrackController, except: [:index, :show]
get "/tracks/:id/sessions", SessionController, :show_sessions_for_track
resources "/categories", CategoryController resources "/categories", CategoryController
resources "/tags", TagController resources "/tags", TagController
resources "/event_accounts", EventAccountsController
resources "/event_requests", EventRequestController
resources "/groups", GroupController, except: [:index]
resources "/group_accounts", GroupAccountController
resources "/group_requests", GroupRequestController
resources "/sessions", SessionController, except: [:new, :edit]
resources "/tracks", TrackController, except: [:new, :edit]
end end
scope "/", EventosWeb do scope "/", EventosWeb do

View File

@ -3,23 +3,27 @@ defmodule EventosWeb.AccountView do
View for Accounts View for Accounts
""" """
use EventosWeb, :view use EventosWeb, :view
alias EventosWeb.AccountView alias EventosWeb.{AccountView, EventView}
def render("index.json", %{accounts: accounts}) do def render("index.json", %{accounts: accounts}) do
%{data: render_many(accounts, AccountView, "account_for_user.json")} %{data: render_many(accounts, AccountView, "acccount_basic.json")}
end end
def render("show.json", %{account: account}) do def render("show.json", %{account: account}) do
%{data: render_one(account, AccountView, "account.json")} %{data: render_one(account, AccountView, "account.json")}
end end
def render("account_for_user.json", %{account: account}) do def render("show_basic.json", %{account: account}) do
%{data: render_one(account, AccountView, "account_basic.json")}
end
def render("acccount_basic.json", %{account: account}) do
%{id: account.id, %{id: account.id,
username: account.username, username: account.username,
domain: account.domain, domain: account.domain,
display_name: account.display_name, display_name: account.display_name,
description: account.description, description: account.description,
public_key: account.public_key, # public_key: account.public_key,
suspended: account.suspended, suspended: account.suspended,
uri: account.uri, uri: account.uri,
url: account.url, url: account.url,
@ -32,11 +36,11 @@ defmodule EventosWeb.AccountView do
domain: account.domain, domain: account.domain,
display_name: account.display_name, display_name: account.display_name,
description: account.description, description: account.description,
public_key: account.public_key, # public_key: account.public_key,
suspended: account.suspended, suspended: account.suspended,
uri: account.uri, uri: account.uri,
url: account.url, url: account.url,
organized_events: account.organized_events organized_events: render_many(account.organized_events, EventView, "event_simple.json")
} }
end end
end end

View File

@ -3,21 +3,38 @@ defmodule EventosWeb.EventView do
View for Events View for Events
""" """
use EventosWeb, :view use EventosWeb, :view
alias EventosWeb.EventView alias EventosWeb.{EventView, AccountView, GroupView}
def render("index.json", %{events: events}) do def render("index.json", %{events: events}) do
%{data: render_many(events, EventView, "event.json")} %{data: render_many(events, EventView, "event_simple.json")}
end
def render("show_simple.json", %{event: event}) do
%{data: render_one(event, EventView, "event_simple.json")}
end end
def render("show.json", %{event: event}) do def render("show.json", %{event: event}) do
%{data: render_one(event, EventView, "event.json")} %{data: render_one(event, EventView, "event.json")}
end end
def render("event_simple.json", %{event: event}) do
%{id: event.id,
title: event.title,
description: event.description,
begins_on: event.begins_on,
ends_on: event.ends_on,
}
end
def render("event.json", %{event: event}) do def render("event.json", %{event: event}) do
%{id: event.id, %{id: event.id,
title: event.title, title: event.title,
description: event.description, description: event.description,
begins_on: event.begins_on, begins_on: event.begins_on,
ends_on: event.ends_on} ends_on: event.ends_on,
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"),
}
end end
end end

View File

@ -3,22 +3,39 @@ defmodule EventosWeb.GroupView do
View for Groups View for Groups
""" """
use EventosWeb, :view use EventosWeb, :view
alias EventosWeb.GroupView alias EventosWeb.{GroupView, AccountView}
def render("index.json", %{groups: groups}) do def render("index.json", %{groups: groups}) do
%{data: render_many(groups, GroupView, "group.json")} %{data: render_many(groups, GroupView, "group_simple.json")}
end end
def render("show.json", %{group: group}) do def render("show.json", %{group: group}) do
%{data: render_one(group, GroupView, "group.json")} %{data: render_one(group, GroupView, "group.json")}
end end
def render("show_simple.json", %{group: group}) do
%{data: render_one(group, GroupView, "group_simple.json")}
end
def render("group_simple.json", %{group: group}) do
%{id: group.id,
title: group.title,
description: group.description,
suspended: group.suspended,
url: group.url,
uri: group.uri
}
end
def render("group.json", %{group: group}) do def render("group.json", %{group: group}) do
%{id: group.id, %{id: group.id,
title: group.title, title: group.title,
description: group.description, description: group.description,
suspended: group.suspended, suspended: group.suspended,
url: group.url, url: group.url,
uri: group.uri} uri: group.uri,
members: render_many(group.members, AccountView, "acccount_basic.json"),
events: render_many(group.organized_events, EventView, "event_simple.json")
}
end end
end end

View File

@ -28,7 +28,7 @@ defmodule EventosWeb.UserView do
def render("user_simple.json", %{user: user}) do def render("user_simple.json", %{user: user}) do
%{id: user.id, %{id: user.id,
role: user.role, role: user.role,
account: render_one(user.account, AccountView, "account_for_user.json") account: render_one(user.account, AccountView, "acccount_basic.json")
} }
end end

View File

@ -15,13 +15,15 @@ defmodule Eventos.Repo.Migrations.CreateEvents do
add :large_image, :string add :large_image, :string
add :thumbnail, :string add :thumbnail, :string
add :publish_at, :datetimetz add :publish_at, :datetimetz
add :organizer_id, references(:accounts, on_delete: :nothing), null: false 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 :category_id, references(:categories, on_delete: :nothing), null: false
timestamps() timestamps()
end end
create index(:events, [:organizer_id]) create index(:events, [:organizer_account_id])
create index(:events, [:organizer_group_id])
create unique_index(:events, [:slug]) create unique_index(:events, [:slug])
end end

View File

@ -9,7 +9,6 @@
# #
# We recommend using the bang functions (`insert!`, `update!` # We recommend using the bang functions (`insert!`, `update!`
# and so on) as they will fail if something goes wrong. # and so on) as they will fail if something goes wrong.
import Logger
Eventos.Repo.delete_all Eventos.Accounts.User Eventos.Repo.delete_all Eventos.Accounts.User

View File

@ -14,7 +14,7 @@ defmodule Eventos.EventsTest do
end end
def event_fixture do def event_fixture do
insert(:event, organizer: account_fixture()) insert(:event, organizer_account: account_fixture())
end end
def category_fixture do def category_fixture do
@ -42,7 +42,7 @@ defmodule Eventos.EventsTest do
test "create_event/1 with valid data creates a event" do test "create_event/1 with valid data creates a event" do
{:ok, account} = Accounts.create_account(@account_valid_attrs) {:ok, account} = Accounts.create_account(@account_valid_attrs)
category = category_fixture() category = category_fixture()
valid_attrs_with_account_id = Map.put(@event_valid_attrs, :organizer_id, account.id) 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) 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) assert {:ok, %Event{} = event} = Events.create_event(valid_attrs_with_account_id)
assert event.begins_on == DateTime.from_naive!(~N[2010-04-17 14:00:00.000000Z], "Etc/UTC") assert event.begins_on == DateTime.from_naive!(~N[2010-04-17 14:00:00.000000Z], "Etc/UTC")

View File

@ -30,7 +30,7 @@ defmodule EventosWeb.EventControllerTest do
describe "create event" do describe "create event" do
test "renders event when data is valid", %{conn: conn, user: user} do test "renders event when data is valid", %{conn: conn, user: user} do
attrs = Map.put(@create_attrs, :organizer_id, user.account.id) attrs = Map.put(@create_attrs, :organizer_account_id, user.account.id)
category = insert(:category) category = insert(:category)
attrs = Map.put(attrs, :category_id, category.id) attrs = Map.put(attrs, :category_id, category.id)
@ -44,12 +44,25 @@ defmodule EventosWeb.EventControllerTest do
"begins_on" => "2010-04-17T14:00:00Z", "begins_on" => "2010-04-17T14:00:00Z",
"description" => "some description", "description" => "some description",
"ends_on" => "2010-04-17T14:00:00Z", "ends_on" => "2010-04-17T14:00:00Z",
"title" => "some title"} "title" => "some title",
"group" => nil,
"organizer" => %{
"description" => nil,
"display_name" => nil,
"domain" => nil,
"id" => user.account.id,
"suspended" => false,
"uri" => "https://",
"url" => "https://",
"username" => user.account.username
},
"participants" => []
}
end end
test "renders errors when data is invalid", %{conn: conn, user: user} do test "renders errors when data is invalid", %{conn: conn, user: user} do
conn = auth_conn(conn, user) conn = auth_conn(conn, user)
attrs = Map.put(@invalid_attrs, :organizer_id, user.account.id) attrs = Map.put(@invalid_attrs, :organizer_account_id, user.account.id)
conn = post conn, event_path(conn, :create), event: attrs conn = post conn, event_path(conn, :create), event: attrs
assert json_response(conn, 422)["errors"] != %{} assert json_response(conn, 422)["errors"] != %{}
end end
@ -71,7 +84,7 @@ defmodule EventosWeb.EventControllerTest do
test "renders event when data is valid", %{conn: conn, event: %Event{id: id} = event, user: user} do test "renders event when data is valid", %{conn: conn, event: %Event{id: id} = event, user: user} do
conn = auth_conn(conn, user) conn = auth_conn(conn, user)
attrs = Map.put(@update_attrs, :organizer_id, user.account.id) attrs = Map.put(@update_attrs, :organizer_account_id, user.account.id)
conn = put conn, event_path(conn, :update, event), event: attrs conn = put conn, event_path(conn, :update, event), event: attrs
assert %{"id" => ^id} = json_response(conn, 200)["data"] assert %{"id" => ^id} = json_response(conn, 200)["data"]
@ -81,12 +94,25 @@ defmodule EventosWeb.EventControllerTest do
"begins_on" => "2011-05-18T15:01:01Z", "begins_on" => "2011-05-18T15:01:01Z",
"description" => "some updated description", "description" => "some updated description",
"ends_on" => "2011-05-18T15:01:01Z", "ends_on" => "2011-05-18T15:01:01Z",
"title" => "some updated title"} "title" => "some updated title",
"group" => nil,
"organizer" => %{
"description" => nil,
"display_name" => nil,
"domain" => nil,
"id" => user.account.id,
"suspended" => false,
"uri" => "https://",
"url" => "https://",
"username" => user.account.username
},
"participants" => []
}
end end
test "renders errors when data is invalid", %{conn: conn, event: event, user: user} do test "renders errors when data is invalid", %{conn: conn, event: event, user: user} do
conn = auth_conn(conn, user) conn = auth_conn(conn, user)
attrs = Map.put(@invalid_attrs, :organizer_id, user.account.id) attrs = Map.put(@invalid_attrs, :organizer_account_id, user.account.id)
conn = put conn, event_path(conn, :update, event), event: attrs conn = put conn, event_path(conn, :update, event), event: attrs
assert json_response(conn, 422)["errors"] != %{} assert json_response(conn, 422)["errors"] != %{}
end end
@ -107,7 +133,7 @@ defmodule EventosWeb.EventControllerTest do
defp create_event(_) do defp create_event(_) do
account = insert(:account) account = insert(:account)
event = insert(:event, organizer: account) event = insert(:event, organizer_account: account)
{:ok, event: event, account: account} {:ok, event: event, account: account}
end end

View File

@ -40,8 +40,11 @@ defmodule EventosWeb.GroupControllerTest do
"description" => "some description", "description" => "some description",
"suspended" => true, "suspended" => true,
"title" => "some title", "title" => "some title",
"uri" => "some uri", "uri" => "h",
"url" => "some url"} "url" => "h",
"events" => [],
"members" => []
}
end end
test "renders errors when data is invalid", %{conn: conn, user: user} do test "renders errors when data is invalid", %{conn: conn, user: user} do
@ -66,7 +69,10 @@ defmodule EventosWeb.GroupControllerTest do
"suspended" => false, "suspended" => false,
"title" => "some updated title", "title" => "some updated title",
"uri" => "some updated uri", "uri" => "some updated uri",
"url" => "some updated url"} "url" => "some updated url",
"events" => [],
"members" => []
}
end end
test "renders errors when data is invalid", %{conn: conn, group: group, user: user} do test "renders errors when data is invalid", %{conn: conn, group: group, user: user} do

View File

@ -18,7 +18,7 @@ defmodule EventosWeb.SessionControllerTest do
setup %{conn: conn} do setup %{conn: conn} do
account = insert(:account) account = insert(:account)
user = insert(:user, account: account) user = insert(:user, account: account)
event = insert(:event, organizer: account) event = insert(:event, organizer_account: account)
{:ok, conn: conn, user: user, event: event} {:ok, conn: conn, user: user, event: event}
end end
@ -32,10 +32,14 @@ defmodule EventosWeb.SessionControllerTest do
describe "create session" do describe "create session" do
test "renders session when data is valid", %{conn: conn, user: user, event: event} do test "renders session when data is valid", %{conn: conn, user: user, event: event} do
conn = auth_conn(conn, user) conn = auth_conn(conn, user)
attrs = Map.put(@create_attrs, :event_id, event.id) event_id = event.id
attrs = Map.put(@create_attrs, :event_id, event_id)
conn = post conn, session_path(conn, :create), session: attrs conn = post conn, session_path(conn, :create), session: attrs
assert %{"id" => id} = json_response(conn, 201)["data"] assert %{"id" => id} = json_response(conn, 201)["data"]
conn = get conn, "/api/events/" <> Integer.to_string(event_id) <> "/sessions"
assert hd(json_response(conn, 200)["data"])["id"] == id
conn = get conn, session_path(conn, :show, id) conn = get conn, session_path(conn, :show, id)
assert json_response(conn, 200)["data"] == %{ assert json_response(conn, 200)["data"] == %{
"id" => id, "id" => id,

View File

@ -18,7 +18,7 @@ defmodule EventosWeb.TrackControllerTest do
setup %{conn: conn} do setup %{conn: conn} do
account = insert(:account) account = insert(:account)
user = insert(:user, account: account) user = insert(:user, account: account)
event = insert(:event, organizer: account) event = insert(:event, organizer_account: account)
{:ok, conn: conn, user: user, event: event} {:ok, conn: conn, user: user, event: event}
end end

View File

@ -39,7 +39,7 @@ defmodule Eventos.Factory do
description: "My desc", description: "My desc",
begins_on: nil, begins_on: nil,
ends_on: nil, ends_on: nil,
organizer: build(:account), organizer_account: build(:account),
category: build(:category) category: build(:category)
} }
end end