Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customize event index. #285

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions lib/course_planner/events/events.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@ defmodule CoursePlanner.Events do
}
alias Ecto.Changeset

@query from e in Event, order_by: [asc: e.date, asc: e.starting_time, asc: e.finishing_time]
@notifier Application.get_env(:course_planner, :notifier, CoursePlanner.Notifications.Notifier)

def all do
query = from e in Event,
order_by: [asc: e.date, asc: e.starting_time, asc: e.finishing_time]

Repo.all(query)
def all(%{role: role}) when role in ["Coordinator", "Supervisor"], do: Repo.all(@query)
def all(user) do
user
|> Repo.preload(events: @query)
|> Map.get(:events)
end

def all_splitted(now) do
all()
def all_splitted(now, current_user) do
current_user
|> all()
|> Enum.split_with(&(compare_event_date_time(&1, now)))
|> reverse_past_events()
end
Expand All @@ -37,14 +39,6 @@ defmodule CoursePlanner.Events do
{Enum.reverse(past_events), upcoming_events}
end

def all_with_users do
query = from e in Event,
preload: [:users],
order_by: [asc: e.date, asc: e.starting_time, asc: e.finishing_time]

Repo.all(query)
end

def get(id) do
case Repo.get(Event, id) do
nil -> {:error, :not_found}
Expand Down
4 changes: 2 additions & 2 deletions lib/course_planner_web/controllers/event_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ defmodule CoursePlannerWeb.EventController do
import Canary.Plugs
plug :authorize_controller

def index(conn, _params) do
{past_events, upcoming_events} = Events.all_splitted(Timex.now())
def index(%{assigns: %{current_user: current_user}} = conn, _params) do
{past_events, upcoming_events} = Events.all_splitted(Timex.now(), current_user)

render(conn, "index.html",
past_events: past_events,
Expand Down
39 changes: 32 additions & 7 deletions test/course_planner/events/events_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,42 @@ defmodule CoursePlanner.EventsTest do
@update_attrs %{date: @today, description: "some updated description", finishing_time: ~T[15:01:01.000000], location: "some updated location", name: "some updated name", starting_time: ~T[14:01:01.000000]}
@invalid_attrs %{date: nil, description: nil, finishing_time: nil, location: nil, name: nil, starting_time: nil}

test "all/0 returns all events" do
event = insert(:event)
assert Events.all() == [event]
test "all/1 returns all events ordered" do
%{id: id3} = insert(:event, %{date: @today, starting_time: ~T[13:00:00.000000],
finishing_time: ~T[14:00:00.000000]})
%{id: id2} = insert(:event, %{date: @today, starting_time: ~T[13:00:00.000000],
finishing_time: ~T[13:00:00.000000]})
%{id: id1} = insert(:event, %{date: @today, starting_time: ~T[10:00:00.000000]})

assert [%{id: ^id1}, %{id: ^id2}, %{id: ^id3}] = Events.all(%{role: "Coordinator"})
end

test "all_with_users/0 returns all events with preloaded users" do
event = insert(:event)
assert Events.all_with_users() == [event |> Repo.preload(:users)]
for role <- [:student, :teacher, :volunteer] do
@role role
test "all/1 return only invited events for #{@role}" do
user = insert(@role)
other_users = [insert(:student), insert(:teacher)]
%{id: id} = insert(:event, users: [user | other_users])
_not_invited_event = insert(:event, users: other_users)

assert [%{id: ^id}] = Events.all(user)
end
end

for role <- [:supervisor, :coordinator] do
@role role
test "all/1 return all events for #{@role}" do
user = insert(@role)
other_users = [insert(:student), insert(:teacher)]
%{id: id1} = insert(:event, starting_time: ~T[10:00:00.000000], users: [user | other_users])
%{id: id2} = insert(:event, starting_time: ~T[12:00:00.000000], users: other_users)

assert [%{id: ^id1}, %{id: ^id2}] = Events.all(user)
end
end

test "all_splitted/1 returns events splited by time" do
user = insert(:coordinator)
day_before = Timex.shift(@today, days: -1)
day_after = Timex.shift(@today, days: 1)
%{id: id1} = insert(:event, %{date: day_before, starting_time: ~T[10:00:00.000000]})
Expand All @@ -38,7 +63,7 @@ defmodule CoursePlanner.EventsTest do
{past_events, upcoming_events} =
@now
|> Timex.set([hour: 14, minute: 0, second: 0])
|> Events.all_splitted()
|> Events.all_splitted(user)

assert [%{id: ^id3}, %{id: ^id2}, %{id: ^id1}] = past_events
assert [%{id: ^id4}, %{id: ^id5}, %{id: ^id6}] = upcoming_events
Expand Down