Skip to content

Commit

Permalink
Merge branch 'develop' into feat/add-migration-academics
Browse files Browse the repository at this point in the history
  • Loading branch information
huGgW authored Feb 29, 2024
2 parents ef6e825 + a4bfc47 commit ca66db5
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.wafflestudio.csereal.core.admissions.service.AdmissionsService
import com.wafflestudio.csereal.core.admissions.type.AdmissionsMainType
import com.wafflestudio.csereal.core.admissions.type.AdmissionsPostType
import jakarta.validation.Valid
import jakarta.validation.constraints.Positive
import org.springframework.web.bind.annotation.*

@RequestMapping("/api/v1/admissions")
Expand Down Expand Up @@ -41,15 +42,32 @@ class AdmissionsController(
return admissionsService.readAdmission(mainType, postType, languageType)
}

@GetMapping("/search")
@GetMapping("/search/top")
fun searchTopAdmissions(
@RequestParam(required = true) keyword: String,
@RequestParam(required = true, defaultValue = "ko") language: String,
@RequestParam(required = true) number: Int
@RequestParam(required = true) @Valid @Positive number: Int,
@RequestParam(required = false, defaultValue = "30") @Valid @Positive amount: Int
) = admissionsService.searchTopAdmission(
keyword,
LanguageType.makeStringToLanguageType(language),
number
number,
amount
)

@GetMapping("/search")
fun searchPageAdmissions(
@RequestParam(required = true) keyword: String,
@RequestParam(required = true, defaultValue = "ko") language: String,
@RequestParam(required = true) @Valid @Positive pageSize: Int,
@RequestParam(required = true) @Valid @Positive pageNum: Int,
@RequestParam(required = false, defaultValue = "30") @Valid @Positive amount: Int
) = admissionsService.searchPageAdmission(
keyword,
LanguageType.makeStringToLanguageType(language),
pageSize,
pageNum,
amount
)

@PostMapping("/migrate")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.wafflestudio.csereal.core.admissions.api.res

data class AdmissionSearchResBody(
val total: Long,
val admissions: List<AdmissionSearchResElem>
)
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
package com.wafflestudio.csereal.core.admissions.api.res

import com.wafflestudio.csereal.common.properties.LanguageType
import com.wafflestudio.csereal.common.utils.substringAroundKeyword
import com.wafflestudio.csereal.core.admissions.database.AdmissionsEntity

data class AdmissionSearchResBody(
val admissions: List<AdmissionSearchResElem>
)

