diff --git a/src/main/java/ext/Ext.kt b/src/main/java/ext/Ext.kt index 6aba3310..6df696da 100644 --- a/src/main/java/ext/Ext.kt +++ b/src/main/java/ext/Ext.kt @@ -1,7 +1,34 @@ package ext import org.springframework.http.HttpStatus +import org.springframework.web.bind.annotation.RequestBody +import org.springframework.web.bind.annotation.RequestHeader +import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.server.ResponseStatusException +import io.ktor.client.* +import io.ktor.client.engine.cio.* +import io.ktor.client.plugins.contentnegotiation.* +import io.ktor.serialization.kotlinx.json.* + +typealias RP = RequestParam +typealias RB = RequestBody +typealias RH = RequestHeader +typealias Str = String +typealias Bool = Boolean // Make it easier to throw a ResponseStatusException -operator fun HttpStatus.invoke(message: String? = null): Nothing = throw ResponseStatusException(this, message ?: this.reasonPhrase) \ No newline at end of file +operator fun HttpStatus.invoke(message: String? = null): Nothing = throw ResponseStatusException(this, message ?: this.reasonPhrase) +operator fun Int.compareTo(message: String): Int = throw ResponseStatusException(HttpStatus.valueOf(this), message) + +// Email validation +// https://www.baeldung.com/java-email-validation-regex +val emailRegex = "^(?=.{1,64}@)[\\p{L}0-9_-]+(\\.[\\p{L}0-9_-]+)*@[^-][\\p{L}0-9-]+(\\.[\\p{L}0-9-]+)*(\\.[\\p{L}]{2,})$".toRegex() +fun Str.isValidEmail(): Bool = emailRegex.matches(this) + +fun millis() = System.currentTimeMillis() + +val HTTP = HttpClient(CIO) { + install(ContentNegotiation) { + json() + } +} \ No newline at end of file diff --git a/src/main/java/icu/samnyan/aqua/api/controller/sega/game/maimai2/Maimai2New.kt b/src/main/java/icu/samnyan/aqua/api/controller/sega/game/maimai2/Maimai2New.kt index c2023aee..6f5f59de 100644 --- a/src/main/java/icu/samnyan/aqua/api/controller/sega/game/maimai2/Maimai2New.kt +++ b/src/main/java/icu/samnyan/aqua/api/controller/sega/game/maimai2/Maimai2New.kt @@ -1,5 +1,6 @@ package icu.samnyan.aqua.api.controller.sega.game.maimai2 +import ext.RP import ext.invoke import icu.samnyan.aqua.sega.maimai2.dao.userdata.UserDataRepository import icu.samnyan.aqua.sega.maimai2.dao.userdata.UserGeneralDataRepository @@ -22,7 +23,7 @@ class Maimai2New( data class TrendOut(val date: String, val rating: Int, val plays: Int) @GetMapping("trend") - fun trend(@RequestParam userId: Long): List { + fun trend(@RP userId: Long): List { // O(n log n) sort val d = userPlaylogRepository.findByUser_Card_ExtId(userId).sortedBy { it.playDate }.toList() @@ -44,7 +45,7 @@ class Maimai2New( 97.0 to "S").map { (k, v) -> (k * 10000).toInt() to v } @GetMapping("user-summary") - fun userSummary(@RequestParam userId: Long): Map { + fun userSummary(@RP userId: Long): Map { // Summary values: total plays, player rating, server-wide ranking // number of each rank, max combo, number of full combo, number of all perfect val user = userDataRepository.findByCard_ExtId(userId).getOrNull() ?: NOT_FOUND()