Skip to content

Commit

Permalink
♻️ 커뮤니티 게시글 조회 리팩토링 (#285)
Browse files Browse the repository at this point in the history
* ♻️ 작성자 필터 추가 & 커서 기반 페이징 적용 (#284)

* ✅ 커뮤니티 게시글 테스트 코드 수정 (#284)

* ♻️ 커뮤니티 게시글 command in 패키지 추가 (#284)
  • Loading branch information
discphy authored Dec 11, 2024
1 parent 4bd8625 commit f6ddb9d
Show file tree
Hide file tree
Showing 25 changed files with 399 additions and 204 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package backend.team.ahachul_backend.api.community.adapter.web.`in`

import backend.team.ahachul_backend.api.community.adapter.web.`in`.dto.*
import backend.team.ahachul_backend.api.community.adapter.web.`in`.dto.post.*
import backend.team.ahachul_backend.api.community.application.command.`in`.DeleteCommunityPostCommand
import backend.team.ahachul_backend.api.community.application.command.`in`.GetCommunityPostCommand
import backend.team.ahachul_backend.api.community.application.port.`in`.CommunityPostUseCase
import backend.team.ahachul_backend.common.annotation.Authentication
import backend.team.ahachul_backend.common.dto.PageInfoDto
import backend.team.ahachul_backend.common.response.CommonResponse
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.*

@RestController
Expand All @@ -16,10 +17,11 @@ class CommunityPostController(
@Authentication(required = false)
@GetMapping("/v1/community-posts")
fun searchCommunityPosts(
pageable: Pageable,
@RequestParam(required = false) pageToken: String?,
@RequestParam pageSize: Int,
request: SearchCommunityPostDto.Request
): CommonResponse<SearchCommunityPostDto.Response> {
return CommonResponse.success(communityPostUseCase.searchCommunityPosts(request.toCommand(pageable)))
): CommonResponse<PageInfoDto<SearchCommunityPostDto.Response>> {
return CommonResponse.success(communityPostUseCase.searchCommunityPosts(request.toCommand(pageToken, pageSize)))
}

@Authentication(required = false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package backend.team.ahachul_backend.api.community.adapter.web.`in`

import backend.team.ahachul_backend.api.community.adapter.web.`in`.dto.*
import backend.team.ahachul_backend.api.community.adapter.web.`in`.dto.post.*
import backend.team.ahachul_backend.api.community.application.port.`in`.CommunityPostLikeUseCase
import backend.team.ahachul_backend.common.annotation.Authentication
import backend.team.ahachul_backend.common.response.CommonResponse
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package backend.team.ahachul_backend.api.community.adapter.web.`in`.dto

import backend.team.ahachul_backend.api.community.adapter.web.`in`.dto.post.CreateCommunityPostCommand
import backend.team.ahachul_backend.api.community.application.command.`in`.CreateCommunityPostCommand
import backend.team.ahachul_backend.api.community.domain.entity.CommunityPostEntity
import backend.team.ahachul_backend.api.community.domain.model.CommunityCategoryType
import backend.team.ahachul_backend.common.domain.model.RegionType
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package backend.team.ahachul_backend.api.community.adapter.web.`in`.dto

import backend.team.ahachul_backend.api.community.adapter.web.`in`.dto.post.DeleteCommunityPostCommand
import backend.team.ahachul_backend.api.community.application.command.`in`.DeleteCommunityPostCommand

class DeleteCommunityPostDto {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package backend.team.ahachul_backend.api.community.adapter.web.`in`.dto

import backend.team.ahachul_backend.api.community.adapter.web.`in`.dto.post.SearchCommunityPostCommand
import backend.team.ahachul_backend.api.community.domain.SearchCommunityPost
import backend.team.ahachul_backend.api.community.application.command.`in`.SearchCommunityPostCommand
import backend.team.ahachul_backend.api.community.domain.model.CommunityCategoryType
import backend.team.ahachul_backend.common.domain.model.RegionType
import backend.team.ahachul_backend.common.domain.model.YNType
import backend.team.ahachul_backend.common.dto.ImageDto
import org.springframework.data.domain.Pageable
import java.time.LocalDateTime
import org.springframework.data.domain.Sort

class SearchCommunityPostDto {

Expand All @@ -17,36 +15,30 @@ class SearchCommunityPostDto {
val content: String?,
val hashTag: String?,
val hotPostYn: YNType?,
val writer: String?,
val sort: String
) {
fun toCommand(pageable: Pageable): SearchCommunityPostCommand {
fun toCommand(pageToken: String?, pageSize: Int): SearchCommunityPostCommand {
return SearchCommunityPostCommand(
categoryType = categoryType,
subwayLineId = subwayLineId,
content = content,
hashTag = hashTag,
hotPostYn = hotPostYn,
pageable = pageable
writer = writer,
sort = toSort(),
pageToken = pageToken,
pageSize = pageSize
)
}
}

data class Response(
val hasNext: Boolean,
val nextPageNum: Int?,
val posts: List<CommunityPost>,
) {
companion object {
fun of(hasNext: Boolean, posts: List<CommunityPost>, currentPageNum: Int): Response {
return Response(
hasNext = hasNext,
nextPageNum = if (hasNext) currentPageNum + 1 else null,
posts = posts
)
}
private fun toSort(): Sort {
val parts = sort.split(",")
return Sort.by(Sort.Direction.fromString(parts[1]), parts[0])
}
}

data class CommunityPost(
data class Response(
val id: Long,
val title: String,
val content: String,
Expand All @@ -58,31 +50,9 @@ class SearchCommunityPostDto {
val hotPostYn: YNType,
val regionType: RegionType,
val subwayLineId: Long,
val createdAt: LocalDateTime,
val createdAt: String,
val createdBy: String,
val writer: String,
val image: ImageDto?,
) {
companion object {
fun of(searchCommunityPost: SearchCommunityPost, image: ImageDto?, views: Int, hashTags: List<String>): CommunityPost {
return CommunityPost(
id = searchCommunityPost.id,
title = searchCommunityPost.title,
content = searchCommunityPost.content,
categoryType = searchCommunityPost.categoryType,
hashTags = hashTags,
commentCnt = searchCommunityPost.commentCnt,
viewCnt = views,
likeCnt = searchCommunityPost.likeCnt,
hotPostYn = searchCommunityPost.hotPostYn,
regionType = searchCommunityPost.regionType,
subwayLineId = searchCommunityPost.subwayLineId,
createdAt = searchCommunityPost.createdAt,
createdBy = searchCommunityPost.createdBy,
writer = searchCommunityPost.writer,
image = image,
)
}
}
}
)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package backend.team.ahachul_backend.api.community.adapter.web.`in`.dto

import backend.team.ahachul_backend.api.community.adapter.web.`in`.dto.post.UpdateCommunityPostCommand
import backend.team.ahachul_backend.api.community.application.command.`in`.UpdateCommunityPostCommand
import backend.team.ahachul_backend.api.community.domain.entity.CommunityPostEntity
import backend.team.ahachul_backend.api.community.domain.model.CommunityCategoryType
import backend.team.ahachul_backend.common.dto.ImageDto
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package backend.team.ahachul_backend.api.community.application.port.`in`

import backend.team.ahachul_backend.api.community.adapter.web.`in`.dto.*
import backend.team.ahachul_backend.api.community.adapter.web.`in`.dto.post.*
import backend.team.ahachul_backend.api.community.application.command.`in`.*
import backend.team.ahachul_backend.common.dto.PageInfoDto

interface CommunityPostUseCase {

fun searchCommunityPosts(command: SearchCommunityPostCommand): SearchCommunityPostDto.Response
fun searchCommunityPosts(command: SearchCommunityPostCommand): PageInfoDto<SearchCommunityPostDto.Response>

fun getCommunityPost(command: GetCommunityPostCommand): GetCommunityPostDto.Response

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package backend.team.ahachul_backend.api.community.application.service

import backend.team.ahachul_backend.api.community.adapter.web.`in`.dto.*
import backend.team.ahachul_backend.api.community.adapter.web.`in`.dto.post.*
import backend.team.ahachul_backend.api.community.application.command.`in`.*
import backend.team.ahachul_backend.api.community.application.command.out.GetSliceCommunityPostCommand
import backend.team.ahachul_backend.api.community.application.port.`in`.CommunityPostUseCase
import backend.team.ahachul_backend.api.community.application.port.out.CommunityPostFileReader
import backend.team.ahachul_backend.api.community.application.port.out.CommunityPostHashTagReader
Expand All @@ -11,12 +12,14 @@ import backend.team.ahachul_backend.api.community.domain.entity.CommunityPostEnt
import backend.team.ahachul_backend.api.community.domain.entity.CommunityPostFileEntity
import backend.team.ahachul_backend.api.member.application.port.out.MemberReader
import backend.team.ahachul_backend.common.dto.ImageDto
import backend.team.ahachul_backend.common.dto.PageInfoDto
import backend.team.ahachul_backend.common.logging.NamedLogger
import backend.team.ahachul_backend.common.persistence.SubwayLineReader
import backend.team.ahachul_backend.common.support.ViewsSupport
import backend.team.ahachul_backend.common.utils.RequestUtils
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.format.DateTimeFormatter

@Service
@Transactional(readOnly = true)
Expand All @@ -37,28 +40,47 @@ class CommunityPostService(

private val logger = NamedLogger("HASHTAG_LOGGER")

override fun searchCommunityPosts(command: SearchCommunityPostCommand): SearchCommunityPostDto.Response {
override fun searchCommunityPosts(command: SearchCommunityPostCommand): PageInfoDto<SearchCommunityPostDto.Response> {
val userId: String? = RequestUtils.getAttribute("memberId")
val searchCommunityPosts = communityPostReader.searchCommunityPosts(command)
val subwayLine = command.subwayLineId?.let { subwayLineReader.getById(it) }

val searchCommunityPosts = communityPostReader.searchCommunityPosts(
GetSliceCommunityPostCommand.from(
command = command,
subwayLine = subwayLine
)
)

val posts = searchCommunityPosts
.map {
val file = communityPostFileReader.findByPostId(it.id)?.file
SearchCommunityPostDto.CommunityPost.of(
searchCommunityPost = it,
image = file?.let { it1 -> ImageDto.of(it1.id, file.filePath) },
views = viewsSupport.get(it.id),
hashTags = communityPostHashTagReader.findAllByPostId(it.id).map { it.hashTag.name }
SearchCommunityPostDto.Response(
id = it.id,
title = it.title,
content = it.content,
categoryType = it.categoryType,
hashTags = communityPostHashTagReader.findAllByPostId(it.id).map { it.hashTag.name },
commentCnt = it.commentCnt,
viewCnt = viewsSupport.get(it.id),
likeCnt = it.likeCnt,
hotPostYn = it.hotPostYn,
regionType = it.regionType,
subwayLineId = it.subwayLineId,
createdAt = it.createdAt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")),
createdBy = it.createdBy,
writer = it.writer,
image = file?.let { it1 -> ImageDto.of(it1.id, file.filePath) }
)
}.toList()

if (isHashTagSearchCond(command.hashTag, command.content)) {
logger.info("userId = $userId hashtag = ${command.hashTag}")
}

return SearchCommunityPostDto.Response.of(
hasNext = searchCommunityPosts.hasNext(),
posts = posts,
command.pageable.pageNumber,
return PageInfoDto.of(
data=posts,
pageSize=command.pageSize,
arrayOf(SearchCommunityPostDto.Response::createdAt, SearchCommunityPostDto.Response::id)
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package backend.team.ahachul_backend.admin.application.service

import backend.team.ahachul_backend.api.community.adapter.web.`in`.dto.post.CreateCommunityPostCommand
import backend.team.ahachul_backend.api.community.application.command.`in`.CreateCommunityPostCommand
import backend.team.ahachul_backend.api.community.adapter.web.out.CommunityPostRepository
import backend.team.ahachul_backend.api.community.domain.entity.CommunityPostEntity
import backend.team.ahachul_backend.api.community.domain.model.CommunityCategoryType
Expand Down
Loading

0 comments on commit f6ddb9d

Please sign in to comment.