Skip to content
Draft
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
6 changes: 5 additions & 1 deletion lib/atlas/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ defmodule Atlas.Accounts do
** (Ecto.NoResultsError)

"""
def get_user!(id), do: Repo.get!(User, id)
def get_user!(id, opts \\ []) do
User
|> apply_filters(opts)
|> Repo.get!(id)
end

@doc """
Gets a single user.
Expand Down
15 changes: 0 additions & 15 deletions lib/atlas/accounts/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,6 @@ defmodule Atlas.Accounts.User do

alias Atlas.University

@derive {
Flop.Schema,
filterable: [:name, :number],
sortable: [:name],
default_limit: 20,
join_fields: [
number: [
binding: :student,
field: :number,
path: [:student, :number],
ecto_type: :string
]
]
}

schema "users" do
field :name, :string
field :email, :string
Expand Down
1 change: 1 addition & 0 deletions lib/atlas/events/event_category.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ defmodule Atlas.Events.EventCategory do
event_category
|> cast(attrs, @required_fields ++ @optional_fields)
|> validate_required(@required_fields)
|> foreign_key_constraint(:course_id)
end
end
29 changes: 12 additions & 17 deletions lib/atlas/university.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ defmodule Atlas.University do

use Atlas.Context

alias Atlas.Accounts.User
alias Atlas.University.{CourseEnrollment, ShiftEnrollment, Student}
alias Atlas.University.Degrees.Courses.Course
alias Atlas.Workers
Expand All @@ -20,35 +19,31 @@ defmodule Atlas.University do

"""
def list_students do
User
|> where(type: :student)
|> preload(:student)
Student
|> preload(:user)
|> Repo.all()
end

def list_students(opts) when is_list(opts) do
User
Student
|> apply_filters(opts)
|> where(type: :student)
|> preload(:student)
|> preload(:user)
|> Repo.all()
end

def list_students(params) do
User
|> where(type: :student)
|> join(:left, [o], p in assoc(o, :student), as: :student)
|> preload(:student)
|> Flop.validate_and_run(params, for: User)
Student
|> join(:left, [o], p in assoc(o, :user), as: :user)
|> preload(:user)
|> Flop.validate_and_run(params, for: Student)
end

def list_students(%{} = params, opts) when is_list(opts) do
User
Student
|> apply_filters(opts)
|> where(type: :student)
|> join(:left, [o], p in assoc(o, :student), as: :student)
|> preload(:student)
|> Flop.validate_and_run(params, for: User)
|> join(:left, [o], p in assoc(o, :user), as: :user)
|> preload(:user)
|> Flop.validate_and_run(params, for: Student)
end

@doc """
Expand Down
15 changes: 15 additions & 0 deletions lib/atlas/university/student.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ defmodule Atlas.University.Student do
@required_fields ~w(number degree_id)a
@optional_fields ~w(special_status degree_year user_id)a

@derive {
Flop.Schema,
filterable: [:number, :name],
sortable: [:number, :name],
default_limit: 20,
join_fields: [
name: [
binding: :user,
field: :name,
path: [:user, :name],
ecto_type: :string
]
]
}

schema "students" do
field :number, :string
field :special_status, :string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule AtlasWeb.University.StudentsController do
defmodule AtlasWeb.University.StudentController do
use AtlasWeb, :controller

alias Atlas.University
Expand All @@ -15,6 +15,13 @@ defmodule AtlasWeb.University.StudentsController do
end
end

def show(conn, %{"id" => student_id}) do
student = University.get_student!(student_id, preloads: [:user])

conn
|> render(:show, student: student)
end

def schedule_index(conn, params) do
{user, _session} = Guardian.Plug.current_resource(conn)

Expand Down
30 changes: 30 additions & 0 deletions lib/atlas_web/controllers/university/student_json.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule AtlasWeb.University.StudentJSON do
alias Atlas.University.Student
alias AtlasWeb.MetaJSON

def index(%{students: students, meta: meta}) do
%{
users: for(student <- students, do: data(student)),
meta: MetaJSON.data(meta)
}
end

def show(%{student: student}) do
%{student: data(student)}
end

def data(%Student{} = student) do
%{
id: student.id,
number: student.number,
special_status: student.special_status,
degree_year: student.degree_year,
user:
if Ecto.assoc_loaded?(student.user) && student.user do
AtlasWeb.UserJSON.data(student.user)
else
nil
end
}
end
end
25 changes: 0 additions & 25 deletions lib/atlas_web/controllers/university/students_json.ex

This file was deleted.

12 changes: 8 additions & 4 deletions lib/atlas_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,17 @@ defmodule AtlasWeb.Router do

scope "/student", University do
scope "/schedule" do
get "/", StudentsController, :schedule_index
post "/", StudentsController, :schedule_update
get "/", StudentController, :schedule_index
post "/", StudentController, :schedule_update

pipe_through :is_at_least_professor

get "/:id", StudentsController, :student_schedule_index
get "/:id", StudentController, :student_schedule_index
end

pipe_through :is_at_least_professor

get "/:id", StudentController, :show
end

scope "/shift_exchanges" do
Expand Down Expand Up @@ -111,7 +115,7 @@ defmodule AtlasWeb.Router do

pipe_through :is_at_least_professor

get "/students", University.StudentsController, :index
get "/students", University.StudentController, :index

scope "/jobs" do
get "/", JobController, :index
Expand Down