Skip to content

Commit

Permalink
#20 add kotlinter
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillsinyuk committed Feb 18, 2024
1 parent c50ea34 commit 6465349
Show file tree
Hide file tree
Showing 22 changed files with 118 additions and 115 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[*.{kt,kts}]
ktlint_standard_package-name = disabled
ktlint_standard_value-parameter-comment = disabled

1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "3.2.2"
id("io.spring.dependency-management") version "1.1.4"
id("org.jmailen.kotlinter") version "4.2.0"
kotlin("jvm") version "1.9.22"
kotlin("plugin.spring") version "1.9.22"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import org.springframework.boot.runApplication
class ElasticsearchApplication

fun main(args: Array<String>) {
runApplication<ElasticsearchApplication>(*args)
runApplication<ElasticsearchApplication>(*args)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ import org.springframework.web.bind.annotation.RestController

@RestController
class DealController(
private val searchDealsUseCase: SearchDealsUseCase
private val searchDealsUseCase: SearchDealsUseCase,
) {

@GetMapping("/deals")
fun generateRandomData(@RequestParam search: String, pageable: Pageable): Page<Deal> {
fun generateRandomData(
@RequestParam search: String,
pageable: Pageable,
): Page<Deal> {
logger.info { "Received request for search deals with $search" }
return searchDealsUseCase.searchDeals(search, pageable)
}

companion object: KLogging()
}
companion object : KLogging()
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import org.springframework.web.bind.annotation.RestController

@RestController
class GenerateDataController(
private val generateDealsUseCase: GenerateDealsUseCase
private val generateDealsUseCase: GenerateDealsUseCase,
) {

@PostMapping("/generate")
fun generateRandomData(@RequestParam dealAmount: Int) {
fun generateRandomData(
@RequestParam dealAmount: Int,
) {
logger.info { "Received request for $dealAmount deals generation" }
generateDealsUseCase.generateDeals(dealAmount)
}

companion object: KLogging()
}
companion object : KLogging()
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import org.springframework.data.elasticsearch.annotations.Query
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository

interface ElasticDealRepository : ElasticsearchRepository<Deal, String> {

@Query(query = "{ \"multi_match\" : {\"query\" : \"?0\" }}")
//@Query(query = "\"query\": {\"bool\": {\"must\": [{ \"match\": {\"userId\": ?0}}]}}")
@Query(query = "Jac")
fun searchAll(search: String, page: Pageable): Page<Deal>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import com.kvsinyuk.elasticsearch.domain.Deal
import org.bson.types.ObjectId
import org.springframework.data.mongodb.repository.MongoRepository

interface MongoDealRepository : MongoRepository<Deal, ObjectId>
interface MongoDealRepository : MongoRepository<Deal, ObjectId>
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,18 @@ import org.springframework.stereotype.Component

@Component
class GenerateDealsUseCaseImpl(
private val dealRepository: MongoDealRepository
): GenerateDealsUseCase {

private val dealRepository: MongoDealRepository,
) : GenerateDealsUseCase {
@Value("\${generator.batch.count}")
private var batchCount: Int = 20

override fun generateDeals(dealAmount: Int) {
for (i in dealAmount downTo 0 step batchCount) {
for (i in 0 until dealAmount step batchCount) {
CoroutineScope(Dispatchers.Default).launch {
(0 until batchCount)
.map { generateDeal() }
.let { dealRepository.saveAll(it) }
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import org.springframework.stereotype.Component

@Component
class SearchDealsUseCaseImpl(
private val dealRepository: ElasticDealRepository
): SearchDealsUseCase {

override fun searchDeals(search: String, page: Pageable) =
dealRepository.searchAll(search, page)
}
private val dealRepository: ElasticDealRepository,
) : SearchDealsUseCase {
override fun searchDeals(
search: String,
page: Pageable,
) = dealRepository.searchAll(search, page)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.kvsinyuk.elasticsearch.application.port

interface GenerateDealsUseCase {

fun generateDeals(dealAmount: Int)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable

interface SearchDealsUseCase {

fun searchDeals(search: String, page: Pageable): Page<Deal>
}
fun searchDeals(
search: String,
page: Pageable,
): Page<Deal>
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import org.springframework.data.elasticsearch.repository.config.EnableElasticsea

@Configuration
@EnableElasticsearchRepositories(basePackages = ["com.kvsinyuk.elasticsearch.adapter.out.elasticsearch"])
@ComponentScan(basePackages = [ "com.kvsinyuk.elasticsearch" ])
class ElasticsearchConfiguration
@ComponentScan(basePackages = ["com.kvsinyuk.elasticsearch"])
class ElasticsearchConfiguration
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import org.springframework.data.mongodb.repository.config.EnableMongoRepositorie
@Configuration
@EnableMongoAuditing
@EnableMongoRepositories(basePackages = ["com.kvsinyuk.elasticsearch.adapter.out.mongo"])
@ComponentScan(basePackages = [ "com.kvsinyuk.elasticsearch" ])
class MongoConfiguration
@ComponentScan(basePackages = ["com.kvsinyuk.elasticsearch"])
class MongoConfiguration
3 changes: 1 addition & 2 deletions src/main/kotlin/com/kvsinyuk/elasticsearch/domain/Buyer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package com.kvsinyuk.elasticsearch.domain
import java.util.UUID

class Buyer {

val id: String = UUID.randomUUID().toString()

lateinit var name: Name

lateinit var contacts: Contacts
}
}
4 changes: 2 additions & 2 deletions src/main/kotlin/com/kvsinyuk/elasticsearch/domain/Contacts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package com.kvsinyuk.elasticsearch.domain

data class Contacts(
val email: String,
val phone: String
)
val phone: String,
)
2 changes: 1 addition & 1 deletion src/main/kotlin/com/kvsinyuk/elasticsearch/domain/Deal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ class Deal {

@CreatedDate
lateinit var createdAt: Instant
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package com.kvsinyuk.elasticsearch.domain
import java.util.UUID

class Guarantor {

val id: String = UUID.randomUUID().toString()

lateinit var name: Name

lateinit var contacts: Contacts
}
}
3 changes: 1 addition & 2 deletions src/main/kotlin/com/kvsinyuk/elasticsearch/domain/Name.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ data class Name(
val lastName: String,
val middleName: String?,
) {

fun getFullName() = "$lastName $firstName" + (middleName?.let { " $it" } ?: "")
}
}
3 changes: 1 addition & 2 deletions src/main/kotlin/com/kvsinyuk/elasticsearch/domain/Seller.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package com.kvsinyuk.elasticsearch.domain
import java.util.UUID

class Seller {

val id: String = UUID.randomUUID().toString()

lateinit var name: Name

lateinit var contacts: Contacts
}
}
86 changes: 45 additions & 41 deletions src/main/kotlin/com/kvsinyuk/elasticsearch/utils/GeneratorUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ fun generateDeal(): Deal {
return Deal()
.apply {
number = dealNumberPatternGenerator.generate()
date = LocalDateTime.ofInstant(Instant.ofEpochSecond(ThreadLocalRandom.current().nextLong(-hundredYears, hundredYears)), ZoneId.systemDefault())
date =
LocalDateTime.ofInstant(
Instant.ofEpochSecond(ThreadLocalRandom.current().nextLong(-HUNDRED_YEARS, HUNDRED_YEARS)),
ZoneId.systemDefault(),
)
sellers = createRandomSellers((0 until 10).random())
buyers = createRandomBuyer((0 until 10).random())
guarantors = createRandomGuarantors((0 until 3).random())
Expand Down Expand Up @@ -57,53 +61,53 @@ fun createRandomName() =
Name(
firstName = nameList[nameList.indices.random()],
lastName = nameList[nameList.indices.random()],
middleName = nameList[nameList.indices.random()]
middleName = nameList[nameList.indices.random()],
)

fun createRandomContacts() =
Contacts(
email = emailPatternGenerator.generate(),
phone = phonePatternGenerator.generate()
phone = phonePatternGenerator.generate(),
)


private val dealNumberPatternGenerator = RgxGen("TEST-\\d{2}-\\d{5}")
private val emailPatternGenerator = RgxGen("[a-z]{5}@[a-z]{4}\\.com")
private val phonePatternGenerator = RgxGen("7\\d{10}")
private const val hundredYears = 100 * 365L
private val nameList = arrayOf(
"Ethan",
"Leo",
"Jackson",
"Mason",
"Ezra",
"John",
"Miles",
"Christopher",
"Nathan",
"Isaiah",
"Kai",
"Joshua",
"Eli",
"Wesley",
"Aaron",
"Ian",
"Christian",
"Ryan",
"Leonardo",
"Brooks",
"Axel",
"Walker",
"Jonathan",
"Robert",
"Luka",
"Connor",
"Alex",
"Milo",
"Enzo",
"Giovanni",
"Vincent",
"Diego",
"Luis",
"Archer",
)
private const val HUNDRED_YEARS = 100 * 365L
private val nameList =
arrayOf(
"Ethan",
"Leo",
"Jackson",
"Mason",
"Ezra",
"John",
"Miles",
"Christopher",
"Nathan",
"Isaiah",
"Kai",
"Joshua",
"Eli",
"Wesley",
"Aaron",
"Ian",
"Christian",
"Ryan",
"Leonardo",
"Brooks",
"Axel",
"Walker",
"Jonathan",
"Robert",
"Luka",
"Connor",
"Alex",
"Milo",
"Enzo",
"Giovanni",
"Vincent",
"Diego",
"Luis",
"Archer",
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import org.springframework.boot.test.context.SpringBootTest

@SpringBootTest
class ElasticsearchApplicationTests {

@Test
fun contextLoads() {
}

@Test
fun contextLoads() {
}
}
Loading

0 comments on commit 6465349

Please sign in to comment.