Skip to content

Commit

Permalink
SubmissionHandler now returns ApiSubmission
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaro committed Jul 10, 2024
1 parent 4b79f38 commit 2312f89
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import dev.dres.api.rest.RestApi
import dev.dres.api.rest.handler.AccessManagedRestHandler
import dev.dres.api.rest.handler.PostRestHandler
import dev.dres.api.rest.types.evaluation.submission.ApiClientSubmission
import dev.dres.api.rest.types.evaluation.submission.ApiSubmission
import dev.dres.api.rest.types.evaluation.submission.ApiVerdictStatus
import dev.dres.api.rest.types.status.ErrorStatus
import dev.dres.api.rest.types.status.ErrorStatusException
import dev.dres.api.rest.types.status.SuccessStatus
import dev.dres.api.rest.types.status.SuccessfulSubmissionsStatus
import dev.dres.api.rest.types.users.ApiRole
import dev.dres.data.model.run.RunActionContext.Companion.runActionContext
import dev.dres.data.model.submissions.DbSubmission
Expand All @@ -32,7 +32,7 @@ import kotlinx.dnq.query.firstOrNull
import kotlinx.dnq.transactional
import org.slf4j.LoggerFactory

class SubmissionHandler(private val store: TransientEntityStore) : PostRestHandler<SuccessfulSubmissionsStatus>,
class SubmissionHandler(private val store: TransientEntityStore) : PostRestHandler<ApiSubmission>,
AccessManagedRestHandler {

override val permittedRoles = setOf(ApiRole.PARTICIPANT)
Expand Down Expand Up @@ -73,13 +73,8 @@ class SubmissionHandler(private val store: TransientEntityStore) : PostRestHandl
responses = [
OpenApiResponse(
"200",
[OpenApiContent(SuccessfulSubmissionsStatus::class)],
description = "The submission was accepted by the server and there was a verdict"
),
OpenApiResponse(
"202",
[OpenApiContent(SuccessfulSubmissionsStatus::class)],
description = "The submission was accepted by the server and there has not yet been a verdict available"
[OpenApiContent(ApiSubmission::class)],
description = "The submission that was received."
),
OpenApiResponse("400", [OpenApiContent(ErrorStatus::class)]),
OpenApiResponse("401", [OpenApiContent(ErrorStatus::class)]),
Expand All @@ -92,7 +87,7 @@ class SubmissionHandler(private val store: TransientEntityStore) : PostRestHandl
],
tags = ["Submission"]
)
override fun doPost(ctx: Context): SuccessfulSubmissionsStatus {
override fun doPost(ctx: Context): ApiSubmission {
/* Obtain run action context and parse submission. */
val rac = ctx.runActionContext()
val apiClientSubmission = try {
Expand Down Expand Up @@ -131,47 +126,7 @@ class SubmissionHandler(private val store: TransientEntityStore) : PostRestHandl
)
}


/* Return status. */

var correct = 0
var wrong = 0
var undcidable = 0
var indeterminate = 0
apiSubmission.answers.map { it.status() }.forEach {
when (it) {
VerdictStatus.CORRECT -> correct++
VerdictStatus.WRONG -> wrong++
VerdictStatus.INDETERMINATE -> indeterminate++
VerdictStatus.UNDECIDABLE -> undcidable++
}
}
val max = listOf(correct, wrong, undcidable, indeterminate).max()
return when (max) {
0 -> throw ErrorStatusException(
500,
"No verdict information available ${apiClientSubmission.submissionId}. This is a serious bug and should be reported!",
ctx
)

correct -> SuccessfulSubmissionsStatus(ApiVerdictStatus.CORRECT, "Submission correct, well done!")
wrong -> SuccessfulSubmissionsStatus(ApiVerdictStatus.WRONG, "Submission wrong, try again!")
undcidable -> SuccessfulSubmissionsStatus(
ApiVerdictStatus.UNDECIDABLE,
"Submission undecidable, try again!"
)

indeterminate -> {
ctx.status(202)
SuccessfulSubmissionsStatus(ApiVerdictStatus.INDETERMINATE, "Submission received, awaiting verdict!")
}

else -> throw ErrorStatusException(
500,
"Error while calculating submission verdict for submission ${apiClientSubmission.submissionId}. This is a serious bug and should be reported!",
ctx
)
}
return apiSubmission

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ data class ApiSubmission(
val teamName: String,
val memberName: String,
override val timestamp: Long,
val answers: List<ApiAnswerSet> = emptyList()
val answerSets: List<ApiAnswerSet> = emptyList()
) : Submission {

init {
answers.forEach { it.submission = this }
answerSets.forEach { it.submission = this }
}
override fun answerSets(): Sequence<AnswerSet> = answers.asSequence()
override fun answerSets(): Sequence<AnswerSet> = answerSets.asSequence()

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ class DbSubmission(entity: Entity) : PersistentEntity(entity), Submission {
memberId = this.user.id,
memberName = this.user.username,
timestamp = this.timestamp,
answers = this.answerSets.asSequence().map { it.toApi(blind) }.toList()
answerSets = this.answerSets.asSequence().map { it.toApi(blind) }.toList()
)
}

0 comments on commit 2312f89

Please sign in to comment.