-
Notifications
You must be signed in to change notification settings - Fork 56
Update /assessments
endpoint
#729
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
Merged
Merged
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
d7da86f
`/assessments`: split out separate `/admin` endpoint
bnjmnt4n 01de819
Consistently use `/assessments/:assessmentid`
bnjmnt4n 2b247c9
Merge `/admin/assessments/:assessmentid/{update,publish}` endpoints
bnjmnt4n 956abc2
Quick swagger docs fix for `AdminAssessmentsController`
bnjmnt4n 0f48423
Format code via `mix format`
bnjmnt4n 3e33845
Formatting fix
bnjmnt4n abd2fa2
`/assessments/{assessmentid}/submit`: shift role validation to contro…
bnjmnt4n 41cd47d
Add more guards
bnjmnt4n 10d5ec1
`/assessments/question/{questionid}/submit`: shift role validation to…
bnjmnt4n af263bc
clean up some docs
qreoct 6370c36
Modify `AnswerController#submit` path
bnjmnt4n 646c514
Assessments: remove unused definitions
bnjmnt4n 2706c44
Remove leftover unused route
bnjmnt4n 1d2428d
Split `AssesmentsController#unlock` out of `AssessmentsController#show`
bnjmnt4n 18e7d19
Merge remote-tracking branch 'origin/master' into v2
bnjmnt4n 27658cb
Fix formatting issues
bnjmnt4n 21b114e
`/assessments`: split out separate `/admin` endpoint
bnjmnt4n 5428e03
Consistently use `/assessments/:assessmentid`
bnjmnt4n a99e907
Merge `/admin/assessments/:assessmentid/{update,publish}` endpoints
bnjmnt4n 666df2f
Quick swagger docs fix for `AdminAssessmentsController`
bnjmnt4n af1bfda
Format code via `mix format`
bnjmnt4n 0802b86
`/assessments/{assessmentid}/submit`: shift role validation to contro…
bnjmnt4n 638e8ed
Add more guards
bnjmnt4n 961df0f
`/assessments/question/{questionid}/submit`: shift role validation to…
bnjmnt4n 0fb4505
clean up some docs
qreoct 09b9255
Modify `AnswerController#submit` path
bnjmnt4n 7057cf2
Assessments: remove unused definitions
bnjmnt4n 7c909a3
Remove leftover unused route
bnjmnt4n 5bf6a0c
Split `AssesmentsController#unlock` out of `AssessmentsController#show`
bnjmnt4n d9aaeba
Fix formatting issues
bnjmnt4n 991b025
pull master
qreoct 3911707
formatting
qreoct a6e47b0
Merge branch 'master' of https://github.com/source-academy/cadet into…
qreoct c18d9e3
Update per PR feedback
bnjmnt4n 748bb57
Merge remote-tracking branch 'origin/v2' into bnjmnt4n/assessments
bnjmnt4n 13a7da6
Remove unused variable
bnjmnt4n File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
lib/cadet_web/admin_controllers/admin_assessments_controller.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
defmodule CadetWeb.AdminAssessmentsController do | ||
use CadetWeb, :controller | ||
|
||
use PhoenixSwagger | ||
|
||
alias Cadet.Assessments | ||
import Cadet.Updater.XMLParser, only: [parse_xml: 2] | ||
|
||
def create(conn, %{"assessment" => assessment, "forceUpdate" => force_update}) do | ||
file = | ||
assessment["file"].path | ||
|> File.read!() | ||
|
||
result = | ||
case force_update do | ||
"true" -> parse_xml(file, true) | ||
"false" -> parse_xml(file, false) | ||
end | ||
|
||
case result do | ||
:ok -> | ||
if force_update == "true" do | ||
text(conn, "Force update OK") | ||
else | ||
text(conn, "OK") | ||
end | ||
|
||
{:ok, warning_message} -> | ||
text(conn, warning_message) | ||
|
||
{:error, {status, message}} -> | ||
conn | ||
|> put_status(status) | ||
|> text(message) | ||
end | ||
end | ||
|
||
def delete(conn, %{"assessmentid" => assessment_id}) do | ||
result = Assessments.delete_assessment(assessment_id) | ||
|
||
case result do | ||
{:ok, _nil} -> | ||
text(conn, "OK") | ||
|
||
{:error, {status, message}} -> | ||
conn | ||
|> put_status(status) | ||
|> text(message) | ||
end | ||
end | ||
|
||
def update(conn, params = %{"assessmentid" => assessment_id}) when is_ecto_id(assessment_id) do | ||
open_at = params |> Map.get("openAt") | ||
close_at = params |> Map.get("closeAt") | ||
is_published = params |> Map.get("isPublished") | ||
|
||
updated_assessment = | ||
if is_published == nil do | ||
%{} | ||
else | ||
%{:is_published => is_published} | ||
end | ||
|
||
with {:ok, assessment} <- check_dates(open_at, close_at, updated_assessment), | ||
{:ok, _nil} <- Assessments.update_assessment(assessment_id, assessment) do | ||
text(conn, "OK") | ||
else | ||
{:error, {status, message}} -> | ||
conn | ||
|> put_status(status) | ||
|> text(message) | ||
end | ||
end | ||
|
||
defp check_dates(open_at, close_at, assessment) do | ||
if open_at == nil and close_at == nil do | ||
{:ok, assessment} | ||
else | ||
formatted_open_date = elem(DateTime.from_iso8601(open_at), 1) | ||
formatted_close_date = elem(DateTime.from_iso8601(close_at), 1) | ||
|
||
if Timex.before?(formatted_close_date, formatted_open_date) do | ||
{:error, {:bad_request, "New end date should occur after new opening date"}} | ||
else | ||
assessment = Map.put(assessment, :open_at, formatted_open_date) | ||
assessment = Map.put(assessment, :close_at, formatted_close_date) | ||
|
||
{:ok, assessment} | ||
end | ||
end | ||
end | ||
|
||
swagger_path :create do | ||
post("/admin/assessments") | ||
|
||
summary("Creates a new assessment or updates an existing assessment.") | ||
|
||
security([%{JWT: []}]) | ||
|
||
consumes("multipart/form-data") | ||
|
||
parameters do | ||
assessment(:body, :file, "Assessment to create or update", required: true) | ||
forceUpdate(:body, :boolean, "Force update", required: true) | ||
end | ||
|
||
response(200, "OK") | ||
response(400, "XML parse error") | ||
response(403, "Forbidden") | ||
end | ||
|
||
swagger_path :delete do | ||
PhoenixSwagger.Path.delete("/admin/assessments/{assessmentid}") | ||
|
||
summary("Deletes an assessment.") | ||
|
||
security([%{JWT: []}]) | ||
|
||
parameters do | ||
assessmentId(:path, :integer, "Assessment ID", required: true) | ||
end | ||
|
||
response(200, "OK") | ||
response(403, "Forbidden") | ||
end | ||
|
||
swagger_path :update do | ||
post("/admin/assessments/{assessmentid}") | ||
|
||
summary("Updates an assessment.") | ||
|
||
security([%{JWT: []}]) | ||
|
||
consumes("application/json") | ||
|
||
parameters do | ||
assessmentId(:path, :integer, "Assessment ID", required: true) | ||
closeAt(:body, :string, "Open date", required: false) | ||
openAt(:body, :string, "Close date", required: false) | ||
isPublished(:body, :boolean, "Whether the assessment is published", required: false) | ||
end | ||
|
||
response(200, "OK") | ||
response(401, "Assessment is already opened") | ||
response(403, "Forbidden") | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just checking, forceUpdate is a string because this is submitted as multipart/form-data, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this is the case.