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
2 changes: 1 addition & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ config :atlas, AtlasWeb.Endpoint,
http: [ip: {127, 0, 0, 1}, port: 4000],
check_origin: false,
code_reloader: true,
debug_errors: true,
debug_errors: false,
secret_key_base: "+3Sc5XJKuK4YFpwINEFjTPHT57LDXshWNe8gMha9b/KkKjXt0h7cYp64JZkoIaFK",
watchers: []

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ defmodule AtlasWeb.ShiftExchangeRequestJSON do
@moduledoc """
A module for rendering shift exchange request data in JSON format.
"""
alias AtlasWeb.University.{CourseJSON, ShiftJSON}
alias AtlasWeb.ShiftsJSON
alias AtlasWeb.University.CourseJSON

@doc """
Renders a list of shift exchange requests as JSON.
Expand All @@ -28,8 +29,8 @@ defmodule AtlasWeb.ShiftExchangeRequestJSON do
%{
id: shift_exchange_request.id,
status: shift_exchange_request.status,
from: ShiftJSON.data(shift_exchange_request.from),
to: ShiftJSON.data(shift_exchange_request.to),
from: ShiftsJSON.data(shift_exchange_request.from),
to: ShiftsJSON.data(shift_exchange_request.to),
course: CourseJSON.data(shift_exchange_request.from.course),
inserted_at: shift_exchange_request.inserted_at
}
Expand Down
41 changes: 41 additions & 0 deletions lib/atlas_web/controllers/shifts/shifts_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
defmodule AtlasWeb.ShiftsController do
use AtlasWeb, :controller

alias Atlas.University.Degrees.Courses.Shifts

action_fallback AtlasWeb.FallbackController

def index(conn, attrs) do
{user, _} = Guardian.Plug.current_resource(conn)

if user_has_elevated_privileges?(user) do
shifts = Shifts.list_shifts(attrs)

conn
|> render(:index, shifts: shifts)
else
conn
|> put_status(:forbidden)
|> json(%{error: "Unauthorized"})
end
end

def update(conn, %{"id" => id} = attrs) do
{user, _} = Guardian.Plug.current_resource(conn)
shift = Shifts.get_shift!(id)

if user_has_elevated_privileges?(user) do
with {:ok, shift} <- Shifts.update_shift(shift, attrs) do
render(conn, :show, shift: shift)
end
else
conn
|> put_status(:forbidden)
|> json(%{error: "Unauthorized"})
end
end

defp user_has_elevated_privileges?(user) do
(user && user.type == :admin) || user.type == :professor
end
end
5 changes: 3 additions & 2 deletions lib/atlas_web/controllers/university/course_json.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule AtlasWeb.University.CourseJSON do
alias Atlas.University.Degrees.Courses.Course
alias AtlasWeb.University.{CourseJSON, ShiftJSON}
alias AtlasWeb.ShiftsJSON
alias AtlasWeb.University.CourseJSON

def index(%{courses: courses}) do
%{courses: for(course <- courses, do: data(course))}
Expand All @@ -22,7 +23,7 @@ defmodule AtlasWeb.University.CourseJSON do
end,
shifts:
if Ecto.assoc_loaded?(course.shifts) do
for(shift <- course.shifts, do: ShiftJSON.data(shift))
for(shift <- course.shifts, do: ShiftsJSON.data(shift))
else
[]
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
defmodule AtlasWeb.University.ShiftJSON do
defmodule AtlasWeb.ShiftsJSON do
alias Atlas.University.Degrees.Courses.Shifts.Shift
alias AtlasWeb.University.TimeslotJSON

def index(%{shifts: shifts}) do
%{data: for(shift <- shifts, do: data(shift))}
end

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

def data(%Shift{} = shift) do
%{
id: shift.id,
number: shift.number,
type: shift.type,
professor: shift.professor,
timeslots: for(timeslot <- shift.timeslots, do: TimeslotJSON.data(timeslot)),
timeslots:
if Ecto.assoc_loaded?(shift.timeslots) do
for timeslot <- shift.timeslots, do: TimeslotJSON.data(timeslot)
else
[]
end,
enrollment_status:
if Ecto.assoc_loaded?(shift.enrollments) && shift.enrollments != [] do
hd(shift.enrollments).status
else
nil
end
}
end
Expand Down
5 changes: 5 additions & 0 deletions lib/atlas_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ defmodule AtlasWeb.Router do

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

scope "/shifts" do
get "/", ShiftsController, :index
put "/:id", ShiftsController, :update
end

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