forked from OlegDokuka/chat-kotlin
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fast forward for the initial state. The tutorial should start from th…
…ere.
- Loading branch information
1 parent
fca5112
commit ebff3ce
Showing
5 changed files
with
104 additions
and
16 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -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" |
22 changes: 22 additions & 0 deletions
22
src/main/kotlin/com/example/kotlin/chat/controller/HtmlController.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,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" | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/kotlin/com/example/kotlin/chat/controller/MessageResource.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,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) | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/kotlin/com/example/kotlin/chat/service/impl/FakeMessageService.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,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") | ||
} | ||
} |