diff --git a/src/main/kotlin/com/jalgoarena/data/SubmissionsRepository.kt b/src/main/kotlin/com/jalgoarena/data/SubmissionsRepository.kt index 1b29c0a..d7f426e 100644 --- a/src/main/kotlin/com/jalgoarena/data/SubmissionsRepository.kt +++ b/src/main/kotlin/com/jalgoarena/data/SubmissionsRepository.kt @@ -9,5 +9,6 @@ interface SubmissionsRepository : JpaRepository { fun findBySubmissionId(submissionId: String): Submission? fun findByProblemId(problemId: String): List fun findBySubmissionTimeLessThan(tillDate: LocalDateTime): List + fun findAllByIdGreaterThan(id: Int): List } diff --git a/src/main/kotlin/com/jalgoarena/web/SubmissionsController.kt b/src/main/kotlin/com/jalgoarena/web/SubmissionsController.kt index 4a1362f..b7ac9f3 100644 --- a/src/main/kotlin/com/jalgoarena/web/SubmissionsController.kt +++ b/src/main/kotlin/com/jalgoarena/web/SubmissionsController.kt @@ -27,47 +27,38 @@ class SubmissionsController( private val logger = LoggerFactory.getLogger(this.javaClass) @GetMapping("/submissions", produces = ["application/json"]) - fun findAll(): List = try { + fun findAll(): List = 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 = 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 = try { + ): List = 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 = try { + ): List = 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"]) @@ -99,6 +90,16 @@ class SubmissionsController( } } + private fun 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 checkUser(token: String?, action: (User) -> ResponseEntity): ResponseEntity { if (token == null) { return unauthorized()