[+] Add @ API macro

pull/14/head
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.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

View File

@ -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<TrendOut> {
// 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<String, Any> {
// Summary values: total plays, player rating, server-wide ranking
// 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
@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")

View File

@ -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)
}

View File

@ -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