Skip to content

Commit

Permalink
Moves "find_all_by_user" from offered_courses to terms
Browse files Browse the repository at this point in the history
  • Loading branch information
ghatighorias committed Sep 20, 2017
1 parent f7d6542 commit ea921a5
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 69 deletions.
30 changes: 1 addition & 29 deletions lib/course_planner/courses/offered_courses.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule CoursePlanner.Courses.OfferedCourses do
@moduledoc false

alias CoursePlanner.{Courses.OfferedCourse, Repo, Attendances, Notifications.Notifier,
Notifications, Terms.Term}
Notifications}
import Ecto.Query

@notifier Application.get_env(:course_planner, :notifier, Notifier)
Expand All @@ -16,34 +16,6 @@ defmodule CoursePlanner.Courses.OfferedCourses do
|> Enum.into(%{})
end

def find_all_by_user(%{role: "Coordinator"}) do
Repo.all(from t in Term,
join: oc in assoc(t, :offered_courses),
join: co in assoc(oc, :course),
preload: [offered_courses: {oc, course: co}],
order_by: [asc: t.start_date, asc: co.name])
end
def find_all_by_user(%{role: "Teacher", id: user_id}) do
Repo.all(from t in Term,
join: oc in assoc(t, :offered_courses),
join: co in assoc(oc, :course),
join: te in assoc(oc, :teachers),
preload: [offered_courses: {oc, course: co, teachers: te}],
where: te.id == ^user_id,
order_by: [asc: t.start_date, asc: co.name]
)
end
def find_all_by_user(%{role: "Student", id: user_id}) do
Repo.all(from t in Term,
join: oc in assoc(t, :offered_courses),
join: co in assoc(oc, :course),
join: s in assoc(oc, :students),
preload: [offered_courses: {oc, course: co, students: s}],
where: s.id == ^user_id,
order_by: [asc: t.start_date, asc: co.name]
)
end

def student_matrix(term_id) do
offered_courses =
term_id
Expand Down
30 changes: 30 additions & 0 deletions lib/course_planner/terms/terms.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ defmodule CoursePlanner.Terms do
@moduledoc """
Handle all interactions with Terms, create, list, fetch, edit, and delete
"""
import Ecto.Query

alias CoursePlanner.{Repo, Courses.OfferedCourses, Notifications.Notifier,
Accounts.Coordinators, Notifications}
alias CoursePlanner.Terms.{Holiday, Term}
Expand Down Expand Up @@ -98,4 +100,32 @@ defmodule CoursePlanner.Terms do
students_and_teachers = OfferedCourses.get_subscribed_users(offered_courses)
students_and_teachers ++ Coordinators.all()
end

def find_all_by_user(%{role: "Coordinator"}) do
Repo.all(from t in Term,
join: oc in assoc(t, :offered_courses),
join: co in assoc(oc, :course),
preload: [offered_courses: {oc, course: co}],
order_by: [asc: t.start_date, asc: co.name])
end
def find_all_by_user(%{role: "Teacher", id: user_id}) do
Repo.all(from t in Term,
join: oc in assoc(t, :offered_courses),
join: co in assoc(oc, :course),
join: te in assoc(oc, :teachers),
preload: [offered_courses: {oc, course: co, teachers: te}],
where: te.id == ^user_id,
order_by: [asc: t.start_date, asc: co.name]
)
end
def find_all_by_user(%{role: "Student", id: user_id}) do
Repo.all(from t in Term,
join: oc in assoc(t, :offered_courses),
join: co in assoc(oc, :course),
join: s in assoc(oc, :students),
preload: [offered_courses: {oc, course: co, students: s}],
where: s.id == ^user_id,
order_by: [asc: t.start_date, asc: co.name]
)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule CoursePlannerWeb.OfferedCourseController do
Courses.OfferedCourses,
Accounts.Students,
Accounts.Teachers,
Terms
}
alias Ecto.Changeset
import Ecto.Query, only: [from: 2]
Expand All @@ -17,7 +18,7 @@ defmodule CoursePlannerWeb.OfferedCourseController do
plug :authorize_controller

def index(%{assigns: %{current_user: current_user}} = conn, _params) do
terms = OfferedCourses.find_all_by_user(current_user)
terms = Terms.find_all_by_user(current_user)
render(conn, "index.html", terms: terms)
end

Expand Down
39 changes: 0 additions & 39 deletions test/lib/course_planner/courses/offered_courses_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,45 +42,6 @@ defmodule CoursePlanner.OfferedCoursesTest do
end
end

describe "find_all_by_user/1" do
test "coordinators should see all offered courses" do
user = insert(:coordinator)
term = insert(:term)
insert_list(2, :offered_course, term: term)

terms = OfferedCourses.find_all_by_user(user)
assert length(terms) == 1

term = List.first(terms)
assert length(term.offered_courses) == 2
end

test "teachers should see the offered courses they are assigned to" do
user = insert(:teacher)
term = insert(:term)
user_course = insert(:offered_course, term: term, teachers: [user])
insert(:offered_course, term: term)

terms = OfferedCourses.find_all_by_user(user)
assert length(terms) == 1

term = List.first(terms)
assert List.first(term.offered_courses).id == user_course.id
end

test "students should see the offered courses they are assigned to" do
user = insert(:student)
user_course = insert(:offered_course, students: [user])
insert(:offered_course)

terms = OfferedCourses.find_all_by_user(user)
assert length(terms) == 1

term = List.first(terms)
assert List.first(term.offered_courses).id == user_course.id
end
end

describe "with_pending_attendances" do
test "when there is no offered_course" do
assert [] == OfferedCourses.with_pending_attendances()
Expand Down
40 changes: 40 additions & 0 deletions test/lib/course_planner/terms/terms_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,44 @@ defmodule CoursePlanner.TermsTest do
term_users = Terms.get_subscribed_users(term)
assert length(term_users) == 6
end


describe "find_all_by_user/1" do
test "coordinators should see all offered courses" do
user = insert(:coordinator)
term = insert(:term)
insert_list(2, :offered_course, term: term)

terms = Terms.find_all_by_user(user)
assert length(terms) == 1

term = List.first(terms)
assert length(term.offered_courses) == 2
end

test "teachers should see the offered courses they are assigned to" do
user = insert(:teacher)
term = insert(:term)
user_course = insert(:offered_course, term: term, teachers: [user])
insert(:offered_course, term: term)

terms = Terms.find_all_by_user(user)
assert length(terms) == 1

term = List.first(terms)
assert List.first(term.offered_courses).id == user_course.id
end

test "students should see the offered courses they are assigned to" do
user = insert(:student)
user_course = insert(:offered_course, students: [user])
insert(:offered_course)

terms = Terms.find_all_by_user(user)
assert length(terms) == 1

term = List.first(terms)
assert List.first(term.offered_courses).id == user_course.id
end
end
end

0 comments on commit ea921a5

Please sign in to comment.