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

Feature: About 검색 추가 #192

Merged
merged 8 commits into from
Feb 28, 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
@@ -1,11 +1,13 @@
package com.wafflestudio.csereal.core.about.api

import com.wafflestudio.csereal.common.aop.AuthenticatedStaff
import com.wafflestudio.csereal.common.properties.LanguageType
import com.wafflestudio.csereal.core.about.api.res.AboutSearchResBody
import com.wafflestudio.csereal.core.about.dto.*
import com.wafflestudio.csereal.core.about.dto.AboutRequest
import com.wafflestudio.csereal.core.about.dto.FutureCareersRequest
import com.wafflestudio.csereal.core.about.service.AboutService
import jakarta.validation.Valid
import jakarta.validation.constraints.Positive
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
Expand All @@ -20,7 +22,7 @@ class AboutController(
// postType: directions / name -> by-public-transit, by-car, from-far-away

// Todo: 학부장 인사말(greetings) signature
@AuthenticatedStaff
// @AuthenticatedStaff
@PostMapping("/{postType}")
fun createAbout(
@PathVariable postType: String,
Expand Down Expand Up @@ -68,6 +70,34 @@ class AboutController(
return ResponseEntity.ok(aboutService.readFutureCareers())
}

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

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

@PostMapping("/migrate")
fun migrateAbout(
@RequestBody requestList: List<AboutRequest>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.wafflestudio.csereal.core.about.api.res

import com.wafflestudio.csereal.common.properties.LanguageType
import com.wafflestudio.csereal.common.utils.cleanTextFromHtml
import com.wafflestudio.csereal.common.utils.substringAroundKeyword
import com.wafflestudio.csereal.core.about.database.AboutEntity
import com.wafflestudio.csereal.core.about.database.AboutPostType

data class AboutSearchElementDto private constructor(
val id: Long,
val language: String,
val aboutPostType: AboutPostType,
val name: String?,
val partialDescription: String,
val boldStartIndex: Int,
val boldEndIndex: Int
) {
companion object {
fun of(about: AboutEntity, keyword: String, amount: Int) = about.run {
val (boldStartIdx, partialDescription) = substringAroundKeyword(
keyword,
cleanTextFromHtml(description),
amount
)

AboutSearchElementDto(
id = id,
language = LanguageType.makeLowercase(language),
aboutPostType = postType,
name = name,
partialDescription = partialDescription.replace('\n', ' '),
boldStartIndex = boldStartIdx ?: 0,
boldEndIndex = boldStartIdx?.plus(keyword.length) ?: 0
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.wafflestudio.csereal.core.about.api.res

data class AboutSearchResBody(
val total: Long,
val results: List<AboutSearchElementDto>
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.wafflestudio.csereal.common.controller.AttachmentContentEntityType
import com.wafflestudio.csereal.common.controller.MainImageContentEntityType
import com.wafflestudio.csereal.common.properties.LanguageType
import com.wafflestudio.csereal.common.utils.StringListConverter
import com.wafflestudio.csereal.common.utils.cleanTextFromHtml
import com.wafflestudio.csereal.core.about.dto.AboutDto
import com.wafflestudio.csereal.core.resource.attachment.database.AttachmentEntity
import com.wafflestudio.csereal.core.resource.mainImage.database.MainImageEntity
Expand All @@ -31,22 +32,66 @@ class AboutEntity(
var attachments: MutableList<AttachmentEntity> = mutableListOf(),

@OneToOne
var mainImage: MainImageEntity? = null
var mainImage: MainImageEntity? = null,

@Column(columnDefinition = "TEXT")
var searchContent: String

) : BaseTimeEntity(), MainImageContentEntityType, AttachmentContentEntityType {
override fun bringMainImage(): MainImageEntity? = mainImage
override fun bringAttachments(): List<AttachmentEntity> = attachments

companion object {
fun of(postType: AboutPostType, languageType: LanguageType, aboutDto: AboutDto): AboutEntity {
fun of(
postType: AboutPostType,
languageType: LanguageType,
aboutDto: AboutDto
): AboutEntity {
return AboutEntity(
postType = postType,
language = languageType,
name = aboutDto.name,
description = aboutDto.description,
year = aboutDto.year,
locations = aboutDto.locations?.toMutableList() ?: mutableListOf()
locations = aboutDto.locations?.toMutableList() ?: mutableListOf(),
searchContent = ""
)
}

fun createContent(name: String?, description: String, locations: List<String>) = StringBuilder().apply {
name?.let { appendLine(it) }
appendLine(cleanTextFromHtml(description))
locations.forEach {
appendLine(it)
}
}.toString()

fun createContent(
name: String?,
description: String,
statNames: List<String>,
companyNames: List<String>
): String {
return StringBuilder().apply {
name?.let { appendLine(it) }
appendLine(cleanTextFromHtml(description))
statNames.forEach {
appendLine(it)
}
companyNames.forEach {
appendLine(it)
}
}.toString()
}
}

fun syncSearchContent() {
assert(postType != AboutPostType.FUTURE_CAREERS)
searchContent = createContent(name, description, locations)
}

fun syncSearchContent(statNames: List<String>, companyNames: List<String>) {
assert(postType == AboutPostType.FUTURE_CAREERS)
searchContent = createContent(name, description, statNames, companyNames)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package com.wafflestudio.csereal.core.about.database

import com.querydsl.jpa.impl.JPAQuery
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.about.database.QAboutEntity.aboutEntity
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository

interface AboutRepository : JpaRepository<AboutEntity, Long> {
interface AboutRepository : JpaRepository<AboutEntity, Long>, AboutCustomRepository {
fun findAllByLanguageAndPostTypeOrderByName(
languageType: LanguageType,
postType: AboutPostType
Expand All @@ -13,3 +19,58 @@ interface AboutRepository : JpaRepository<AboutEntity, Long> {
postType: AboutPostType
): AboutEntity
}

interface AboutCustomRepository {
fun searchAbouts(
keyword: String,
language: LanguageType,
pageSize: Int,
pageNum: Int
): Pair<List<AboutEntity>, Long>
}

@Repository
class AboutCustomRepositoryImpl(
private val queryFactory: JPAQueryFactory,
private val commonRepository: CommonRepository
) : AboutCustomRepository {
override fun searchAbouts(
keyword: String,
language: LanguageType,
pageSize: Int,
pageNum: Int
): Pair<List<AboutEntity>, Long> {
val total = searchCount(keyword, language)
val validPageNum = exchangePageNum(pageSize, pageNum, total)
val validOffset = (
if (validPageNum >= 1) validPageNum - 1 else 0
) * pageSize.toLong()

val queryResult = searchQueryExpr(keyword, language)
.offset(validOffset)
.limit(pageSize.toLong())
.fetch()

return queryResult to total
}

fun searchCount(keyword: String, language: LanguageType): Long {
return searchQueryExpr(keyword, language)
.select(aboutEntity.countDistinct())
.fetchOne()!!
}

fun searchQueryExpr(keyword: String, language: LanguageType): JPAQuery<AboutEntity> {
val matchExpression = commonRepository.searchFullSingleTextTemplate(
keyword,
aboutEntity.searchContent
)

return queryFactory.select(aboutEntity)
.from(aboutEntity)
.where(
matchExpression.gt(0.0),
aboutEntity.language.eq(language)
)
}
}
Loading
Loading