Skip to content

Commit

Permalink
Submissions improvements
Browse files Browse the repository at this point in the history
- add new endpoint to query submissions only for newer to given submission id
- improve exception handling
  • Loading branch information
spolnik committed Aug 10, 2018
1 parent 639ca79 commit 7cb58da
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ interface SubmissionsRepository : JpaRepository<Submission, Int> {
fun findBySubmissionId(submissionId: String): Submission?
fun findByProblemId(problemId: String): List<Submission>
fun findBySubmissionTimeLessThan(tillDate: LocalDateTime): List<Submission>
fun findAllByIdGreaterThan(id: Int): List<Submission>
}

43 changes: 22 additions & 21 deletions src/main/kotlin/com/jalgoarena/web/SubmissionsController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,47 +27,38 @@ class SubmissionsController(
private val logger = LoggerFactory.getLogger(this.javaClass)

@GetMapping("/submissions", produces = ["application/json"])
fun findAll(): List<RankingSubmission> = try {
fun findAll(): List<RankingSubmission> = handleExceptions(returnOnException = listOf()) {
submissionsRepository.findAll().map {
RankingSubmission(it.id!!, it.problemId, it.statusCode, it.userId, it.submissionTime, it.elapsedTime)
}
} catch (e: TransactionException) {
logger.error("Cannot connect to database", e)
listOf()
} catch (e: InvalidDataAccessResourceUsageException) {
logger.error("Wrong database schema", e)
listOf()
}

@GetMapping("/submissions/after/{id}", produces = ["application/json"])
fun findAllAfter(
@PathVariable id: Int
): List<RankingSubmission> = handleExceptions(returnOnException = listOf()) {
submissionsRepository.findAllByIdGreaterThan(id).map {
RankingSubmission(it.id!!, it.problemId, it.statusCode, it.userId, it.submissionTime, it.elapsedTime)
}
}

@GetMapping("/submissions/problem/{problemId}", produces = ["application/json"])
fun findAllForProblem(
@PathVariable problemId: String
): List<RankingSubmission> = try {
): List<RankingSubmission> = handleExceptions(returnOnException = listOf()) {
submissionsRepository.findByProblemId(problemId).map {
RankingSubmission(it.id!!, it.problemId, it.statusCode, it.userId, it.submissionTime, it.elapsedTime)
}
} catch (e: TransactionException) {
logger.error("Cannot connect to database", e)
listOf()
} catch (e: InvalidDataAccessResourceUsageException) {
logger.error("Wrong database schema", e)
listOf()
}

@GetMapping("/submissions/date/{date}", produces = ["application/json"])
fun findAllForDate(
@PathVariable date: String
): List<RankingSubmission> = try {
): List<RankingSubmission> = handleExceptions(returnOnException = listOf()) {
val tillDate = LocalDate.parse(date, YYYY_MM_DD).plusDays(1).atStartOfDay()
submissionsRepository.findBySubmissionTimeLessThan(tillDate).map {
RankingSubmission(it.id!!, it.problemId, it.statusCode, it.userId, it.submissionTime, it.elapsedTime)
}
} catch (e: TransactionException) {
logger.error("Cannot connect to database", e)
listOf()
} catch (e: InvalidDataAccessResourceUsageException) {
logger.error("Wrong database schema", e)
listOf()
}

@GetMapping("/submissions/{userId}", produces = ["application/json"])
Expand Down Expand Up @@ -99,6 +90,16 @@ class SubmissionsController(
}
}

private fun <T> handleExceptions(returnOnException: T, body: () -> T) = try {
body()
} catch (e: TransactionException) {
logger.error("Cannot connect to database", e)
returnOnException
} catch (e: InvalidDataAccessResourceUsageException) {
logger.error("Wrong database schema", e)
returnOnException
}

private fun <T> checkUser(token: String?, action: (User) -> ResponseEntity<T>): ResponseEntity<T> {
if (token == null) {
return unauthorized()
Expand Down

0 comments on commit 7cb58da

Please sign in to comment.