Skip to content

Commit ebff3ce

Browse files
committed
Fast forward for the initial state. The tutorial should start from there.
1 parent fca5112 commit ebff3ce

File tree

5 files changed

+104
-16
lines changed

5 files changed

+104
-16
lines changed

build.gradle.kts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
22

33
plugins {
4-
id("org.springframework.boot") version "2.4.0-SNAPSHOT"
4+
id("org.springframework.boot") version "2.4.0"
55
id("io.spring.dependency-management") version "1.0.10.RELEASE"
66
kotlin("jvm") version "1.4.10"
77
kotlin("plugin.spring") version "1.4.10"
@@ -15,7 +15,6 @@ repositories {
1515
mavenCentral()
1616
jcenter()
1717
maven { url = uri("https://repo.spring.io/milestone") }
18-
maven { url = uri("https://repo.spring.io/snapshot") }
1918
}
2019

2120
dependencies {

settings.gradle.kts

-14
Original file line numberDiff line numberDiff line change
@@ -1,15 +1 @@
1-
pluginManagement {
2-
repositories {
3-
maven { url = uri("https://repo.spring.io/milestone") }
4-
maven { url = uri("https://repo.spring.io/snapshot") }
5-
gradlePluginPortal()
6-
}
7-
resolutionStrategy {
8-
eachPlugin {
9-
if (requested.id.id == "org.springframework.boot") {
10-
useModule("org.springframework.boot:spring-boot-gradle-plugin:${requested.version}")
11-
}
12-
}
13-
}
14-
}
151
rootProject.name = "chat-kotlin"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.example.kotlin.chat.controller
2+
3+
import com.example.kotlin.chat.service.MessageService
4+
import com.example.kotlin.chat.service.vm.MessageVM
5+
import org.springframework.stereotype.Controller
6+
import org.springframework.ui.Model
7+
import org.springframework.ui.set
8+
import org.springframework.web.bind.annotation.GetMapping
9+
10+
@Controller
11+
class HtmlController(val messageService: MessageService) {
12+
13+
@GetMapping("/")
14+
fun index(model: Model): String {
15+
val messages: List<MessageVM> = messageService.latest()
16+
17+
model["messages"] = messages
18+
model["lastMessageId"] = messages.lastOrNull()?.id ?: ""
19+
20+
return "chat"
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.example.kotlin.chat.controller
2+
3+
import com.example.kotlin.chat.service.MessageService
4+
import com.example.kotlin.chat.service.vm.MessageVM
5+
import org.springframework.http.ResponseEntity
6+
import org.springframework.web.bind.annotation.*
7+
8+
@RestController
9+
@RequestMapping("/api/v1/messages")
10+
class MessageResource(val messageService: MessageService) {
11+
12+
@GetMapping
13+
fun latest(@RequestParam(value = "lastMessageId", defaultValue = "") lastMessageId: String): ResponseEntity<List<MessageVM>> {
14+
val messages = if (lastMessageId.isNotEmpty()) {
15+
messageService.after(lastMessageId)
16+
} else {
17+
messageService.latest()
18+
}
19+
20+
return if (messages.isEmpty()) {
21+
with(ResponseEntity.noContent()) {
22+
header("lastMessageId", lastMessageId)
23+
build<List<MessageVM>>()
24+
}
25+
} else {
26+
with(ResponseEntity.ok()) {
27+
header("lastMessageId", messages.last().id)
28+
body(messages)
29+
}
30+
}
31+
}
32+
33+
@PostMapping
34+
fun post(@RequestBody message: MessageVM) {
35+
messageService.post(message)
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.example.kotlin.chat.service.impl
2+
3+
import com.example.kotlin.chat.service.MessageService
4+
import com.example.kotlin.chat.service.vm.MessageVM
5+
import com.example.kotlin.chat.service.vm.UserVM
6+
import com.github.javafaker.Faker
7+
import org.springframework.stereotype.Service
8+
import java.net.URL
9+
import java.time.Instant
10+
import kotlin.random.Random
11+
12+
@Service
13+
class FakeMessageService : MessageService {
14+
15+
val users: Map<String, UserVM> = mapOf(
16+
"Shakespeare" to UserVM("Shakespeare", URL("https://blog.12min.com/wp-content/uploads/2018/05/27d-William-Shakespeare.jpg")),
17+
"RickAndMorty" to UserVM("RickAndMorty", URL("http://thecircular.org/wp-content/uploads/2015/04/rick-and-morty-fb-pic1.jpg")),
18+
"Yoda" to UserVM("Yoda", URL("https://news.toyark.com/wp-content/uploads/sites/4/2019/03/SH-Figuarts-Yoda-001.jpg"))
19+
)
20+
21+
val usersQuotes: Map<String, () -> String> = mapOf(
22+
"Shakespeare" to { Faker.instance().shakespeare().asYouLikeItQuote() },
23+
"RickAndMorty" to { Faker.instance().rickAndMorty().quote() },
24+
"Yoda" to { Faker.instance().yoda().quote() }
25+
)
26+
27+
override fun latest(): List<MessageVM> {
28+
val count = Random.nextInt(1, 15)
29+
return (0..count).map {
30+
val user = users.values.random()
31+
val userQuote = usersQuotes.getValue(user.name).invoke()
32+
33+
MessageVM(userQuote, user, Instant.now(), Random.nextBytes(10).toString())
34+
}.toList()
35+
}
36+
37+
override fun after(messageId: String): List<MessageVM> {
38+
return latest()
39+
}
40+
41+
override fun post(message: MessageVM) {
42+
TODO("Not yet implemented")
43+
}
44+
}

0 commit comments

Comments
 (0)