Skip to content

Commit

Permalink
Fast forward for the initial state. The tutorial should start from th…
Browse files Browse the repository at this point in the history
…ere.
  • Loading branch information
antonarhipov committed Dec 3, 2020
1 parent fca5112 commit ebff3ce
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 16 deletions.
3 changes: 1 addition & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id("org.springframework.boot") version "2.4.0-SNAPSHOT"
id("org.springframework.boot") version "2.4.0"
id("io.spring.dependency-management") version "1.0.10.RELEASE"
kotlin("jvm") version "1.4.10"
kotlin("plugin.spring") version "1.4.10"
Expand All @@ -15,7 +15,6 @@ repositories {
mavenCentral()
jcenter()
maven { url = uri("https://repo.spring.io/milestone") }
maven { url = uri("https://repo.spring.io/snapshot") }
}

dependencies {
Expand Down
14 changes: 0 additions & 14 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,15 +1 @@
pluginManagement {
repositories {
maven { url = uri("https://repo.spring.io/milestone") }
maven { url = uri("https://repo.spring.io/snapshot") }
gradlePluginPortal()
}
resolutionStrategy {
eachPlugin {
if (requested.id.id == "org.springframework.boot") {
useModule("org.springframework.boot:spring-boot-gradle-plugin:${requested.version}")
}
}
}
}
rootProject.name = "chat-kotlin"
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.kotlin.chat.controller

import com.example.kotlin.chat.service.MessageService
import com.example.kotlin.chat.service.vm.MessageVM
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.ui.set
import org.springframework.web.bind.annotation.GetMapping

@Controller
class HtmlController(val messageService: MessageService) {

@GetMapping("/")
fun index(model: Model): String {
val messages: List<MessageVM> = messageService.latest()

model["messages"] = messages
model["lastMessageId"] = messages.lastOrNull()?.id ?: ""

return "chat"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.example.kotlin.chat.controller

import com.example.kotlin.chat.service.MessageService
import com.example.kotlin.chat.service.vm.MessageVM
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/api/v1/messages")
class MessageResource(val messageService: MessageService) {

@GetMapping
fun latest(@RequestParam(value = "lastMessageId", defaultValue = "") lastMessageId: String): ResponseEntity<List<MessageVM>> {
val messages = if (lastMessageId.isNotEmpty()) {
messageService.after(lastMessageId)
} else {
messageService.latest()
}

return if (messages.isEmpty()) {
with(ResponseEntity.noContent()) {
header("lastMessageId", lastMessageId)
build<List<MessageVM>>()
}
} else {
with(ResponseEntity.ok()) {
header("lastMessageId", messages.last().id)
body(messages)
}
}
}

@PostMapping
fun post(@RequestBody message: MessageVM) {
messageService.post(message)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.example.kotlin.chat.service.impl

import com.example.kotlin.chat.service.MessageService
import com.example.kotlin.chat.service.vm.MessageVM
import com.example.kotlin.chat.service.vm.UserVM
import com.github.javafaker.Faker
import org.springframework.stereotype.Service
import java.net.URL
import java.time.Instant
import kotlin.random.Random

@Service
class FakeMessageService : MessageService {

val users: Map<String, UserVM> = mapOf(
"Shakespeare" to UserVM("Shakespeare", URL("https://blog.12min.com/wp-content/uploads/2018/05/27d-William-Shakespeare.jpg")),
"RickAndMorty" to UserVM("RickAndMorty", URL("http://thecircular.org/wp-content/uploads/2015/04/rick-and-morty-fb-pic1.jpg")),
"Yoda" to UserVM("Yoda", URL("https://news.toyark.com/wp-content/uploads/sites/4/2019/03/SH-Figuarts-Yoda-001.jpg"))
)

val usersQuotes: Map<String, () -> String> = mapOf(
"Shakespeare" to { Faker.instance().shakespeare().asYouLikeItQuote() },
"RickAndMorty" to { Faker.instance().rickAndMorty().quote() },
"Yoda" to { Faker.instance().yoda().quote() }
)

override fun latest(): List<MessageVM> {
val count = Random.nextInt(1, 15)
return (0..count).map {
val user = users.values.random()
val userQuote = usersQuotes.getValue(user.name).invoke()

MessageVM(userQuote, user, Instant.now(), Random.nextBytes(10).toString())
}.toList()
}

override fun after(messageId: String): List<MessageVM> {
return latest()
}

override fun post(message: MessageVM) {
TODO("Not yet implemented")
}
}

0 comments on commit ebff3ce

Please sign in to comment.