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

Commit

Permalink
Add support to planets & rename some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
SrGaabriel committed Nov 8, 2020
1 parent 9bc006e commit 1357be7
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 10 deletions.
32 changes: 32 additions & 0 deletions src/commonMain/kotlin/com/gabriel/lunala/project/entity/Planet.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.gabriel.lunala.project.entity

import kotlinx.serialization.Serializable

@Serializable
data class Planet(
val name: String,
val distance: Long,
val security: Float,
val owner: Long?,
val galaxy: String,
val visited: Boolean,
)

@Serializable
data class PlanetCreateDTO(
val name: String? = null,
val distance: Long? = null,
val security: Float? = null,
val owner: Long? = null,
val galaxy: String? = null,
val visited: Boolean = false
)

@Serializable
data class PlanetUpdateDTO(
val distance: Long? = null,
val security: Float? = null,
val owner: Long? = null,
val galaxy: String? = null,
val visited: Boolean? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ 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") {
class GuildService(private val wrapper: LunalaWrapper, private val endpoint: String = "${wrapper.url}/guilds") {

suspend fun create(create: GuildCreateDTO) = wrapper.client.post<Guild>(route) {
suspend fun create(create: GuildCreateDTO) = wrapper.client.post<Guild>(endpoint) {
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") {
suspend fun retrieve(id: Long) = wrapper.client.get<Guild>("$endpoint/$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") {
suspend fun update(id: Long, update: GuildUpdateDTO): Guild = wrapper.client.put("$endpoint/$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") {
suspend fun delete(id: Long) = wrapper.client.delete<HttpStatusCode>("$endpoint/$id") {
header(HttpHeaders.Authorization, wrapper.key)
header("Content-Type", "application/json; charset=utf-8")
}
Expand Down
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.Planet
import com.gabriel.lunala.project.entity.PlanetCreateDTO
import com.gabriel.lunala.project.entity.PlanetUpdateDTO
import io.ktor.client.request.*
import io.ktor.http.*

class PlanetService(private val wrapper: LunalaWrapper, private val endpoint: String = "${wrapper.url}/planets") {

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

body = create
}

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

suspend fun update(id: String, update: PlanetUpdateDTO): Planet = wrapper.client.put("$endpoint/$id") {
header(HttpHeaders.Authorization, wrapper.key)
header("Content-Type", "application/json; charset=utf-8")

body = update
}

suspend fun delete(id: String) = wrapper.client.delete<HttpStatusCode>("$endpoint/$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 @@ -8,28 +8,28 @@ 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") {
class UserService(private val wrapper: LunalaWrapper, private val endpoint: String = "${wrapper.url}/users") {

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

body = create
}

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

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

body = update
}

suspend fun delete(id: Long) = wrapper.client.delete<HttpStatusCode>("$route/$id") {
suspend fun delete(id: Long) = wrapper.client.delete<HttpStatusCode>("$endpoint/$id") {
header(HttpHeaders.Authorization, wrapper.key)
header(HttpHeaders.ContentType, ContentType.Application.Default)
}
Expand Down
49 changes: 49 additions & 0 deletions src/jvmTest/kotlin/planets.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.Planet
import com.gabriel.lunala.project.entity.PlanetCreateDTO
import com.gabriel.lunala.project.entity.PlanetUpdateDTO
import com.gabriel.lunala.project.service.PlanetService
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 PlanetOperationsTest {

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

}

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

suspend fun create(service: PlanetService, block: Planet.() -> Unit) =
service.create(PlanetCreateDTO("Earth", 0, 100f, null, "Milky Way", false)).also(block)

suspend fun update(service: PlanetService, block: Planet.() -> Unit) =
service.update("Earth", PlanetUpdateDTO(owner = 360162870069166080)).also(block)

suspend fun get(service: PlanetService, block: Planet.() -> Unit) =
service.retrieve("Earth").also(block)

suspend fun delete(service: PlanetService, id: String = "Earth", block: String.() -> Unit) =
service.delete(id).also { block(id) }

0 comments on commit 1357be7

Please sign in to comment.