Skip to content

Commit

Permalink
[FEAT] add get posts by member api
Browse files Browse the repository at this point in the history
  • Loading branch information
jihoi-kang committed Dec 20, 2023
1 parent ea168af commit 4b71e0b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package healthiee.rest.domain.member.api

import healthiee.rest.domain.common.dto.base.Response
import healthiee.rest.domain.member.entity.Member
import healthiee.rest.domain.member.repository.MemberRepository
import healthiee.rest.domain.member.service.MemberService
import healthiee.rest.domain.post.dto.PostDto
import healthiee.rest.domain.post.dto.PostSearchCondition
import healthiee.rest.domain.post.service.PostService
import healthiee.rest.lib.error.ApiException
import healthiee.rest.lib.error.ErrorCode
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.repository.findByIdOrNull
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.util.*

@RestController
@RequestMapping("v1/members/{memberId}/posts")
class MemberPostApiController(
private val memberService: MemberService,
private val postService: PostService,
private val memberRepository: MemberRepository,
) {

@GetMapping("")
@PreAuthorize("hasRole('MEMBER') or hasRole('ADMIN')")
fun getMemberPosts(
pageable: Pageable,
@PathVariable("memberId") memberId: UUID,
@AuthenticationPrincipal member: Member,
): ResponseEntity<Response<Page<PostDto>>> {
val findMember =
memberRepository.findByIdOrNull(memberId) ?: throw ApiException(ErrorCode.NOT_FOUND, "멤버 정보를 찾을 수 없습니다")
memberService.getMember(memberId)
val searchCondition = PostSearchCondition(
members = listOf(findMember),
)

return ResponseEntity.ok(
Response(
code = HttpStatus.OK.value(),
data = postService.getPosts(pageable, searchCondition, member),
)
)

}

}
10 changes: 10 additions & 0 deletions src/main/kotlin/healthiee/rest/domain/post/service/PostService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@ class PostService(
}
}

fun getPosts(
pageable: Pageable,
searchCondition: PostSearchCondition,
member: Member,
): Page<PostDto> {
return postQueryRepository.findAll(pageable, searchCondition).map {
PostDto.create(it, it.postLikes.any { postLike -> postLike.member.id == member.id })
}
}

fun getPost(postId: UUID, member: Member): PostDto {
val findPost = postQueryRepository.findById(postId) ?: throw ApiException(NOT_FOUND, "존재 하지 않는 멤버입니다")
return PostDto.create(findPost, findPost.postLikes.any { postLike -> postLike.member.id == member.id })
Expand Down

0 comments on commit 4b71e0b

Please sign in to comment.