Skip to content
This repository has been archived by the owner on Jan 4, 2022. It is now read-only.

Commit

Permalink
Support guilds, provide more data to users & bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SrGaabriel committed Nov 8, 2020
1 parent 6749011 commit 9bc006e
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 27 deletions.
23 changes: 23 additions & 0 deletions src/commonMain/kotlin/com/gabriel/lunala/project/entity/Guild.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.gabriel.lunala.project.entity

import kotlinx.serialization.Serializable

@Serializable
data class Guild(
val id: Long,
val locale: String,
val partner: Boolean
)

@Serializable
data class GuildCreateDTO(
val id: Long,
val locale: String,
val partner: Boolean
)

@Serializable
data class GuildUpdateDTO(
val locale: String? = null,
val partner: Boolean? = null
)
33 changes: 30 additions & 3 deletions src/commonMain/kotlin/com/gabriel/lunala/project/entity/User.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
package com.gabriel.lunala.project.entity

import com.gabriel.lunala.project.util.PremiumType
import kotlinx.serialization.Serializable

@Serializable
data class User(val id: Long, val coins: Long, val planet: String)
data class User(
val id: Long,
val ship: Int,
val coins: Long,
val equipment: Int,
val crew: Int,
val planet: String,
val galaxy: String,
val premium: PremiumType
)

@Serializable
data class UserCreateDTO(val id: Long, val coins: Long, val planet: String)
data class UserCreateDTO(
val id: Long,
val ship: Int,
val coins: Long,
val equipment: Int,
val crew: Int,
val planet: String,
val galaxy: String,
val premium: PremiumType
)

