Skip to content

Commit

Permalink
feat: deleteSeminar, searchSeminar 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
skfotakf committed Aug 10, 2023
1 parent 54ad2ff commit 53a5674
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.wafflestudio.csereal.core.seminar.api

import com.wafflestudio.csereal.core.seminar.dto.SeminarDto
import com.wafflestudio.csereal.core.seminar.dto.SeminarSearchResponse
import com.wafflestudio.csereal.core.seminar.service.SeminarService
import jakarta.validation.Valid
import org.springframework.http.ResponseEntity
Expand All @@ -11,6 +12,13 @@ import org.springframework.web.bind.annotation.*
class SeminarController (
private val seminarService: SeminarService,
) {
@GetMapping
fun searchSeminar(
@RequestParam(required = false) keyword: String?,
@RequestParam(required = false, defaultValue = "0") pageNum: Long
) : ResponseEntity<SeminarSearchResponse> {
return ResponseEntity.ok(seminarService.searchSeminar(keyword, pageNum))
}
@PostMapping
fun createSeminar(
@Valid @RequestBody request: SeminarDto
Expand All @@ -33,4 +41,11 @@ class SeminarController (
) : ResponseEntity<SeminarDto> {
return ResponseEntity.ok(seminarService.updateSeminar(seminarId, request))
}

@DeleteMapping("/{seminarId}")
fun deleteSeminar(
@PathVariable seminarId: Long
) {
seminarService.deleteSeminar(seminarId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,79 @@ import com.querydsl.core.BooleanBuilder
import com.querydsl.jpa.impl.JPAQueryFactory
import com.wafflestudio.csereal.common.CserealException
import com.wafflestudio.csereal.core.seminar.database.QSeminarEntity.seminarEntity
import com.wafflestudio.csereal.core.seminar.dto.SeminarSearchDto
import com.wafflestudio.csereal.core.seminar.dto.SeminarSearchResponse
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Component

interface SeminarRepository : JpaRepository<SeminarEntity, Long>, CustomSeminarRepository {
}

interface CustomSeminarRepository {
fun searchSeminar(keyword: String?, pageNum: Long): SeminarSearchResponse
fun findPrevNextId(seminarId: Long, keyword: String?): Array<SeminarEntity?>?
}

@Component
class SeminarRepositoryImpl(
private val queryFactory: JPAQueryFactory,
) : CustomSeminarRepository {
override fun searchSeminar(keyword: String?, pageNum: Long): SeminarSearchResponse {
val keywordBooleanBuilder = BooleanBuilder()

if (!keyword.isNullOrEmpty()) {
val keywordList = keyword.split("[^a-zA-Z0-9가-힣]".toRegex())
keywordList.forEach {
if (it.length == 1) {
throw CserealException.Csereal400("각각의 키워드는 한글자 이상이어야 합니다.")
} else {
keywordBooleanBuilder.and(
seminarEntity.title.contains(it)
.or(seminarEntity.name.contains(it))
.or(seminarEntity.affiliation.contains(it))
.or(seminarEntity.location.contains(it))
)
}
}
}

val jpaQuery = queryFactory.select(seminarEntity).from(seminarEntity)
.where(seminarEntity.isDeleted.eq(false))
.where(keywordBooleanBuilder)

val total = jpaQuery.distinct().fetch().size

val seminarEntityList = jpaQuery.orderBy(seminarEntity.createdAt.desc())
.offset(10*pageNum)
.limit(20)
.distinct()
.fetch()

val seminarSearchDtoList : MutableList<SeminarSearchDto> = mutableListOf()

for(i: Int in 0 until seminarEntityList.size) {
var isYearLast = false
if(i == seminarEntityList.size-1) {
isYearLast = true
} else if(seminarEntityList[i].startDate?.substring(0,4) != seminarEntityList[i+1].startDate?.substring(0,4)) {
isYearLast = true
}

seminarSearchDtoList.add(
SeminarSearchDto(
id = seminarEntityList[i].id,
title = seminarEntityList[i].title,
startDate = seminarEntityList[i].startDate,
isYearLast = isYearLast,
name = seminarEntityList[i].name,
affiliation = seminarEntityList[i].affiliation,
location = seminarEntityList[i].location
)
)
}

return SeminarSearchResponse(total, seminarSearchDtoList)
}
override fun findPrevNextId(seminarId: Long, keyword: String?): Array<SeminarEntity?>? {
val keywordBooleanBuilder = BooleanBuilder()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.wafflestudio.csereal.core.seminar.dto

import com.querydsl.core.annotations.QueryProjection

data class SeminarSearchDto @QueryProjection constructor(
val id: Long,
val title: String,
val startDate: String?,
val isYearLast: Boolean,
val name: String,
val affiliation: String?,
val location: String
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.wafflestudio.csereal.core.seminar.dto

data class SeminarSearchResponse(
val total: Int,
val searchList: List<SeminarSearchDto>
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@ import com.wafflestudio.csereal.common.CserealException
import com.wafflestudio.csereal.core.seminar.database.SeminarEntity
import com.wafflestudio.csereal.core.seminar.database.SeminarRepository
import com.wafflestudio.csereal.core.seminar.dto.SeminarDto
import com.wafflestudio.csereal.core.seminar.dto.SeminarSearchResponse
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

interface SeminarService {
fun searchSeminar(keyword: String?, pageNum: Long): SeminarSearchResponse
fun createSeminar(request: SeminarDto): SeminarDto
fun readSeminar(seminarId: Long, keyword: String?): SeminarDto
fun updateSeminar(seminarId: Long, request: SeminarDto): SeminarDto
fun deleteSeminar(seminarId: Long)
}

@Service
class SeminarServiceImpl(
private val seminarRepository: SeminarRepository
) : SeminarService {
@Transactional(readOnly = true)
override fun searchSeminar(keyword: String?, pageNum: Long): SeminarSearchResponse {
return seminarRepository.searchSeminar(keyword, pageNum)
}

@Transactional
override fun createSeminar(request: SeminarDto): SeminarDto {
Expand Down Expand Up @@ -50,4 +57,11 @@ class SeminarServiceImpl(

return SeminarDto.of(seminar, null)
}
@Transactional
override fun deleteSeminar(seminarId: Long) {
val seminar: SeminarEntity = seminarRepository.findByIdOrNull(seminarId)
?: throw CserealException.Csereal404("존재하지 않는 세미나입니다.(seminarId=$seminarId")

seminar.isDeleted = true
}
}

0 comments on commit 53a5674

Please sign in to comment.