Skip to content

Commit

Permalink
Add JSON:API support
Browse files Browse the repository at this point in the history
  • Loading branch information
Brutus5000 committed Oct 30, 2023
1 parent 2ccf801 commit 718e2d5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/main/kotlin/com/faforever/icebreaker/web/JsonApiResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.faforever.icebreaker.web

import com.faforever.icebreaker.service.Session

data class JsonApiResponse(
val data: DataObject<*>,
) {
data class DataObject<T>(
val type: String,
val id: String,
val attributes: T,
)
}

fun Session.toJsonApiResponse() =
JsonApiResponse(
data = JsonApiResponse.DataObject(
type = "iceSession",
id = id,
attributes = mapOf<String, Any>(
"servers" to servers,
),
),
)
13 changes: 13 additions & 0 deletions src/main/kotlin/com/faforever/icebreaker/web/SessionController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,28 @@ import io.quarkus.security.PermissionsAllowed
import jakarta.inject.Singleton
import jakarta.ws.rs.GET
import jakarta.ws.rs.Path
import jakarta.ws.rs.Produces
import jakarta.ws.rs.core.MediaType
import org.jboss.resteasy.reactive.RestPath

@Path("/session")
@Singleton
class SessionController(private val sessionService: SessionService) {
companion object {
private const val MEDIA_TYPE_JSON_API = "application/vnd.api+json"
}

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/game/{gameId}")
@PermissionsAllowed("USER:lobby")
fun getSession(@RestPath gameId: Long): Session =
sessionService.getSession(gameId)

@GET
@Produces(MEDIA_TYPE_JSON_API)
@Path("/game/{gameId}")
@PermissionsAllowed("USER:lobby")
fun getSessionJsonApi(@RestPath gameId: Long): JsonApiResponse =
sessionService.getSession(gameId).toJsonApiResponse()
}

0 comments on commit 718e2d5

Please sign in to comment.