data class AdmissionSearchResElem(
data class AdmissionSearchResElem private constructor(
val id: Long,
val name: String,
val mainType: String,
val postType: String,
val language: String
val language: String,
val partialDescription: String,
val boldStartIndex: Int,
val boldEndIndex: Int
) {
companion object {
fun of(
admissions: AdmissionsEntity
) = AdmissionSearchResElem(
id = admissions.id,
name = admissions.name,
mainType = admissions.mainType.toJsonValue(),
postType = admissions.postType.toJsonValue(),
language = LanguageType.makeLowercase(admissions.language)
)
admissions: AdmissionsEntity,
keyword: String,
amount: Int
) = admissions.let {
val (boldStartIdx, partialDescription) = substringAroundKeyword(
keyword = keyword,
content = it.description,
amount = amount
)

AdmissionSearchResElem(
id = it.id,
name = it.name,
mainType = it.mainType.toJsonValue(),
postType = it.postType.toJsonValue(),
language = LanguageType.makeLowercase(it.language),
partialDescription = partialDescription.replace('\n', ' '),
boldStartIndex = boldStartIdx ?: 0,
boldEndIndex = boldStartIdx?.plus(keyword.length) ?: 0
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.wafflestudio.csereal.core.admissions.database
import com.querydsl.jpa.impl.JPAQueryFactory
import com.wafflestudio.csereal.common.properties.LanguageType
import com.wafflestudio.csereal.common.repository.CommonRepository
import com.wafflestudio.csereal.common.utils.exchangePageNum
import com.wafflestudio.csereal.core.admissions.database.QAdmissionsEntity.admissionsEntity
import com.wafflestudio.csereal.core.admissions.type.AdmissionsMainType
import com.wafflestudio.csereal.core.admissions.type.AdmissionsPostType
Expand All @@ -18,23 +19,43 @@ interface AdmissionsRepository : JpaRepository<AdmissionsEntity, Long>, Admissio
}

interface AdmissionsCustomRepository {
fun searchTopAdmissions(keyword: String, language: LanguageType, number: Int): List<AdmissionsEntity>
fun searchAdmissions(
keyword: String,
language: LanguageType,
pageSize: Int,
pageNum: Int
): Pair<List<AdmissionsEntity>, Long>
}

@Repository
class AdmissionsCustomRepositoryImpl(
private val commonRepository: CommonRepository,
private val queryFactory: JPAQueryFactory
) : AdmissionsCustomRepository {
override fun searchTopAdmissions(
override fun searchAdmissions(
keyword: String,
language: LanguageType,
number: Int
): List<AdmissionsEntity> =
searchQueryOfLanguage(keyword, language)
.limit(number.toLong())
pageSize: Int,
pageNum: Int
): Pair<List<AdmissionsEntity>, Long> {
val total = searchCount(keyword, language)
val validPageNum = exchangePageNum(pageSize, pageNum, total)
val validOffset = (
if (validPageNum >= 1) validPageNum - 1 else 0
) * pageSize.toLong()

val result = searchQueryOfLanguage(keyword, language)
.offset(validOffset)
.limit(pageSize.toLong())
.fetch()

return result to total
}
fun searchCount(keyword: String, language: LanguageType) =
searchQueryOfLanguage(keyword, language)
.select(admissionsEntity.countDistinct())
.fetchOne()!!

fun searchQueryOfLanguage(keyword: String, language: LanguageType) =
queryFactory.selectFrom(
admissionsEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.wafflestudio.csereal.common.CserealException
import com.wafflestudio.csereal.common.properties.LanguageType
import com.wafflestudio.csereal.core.admissions.api.req.AdmissionMigrateElem
import com.wafflestudio.csereal.core.admissions.api.req.AdmissionReqBody
import com.wafflestudio.csereal.core.admissions.api.res.AdmissionSearchResBody
import com.wafflestudio.csereal.core.admissions.api.res.AdmissionSearchResElem
import com.wafflestudio.csereal.core.admissions.database.AdmissionsEntity
import com.wafflestudio.csereal.core.admissions.database.AdmissionsRepository
Expand All @@ -28,8 +29,15 @@ interface AdmissionsService {

fun migrateAdmissions(requestList: List<AdmissionMigrateElem>): List<AdmissionsDto>

@Transactional(readOnly = true)
fun searchTopAdmission(keyword: String, language: LanguageType, number: Int): List<AdmissionSearchResElem>
fun searchPageAdmission(
keyword: String,
language: LanguageType,
pageSize: Int,
pageNum: Int,
amount: Int
): AdmissionSearchResBody

fun searchTopAdmission(keyword: String, language: LanguageType, number: Int, amount: Int): AdmissionSearchResBody
}

@Service
Expand Down Expand Up @@ -60,10 +68,37 @@ class AdmissionsServiceImpl(
?: throw CserealException.Csereal404("해당하는 페이지를 찾을 수 없습니다.")

@Transactional(readOnly = true)
override fun searchTopAdmission(keyword: String, language: LanguageType, number: Int) =
admissionsRepository.searchTopAdmissions(keyword, language, number).map {
AdmissionSearchResElem.of(it)
}
override fun searchTopAdmission(
keyword: String,
language: LanguageType,
number: Int,
amount: Int
): AdmissionSearchResBody {
val (admissions, total) = admissionsRepository.searchAdmissions(keyword, language, number, 1)
return AdmissionSearchResBody(
total = total,
admissions = admissions.map {
AdmissionSearchResElem.of(it, keyword, amount)
}
)
}

@Transactional(readOnly = true)
override fun searchPageAdmission(
keyword: String,
language: LanguageType,
pageSize: Int,
pageNum: Int,
amount: Int
): AdmissionSearchResBody {
val (admissions, total) = admissionsRepository.searchAdmissions(keyword, language, pageSize, pageNum)
return AdmissionSearchResBody(
total = total,
admissions = admissions.map {
AdmissionSearchResElem.of(it, keyword, amount)
}
)
}

@Transactional
override fun migrateAdmissions(requestList: List<AdmissionMigrateElem>) = requestList.map {
Expand Down

0 comments on commit ca66db5

Please sign in to comment.