diff --git a/src/main/java/ext/Ext.kt b/src/main/java/ext/Ext.kt index 89fe0326..e707b8fb 100644 --- a/src/main/java/ext/Ext.kt +++ b/src/main/java/ext/Ext.kt @@ -11,12 +11,14 @@ import kotlinx.serialization.json.Json 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.RequestMapping import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.server.ResponseStatusException typealias RP = RequestParam typealias RB = RequestBody typealias RH = RequestHeader +typealias API = RequestMapping typealias Str = String typealias Bool = Boolean 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 1a3eb2df..f7ae185c 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,17 +1,17 @@ package icu.samnyan.aqua.api.controller.sega.game.maimai2 +import ext.API import ext.RP import ext.minus import icu.samnyan.aqua.sega.maimai2.dao.userdata.UserDataRepository import icu.samnyan.aqua.sega.maimai2.dao.userdata.UserGeneralDataRepository import icu.samnyan.aqua.sega.maimai2.dao.userdata.UserPlaylogRepository import org.springframework.web.bind.annotation.GetMapping -import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController import kotlin.jvm.optionals.getOrNull @RestController -@RequestMapping("api/game/maimai2new") +@API("api/game/maimai2new") class Maimai2New( private val userPlaylogRepository: UserPlaylogRepository, private val userDataRepository: UserDataRepository, @@ -20,7 +20,7 @@ class Maimai2New( { data class TrendOut(val date: String, val rating: Int, val plays: Int) - @GetMapping("trend") + @API("trend") fun trend(@RP userId: Long): List { // O(n log n) sort val d = userPlaylogRepository.findByUser_Card_ExtId(userId).sortedBy { it.playDate }.toList() @@ -46,7 +46,7 @@ class Maimai2New( 98.0 to "S+", 97.0 to "S").map { (k, v) -> (k * 10000).toInt() to v } - @GetMapping("user-summary") + @API("user-summary") 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 diff --git a/src/main/java/icu/samnyan/aqua/net/CardController.kt b/src/main/java/icu/samnyan/aqua/net/CardController.kt index 3fe482c8..19504bf9 100644 --- a/src/main/java/icu/samnyan/aqua/net/CardController.kt +++ b/src/main/java/icu/samnyan/aqua/net/CardController.kt @@ -14,14 +14,14 @@ import org.springframework.web.bind.annotation.RestController import kotlin.jvm.optionals.getOrNull @RestController -@RequestMapping("/api/v2/card") +@API("/api/v2/card") class CardController( val userRepo: AquaNetUserRepo, val jwt: JWT, val cardService: CardService, val cardSummary: CardSummary, ) { - @RequestMapping("/summary") + @API("/summary") suspend fun summary(@RP cardId: Str): Any { val card = cardService.tryLookup(cardId) ?: (404 - "Card not found") diff --git a/src/main/java/icu/samnyan/aqua/net/UserRegistrar.kt b/src/main/java/icu/samnyan/aqua/net/UserRegistrar.kt index c16ac97d..dfa2b1e3 100644 --- a/src/main/java/icu/samnyan/aqua/net/UserRegistrar.kt +++ b/src/main/java/icu/samnyan/aqua/net/UserRegistrar.kt @@ -21,7 +21,7 @@ import java.time.LocalDateTime import java.util.Random @RestController -@RequestMapping("/api/v2/user") +@API("/api/v2/user") class UserRegistrar( val userRepo: AquaNetUserRepo, val hasher: PasswordEncoder, @@ -42,7 +42,7 @@ class UserRegistrar( /** * Register a new user */ - @PostMapping("/register") + @API("/register") suspend fun register( @RP username: Str, @RP email: Str, @RP password: Str, @RP turnstile: Str, request: HttpServletRequest @@ -106,7 +106,7 @@ class UserRegistrar( return mapOf("success" to true) } - @PostMapping("/login") + @API("/login") suspend fun login( @RP email: Str, @RP password: Str, @RP turnstile: Str, request: HttpServletRequest @@ -127,7 +127,7 @@ class UserRegistrar( return mapOf("token" to token) } - @PostMapping("/confirm-email") + @API("/confirm-email") suspend fun confirmEmail(@RP token: Str): Any { // Find the confirmation val confirmation = async { confirmationRepo.findByToken(token) } @@ -141,9 +141,9 @@ class UserRegistrar( // Confirm the email async { userRepo.save(confirmation.aquaNetUser.apply { emailConfirmed = true }) } - return mapOf("success" to true) + return SUCCESS } - @PostMapping("/me") + @API("/me") suspend fun getUser(@RP token: Str) = jwt.auth(token) } \ No newline at end of file diff --git a/src/main/java/icu/samnyan/aqua/net/utils/ErrorResponse.kt b/src/main/java/icu/samnyan/aqua/net/utils/ErrorResponse.kt index 12a1032a..269ee309 100644 --- a/src/main/java/icu/samnyan/aqua/net/utils/ErrorResponse.kt +++ b/src/main/java/icu/samnyan/aqua/net/utils/ErrorResponse.kt @@ -5,6 +5,9 @@ import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.ControllerAdvice import org.springframework.web.bind.annotation.ExceptionHandler + +val SUCCESS = ResponseEntity.ok().body(mapOf("status" to "ok")) + class ApiException(val code: Int, message: Str) : RuntimeException(message) @ControllerAdvice