@Serializable
data class UserUpdateDTO(val coins: Long?, val planet: String?)
data class UserUpdateDTO(
val ship: Int? = null,
val coins: Long? = null,
val equipment: Int? = null,
val crew: Int? = null,
val planet: String? = null,
val galaxy: String? = null,
val premium: PremiumType? = null
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.gabriel.lunala.project.service

import com.gabriel.lunala.project.LunalaWrapper
import com.gabriel.lunala.project.entity.Guild
import com.gabriel.lunala.project.entity.GuildCreateDTO
import com.gabriel.lunala.project.entity.GuildUpdateDTO
import io.ktor.client.request.*
import io.ktor.http.*

class GuildService(private val wrapper: LunalaWrapper, private val route: String = "${wrapper.url}/guilds") {

suspend fun create(create: GuildCreateDTO) = wrapper.client.post<Guild>(route) {
header(HttpHeaders.Authorization, wrapper.key)
header("Content-Type", "application/json; charset=utf-8")

body = create
}

suspend fun retrieve(id: Long) = wrapper.client.get<Guild>("$route/$id") {
header(HttpHeaders.Authorization, wrapper.key)
header("Content-Type", "application/json; charset=utf-8")
}

suspend fun update(id: Long, update: GuildUpdateDTO): Guild = wrapper.client.put("$route/$id") {
header(HttpHeaders.Authorization, wrapper.key)
header("Content-Type", "application/json; charset=utf-8")

body = update
}

suspend fun delete(id: Long) = wrapper.client.delete<HttpStatusCode>("$route/$id") {
header(HttpHeaders.Authorization, wrapper.key)
header("Content-Type", "application/json; charset=utf-8")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,33 @@ import com.gabriel.lunala.project.LunalaWrapper
import com.gabriel.lunala.project.entity.User
import com.gabriel.lunala.project.entity.UserCreateDTO
import com.gabriel.lunala.project.entity.UserUpdateDTO
import com.gabriel.lunala.project.util.applyHeaders
import com.gabriel.lunala.project.util.Default
import io.ktor.client.request.*
import io.ktor.http.*

class UserService(private val wrapper: LunalaWrapper, private val route: String = "${wrapper.url}/users") {

suspend fun create(create: UserCreateDTO) = wrapper.client.post<User>(route) {
header(HttpHeaders.Authorization, wrapper.key)
header(HttpHeaders.ContentType, ContentType.Application.Default)

body = create
applyHeaders(wrapper)
}

suspend fun retrieve(id: Long) = wrapper.client.get<User>("$route/$id") {
applyHeaders(wrapper)
header(HttpHeaders.Authorization, wrapper.key)
header(HttpHeaders.ContentType, ContentType.Application.Default)
}

suspend fun update(id: Long, update: UserUpdateDTO): User = wrapper.client.put("$route/$id") {
header(HttpHeaders.Authorization, wrapper.key)
header(HttpHeaders.ContentType, ContentType.Application.Default)

body = update
applyHeaders(wrapper)
}

suspend fun delete(id: Long) = wrapper.client.delete<HttpStatusCode>("$route/$id") {
applyHeaders(wrapper)
header(HttpHeaders.Authorization, wrapper.key)
header(HttpHeaders.ContentType, ContentType.Application.Default)
}
}
15 changes: 4 additions & 11 deletions src/commonMain/kotlin/com/gabriel/lunala/project/util/Client.kt
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
package com.gabriel.lunala.project.util

import com.gabriel.lunala.project.LunalaWrapper
import io.ktor.client.*
import io.ktor.client.engine.*
import io.ktor.client.features.json.*
import io.ktor.client.features.json.serializer.*
import io.ktor.client.request.*
import io.ktor.client.utils.*
import io.ktor.http.*
import io.ktor.util.*

val ContentType.Application.Default
get() = "application/json; charset=utf-8"

@KtorExperimentalAPI
fun LunalaWrapper.prepareClient() = client.config {
fun HttpClientConfig<*>.prepareClient() = apply {
install(JsonFeature) {
serializer = KotlinxSerializer()
acceptContentTypes = acceptContentTypes + ContentType("application", "json")
}
}.let { this }

// TODO: workaround
fun HttpRequestBuilder.applyHeaders(wrapper: LunalaWrapper) {
header(HttpHeaders.Authorization, wrapper.key)
header(HttpHeaders.ContentType, ContentType.Application.Json)
}
36 changes: 36 additions & 0 deletions src/commonMain/kotlin/com/gabriel/lunala/project/util/Premium.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.gabriel.lunala.project.util

import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

@Serializable(with = PremiumSerializer::class)
enum class PremiumType(val id: Int) {

NONE(0),
STAR(1),
SUPERNOVA(2),
BLACK_HOLE(3);

companion object {
fun findById(id: Int): PremiumType? = values().firstOrNull {
it.id == id
}
}

}

class PremiumSerializer: KSerializer<PremiumType> {

override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("premium_type", PrimitiveKind.INT)

override fun deserialize(decoder: Decoder): PremiumType =
PremiumType.findById(decoder.decodeInt())!!

override fun serialize(encoder: Encoder, value: PremiumType) =
encoder.encodeInt(value.ordinal)
}
49 changes: 49 additions & 0 deletions src/jvmTest/kotlin/guilds.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import com.gabriel.lunala.project.LunalaWrapper
import com.gabriel.lunala.project.entity.Guild
import com.gabriel.lunala.project.entity.GuildCreateDTO
import com.gabriel.lunala.project.entity.GuildUpdateDTO
import com.gabriel.lunala.project.service.GuildService
import com.gabriel.lunala.project.util.prepareClient
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.util.*
import org.junit.jupiter.api.Test

class GuildOperationsTest {

@Test
@KtorExperimentalAPI
suspend fun `test operations`() {
operate(GuildService(LunalaWrapper("http://localhost:8080/api", "key", client = HttpClient(CIO.create()) {
prepareClient()
})))
}

}

private suspend fun operate(service: GuildService) {
create(service) {
println("Created $this")
}
update(service) {
println("Updated $this")
}
get(service) {
println("Got $this")
}
delete(service) {
println("Deleted $this")
}
}

suspend fun create(service: GuildService, block: Guild.() -> Unit) =
service.create(GuildCreateDTO(1, "en-us", false)).also(block)

suspend fun update(service: GuildService, block: Guild.() -> Unit) =
service.update(1, GuildUpdateDTO(locale = "pt-br")).also(block)

suspend fun get(service: GuildService, block: Guild.() -> Unit) =
service.retrieve(1).also(block)

suspend fun delete(service: GuildService, id: Long = 1, block: Long.() -> Unit) =
service.delete(id).also { block(id) }
17 changes: 9 additions & 8 deletions src/jvmTest/kotlin/users.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@ import com.gabriel.lunala.project.entity.User
import com.gabriel.lunala.project.entity.UserCreateDTO
import com.gabriel.lunala.project.entity.UserUpdateDTO
import com.gabriel.lunala.project.service.UserService
import com.gabriel.lunala.project.util.PremiumType
import com.gabriel.lunala.project.util.prepareClient
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.util.*
import org.junit.jupiter.api.Test

class OperationsTest {
class UserOperationsTest {

@Test
@KtorExperimentalAPI
suspend fun `test operations`() {
operate(UserService(LunalaWrapper("http://localhost:8080/api", "key", client = HttpClient(CIO.create())).prepareClient()))
operate(UserService(LunalaWrapper("http://localhost:8080/api", "key", client = HttpClient(CIO.create()) {
prepareClient()
})))
}

}

suspend fun operate(service: UserService) {
private suspend fun operate(service: UserService) {
create(service) {
println("Created $this")
}
Expand All @@ -35,15 +38,13 @@ suspend fun operate(service: UserService) {
}

suspend fun create(service: UserService, block: User.() -> Unit) =
service.create(UserCreateDTO(1, 0, "Earth")).also(block)
service.create(UserCreateDTO(1, 0, 0, 0, 0, "Earth", "Milky Way", PremiumType.STAR)).also(block)

suspend fun update(service: UserService, block: User.() -> Unit) =
service.update(1, UserUpdateDTO(20, null)).also(block)
service.update(1, UserUpdateDTO(coins = 20)).also(block)

suspend fun get(service: UserService, block: User.() -> Unit) =
service.retrieve(1).also(block)

suspend fun delete(service: UserService, id: Long = 1, block: Long.() -> Unit) =
service.delete(id).also { block(id) }

suspend fun main() = OperationsTest().`test operations`()
service.delete(id).also { block(id) }

0 comments on commit 9bc006e

Please sign in to comment.