Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WEAV-34] ✨ 직장 검색 API #25

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.threedays.bootstrap.api.user

import com.threedays.domain.user.repository.CompanyQueryRepository
import com.threedays.oas.api.CompaniesApi
import com.threedays.oas.model.Company
import com.threedays.oas.model.SearchCompaniesResponse
import com.threedays.support.common.base.domain.UUIDTypeId
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.RestController
import java.util.*

@RestController
class CompaniesController (
private val companyQueryRepository: CompanyQueryRepository
): CompaniesApi {

override fun searchCompanies(
name: String,
next: UUID?,
limit: Int
): ResponseEntity<SearchCompaniesResponse> {
val (resultCompanies, resultNext) = companyQueryRepository.searchCompanies(
name = name,
next = next?.let { UUIDTypeId.from(it) },
limit = limit
)

val companyResponse = resultCompanies.map {
Company(
id = it.id.value,
name = it.name
)
}

return ResponseEntity.ok(
SearchCompaniesResponse(
companies = companyResponse,
next = resultNext?.value
)
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,12 @@ package com.threedays.domain.user.repository
import com.threedays.domain.user.entity.Company
import com.threedays.support.common.base.domain.QueryRepository

interface CompanyQueryRepository : QueryRepository<Company, Company.Id>
interface CompanyQueryRepository : QueryRepository<Company, Company.Id> {

fun searchCompanies(
name: String,
next: Company.Id?,
limit: Int
): Pair<List<Company>, Company.Id?>

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,52 @@
package com.threedays.persistence.user.adapter

import com.linecorp.kotlinjdsl.dsl.jpql.jpql
import com.linecorp.kotlinjdsl.querymodel.jpql.select.SelectQuery
import com.linecorp.kotlinjdsl.render.RenderContext
import com.linecorp.kotlinjdsl.support.spring.data.jpa.extension.createQuery
import com.threedays.domain.user.entity.Company
import com.threedays.domain.user.repository.CompanyQueryRepository
import com.threedays.persistence.user.entity.CompanyJpaEntity
import com.threedays.persistence.user.repository.CompanyJpaRepository
import jakarta.persistence.EntityManager
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional

@Component
@Transactional(readOnly = true)
class CompanyQueryPersistenceAdapter(
private val companyJpaRepository: CompanyJpaRepository
private val companyJpaRepository: CompanyJpaRepository,
private val entityManager: EntityManager,
private val jdslRenderContext: RenderContext,
) : CompanyQueryRepository {

override fun searchCompanies(
name: String,
next: Company.Id?,
limit: Int
): Pair<List<Company>, Company.Id?> {
waterfogSW marked this conversation as resolved.
Show resolved Hide resolved
val query: SelectQuery<CompanyJpaEntity> = jpql {
select(
entity(CompanyJpaEntity::class)
).from(
entity(CompanyJpaEntity::class)
).whereAnd(
path(CompanyJpaEntity::name).like("%$name%"),
next?.let { path(CompanyJpaEntity::id).gt(it.value) }
)
}

val result: List<Company> = entityManager
.createQuery(query, jdslRenderContext)
.apply { setMaxResults(limit + 1) }
.resultList
.map { it.toDomainEntity() }

val nextId: Company.Id? = result.getOrNull(limit)?.id ?: next

return result.take(limit) to nextId
}

override fun find(id: Company.Id): Company? {
return companyJpaRepository
.findById(id.value)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.threedays.persistence.user.repository

import com.linecorp.kotlinjdsl.support.spring.data.jpa.repository.KotlinJdslJpqlExecutor
import com.threedays.persistence.user.entity.CompanyJpaEntity
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
import java.util.*

@Repository
interface CompanyJpaRepository : JpaRepository<CompanyJpaEntity, UUID>
interface CompanyJpaRepository : JpaRepository<CompanyJpaEntity, UUID>, KotlinJdslJpqlExecutor