This repository has been archived by the owner on Jan 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to planets & rename some stuff
- Loading branch information
1 parent
9bc006e
commit 1357be7
Showing
5 changed files
with
126 additions
and
10 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/commonMain/kotlin/com/gabriel/lunala/project/entity/Planet.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/commonMain/kotlin/com/gabriel/lunala/project/service/PlanetService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } |