|
| 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