Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Kotlin 一條龍 - 打造全平台應用

聽完這場分享後,您將了解如何用 Kotlin 打造全平台應用,並且對 Ktor 的 Client 元件及 Server 運作有更進一步的認識。

詳細資訊:https://tw.kotlin.tips/webinars/r6tibc/full-stack-development-with-kotlin

Related Books

Free with a 30 day trial from Scribd

See all
  • Be the first to comment

  • Be the first to like this

Kotlin 一條龍 - 打造全平台應用

  1. 1. Kotlin 
 — (Shengyou Fan) JetBrains 2020/07/24
  2. 2.
  3. 3. API — { "data": [ { "id": "...UUID...", "title": "...", "description": "...", "completed": true } ] }
  4. 4. Ktor — https://ktor.io/ • Web Framework • Asynchronous • Servers + Clients • Developed by JetBrains • Open Source (Apache 2.0)
  5. 5. Ktor API — Server Database Client Kotlin/JVM
  6. 6. — https://plugins.jetbrains.com/plugin/10823-ktor
  7. 7. Exposed
 Kotlin SQL Framework — https://github.com/JetBrains/Exposed
  8. 8. — // gradle.properties exposed_version=0.21.1 h2_version=1.4.200 // build.gradle dependencies { //... // Exposed SQL Library implementation "org.jetbrains.exposed:exposed-core:$ver" implementation "org.jetbrains.exposed:exposed-dao:$ver" implementation "org.jetbrains.exposed:exposed-jdbc:$ver" implementation "org.jetbrains.exposed:exposed-jodatime:$ver" // H2 database implementation "com.h2database:h2:$ver" }
  9. 9. DAO — object Tasks : UUIDTable() { val title = varchar("title", 255) val description = text("description") var completed = bool("completed") val createdAt = datetime("created_at") val updatedAt = datetime("updated_at") } class Task(id: EntityID<UUID>) : UUIDEntity(id) { companion object : UUIDEntityClass<Task>(Tasks) var title by Tasks.title var description by Tasks.description var completed by Tasks.completed var createdAt by Tasks.createdAt var updatedAt by Tasks.updatedAt }
  10. 10. — data class TaskResponse ( val id:UUID, val title: String, val description: String, val completed: Boolean )
  11. 11. Ktor Feature — Client Ktor App HTTP Server Feature Feature Feature
  12. 12. — // build.gradle dependencies { //... // GSON implementation "io.ktor:ktor-gson:$ver" }
  13. 13. Feature — install(ContentNegotiation) { gson { } }
  14. 14. — Database.connect( url = "...", driver = "..." )
  15. 15. — transaction { SchemaUtils.create(Tasks) } transaction { for (i in 1..10) { val task = Task.new { title = "Task $i" description = "..." completed = false createdAt = DateTime.now() updatedAt = DateTime.now() } } }
  16. 16. Routing — routing { }
  17. 17. HTTP Method — routing { get("/api/tasks") { } }
  18. 18. val tasks = transaction { Task.all().map { TaskResponse( it.id.value, it.title, it.description, it.completed, it.createdAt.toString("..."), it.updatedAt.toString("...") ) } } —
  19. 19. Data Class — get("/api/tasks") { // ... call.respond(mapOf("data" to tasks)) }
  20. 20. — post("/api/tasks") { val request = call.receive<TaskRequest>() val data = transaction { val task = Task.new { title = request.title description = request.description completed = false createdAt = DateTime.now() updatedAt = DateTime.now() } // ... } call.respond(mapOf("data" to data)) }
  21. 21. — patch("/api/tasks/{id}/complete") { val id = UUID.fromString(call.parameters["id"]) val data = transaction { val task = Task.findById(id) if (task != null) { task.completed = true } else { throw ModelNotFoundException() } } call.respond(mapOf("data" to data)) }
  22. 22. — delete("/api/tasks/{id}/delete") { val id = UUID.fromString(call.parameters["id"]) transaction { val task = Task.findById(id) if (task != null) { task.delete() } else { throw ModelNotFoundException() } } call.respond(HttpStatusCode.NoContent) }
  23. 23. — // build.gradle plugins { id 'com.google.cloud.tools.jib' version '2.2.0' } $ gradle jib --image=<MY IMAGE> https://github.com/GoogleContainerTools/jib
  24. 24. — • Ktor • Application Call Request & Response • Routing • API (JSON) • Exposed DB
  25. 25. (Shengyou) shengyou.fan@jetbrains.com Q&A — Kotlin

×