[+] Add @ API macro

This commit is contained in:
Azalea
2024-02-20 15:46:48 -05:00
parent befa7d0e8e
commit 0567e0f251
5 changed files with 17 additions and 12 deletions

View File

@@ -11,12 +11,14 @@ import kotlinx.serialization.json.Json
import org.springframework.http.HttpStatus import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestHeader 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.bind.annotation.RequestParam
import org.springframework.web.server.ResponseStatusException import org.springframework.web.server.ResponseStatusException
typealias RP = RequestParam typealias RP = RequestParam
typealias RB = RequestBody typealias RB = RequestBody
typealias RH = RequestHeader typealias RH = RequestHeader
typealias API = RequestMapping
typealias Str = String typealias Str = String
typealias Bool = Boolean typealias Bool = Boolean

View File

@@ -1,17 +1,17 @@
package icu.samnyan.aqua.api.controller.sega.game.maimai2 package icu.samnyan.aqua.api.controller.sega.game.maimai2
import ext.API
import ext.RP import ext.RP
import ext.minus import ext.minus
import icu.samnyan.aqua.sega.maimai2.dao.userdata.UserDataRepository 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.UserGeneralDataRepository
import icu.samnyan.aqua.sega.maimai2.dao.userdata.UserPlaylogRepository import icu.samnyan.aqua.sega.maimai2.dao.userdata.UserPlaylogRepository
import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController import org.springframework.web.bind.annotation.RestController
import kotlin.jvm.optionals.getOrNull import kotlin.jvm.optionals.getOrNull
@RestController @RestController
@RequestMapping("api/game/maimai2new") @API("api/game/maimai2new")
class Maimai2New( class Maimai2New(
private val userPlaylogRepository: UserPlaylogRepository, private val userPlaylogRepository: UserPlaylogRepository,
private val userDataRepository: UserDataRepository, private val userDataRepository: UserDataRepository,
@@ -20,7 +20,7 @@ class Maimai2New(
{ {
data class TrendOut(val date: String, val rating: Int, val plays: Int) data class TrendOut(val date: String, val rating: Int, val plays: Int)
@GetMapping("trend") @API("trend")
fun trend(@RP userId: Long): List<TrendOut> { fun trend(@RP userId: Long): List<TrendOut> {
// O(n log n) sort // O(n log n) sort
val d = userPlaylogRepository.findByUser_Card_ExtId(userId).sortedBy { it.playDate }.toList() val d = userPlaylogRepository.findByUser_Card_ExtId(userId).sortedBy { it.playDate }.toList()
@@ -46,7 +46,7 @@ class Maimai2New(
98.0 to "S+", 98.0 to "S+",
97.0 to "S").map { (k, v) -> (k * 10000).toInt() to v } 97.0 to "S").map { (k, v) -> (k * 10000).toInt() to v }
@GetMapping("user-summary") @API("user-summary")
fun userSummary(@RP userId: Long): Map<String, Any> { fun userSummary(@RP userId: Long): Map<String, Any> {
// Summary values: total plays, player rating, server-wide ranking // Summary values: total plays, player rating, server-wide ranking
// number of each rank, max combo, number of full combo, number of all perfect // number of each rank, max combo, number of full combo, number of all perfect

View File

@@ -14,14 +14,14 @@ import org.springframework.web.bind.annotation.RestController
import kotlin.jvm.optionals.getOrNull import kotlin.jvm.optionals.getOrNull
@RestController @RestController
@RequestMapping("/api/v2/card") @API("/api/v2/card")
class CardController( class CardController(
val userRepo: AquaNetUserRepo, val userRepo: AquaNetUserRepo,
val jwt: JWT, val jwt: JWT,
val cardService: CardService, val cardService: CardService,
val cardSummary: CardSummary, val cardSummary: CardSummary,
) { ) {
@RequestMapping("/summary") @API("/summary")
suspend fun summary(@RP cardId: Str): Any suspend fun summary(@RP cardId: Str): Any
{ {
val card = cardService.tryLookup(cardId) ?: (404 - "Card not found") val card = cardService.tryLookup(cardId) ?: (404 - "Card not found")

View File

@@ -21,7 +21,7 @@ import java.time.LocalDateTime
import java.util.Random import java.util.Random
@RestController @RestController
@RequestMapping("/api/v2/user") @API("/api/v2/user")
class UserRegistrar( class UserRegistrar(
val userRepo: AquaNetUserRepo, val userRepo: AquaNetUserRepo,
val hasher: PasswordEncoder, val hasher: PasswordEncoder,
@@ -42,7 +42,7 @@ class UserRegistrar(
/** /**
* Register a new user * Register a new user
*/ */
@PostMapping("/register") @API("/register")
suspend fun register( suspend fun register(
@RP username: Str, @RP email: Str, @RP password: Str, @RP turnstile: Str, @RP username: Str, @RP email: Str, @RP password: Str, @RP turnstile: Str,
request: HttpServletRequest request: HttpServletRequest
@@ -106,7 +106,7 @@ class UserRegistrar(
return mapOf("success" to true) return mapOf("success" to true)
} }
@PostMapping("/login") @API("/login")
suspend fun login( suspend fun login(
@RP email: Str, @RP password: Str, @RP turnstile: Str, @RP email: Str, @RP password: Str, @RP turnstile: Str,
request: HttpServletRequest request: HttpServletRequest
@@ -127,7 +127,7 @@ class UserRegistrar(
return mapOf("token" to token) return mapOf("token" to token)
} }
@PostMapping("/confirm-email") @API("/confirm-email")
suspend fun confirmEmail(@RP token: Str): Any { suspend fun confirmEmail(@RP token: Str): Any {
// Find the confirmation // Find the confirmation
val confirmation = async { confirmationRepo.findByToken(token) } val confirmation = async { confirmationRepo.findByToken(token) }
@@ -141,9 +141,9 @@ class UserRegistrar(
// Confirm the email // Confirm the email
async { userRepo.save(confirmation.aquaNetUser.apply { emailConfirmed = true }) } 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) suspend fun getUser(@RP token: Str) = jwt.auth(token)
} }

View File

@@ -5,6 +5,9 @@ import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.ControllerAdvice import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler 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) class ApiException(val code: Int, message: Str) : RuntimeException(message)
@ControllerAdvice @ControllerAdvice