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

Refactor/kotlin #105

Merged
merged 2 commits into from
Sep 18, 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
Expand Up @@ -19,7 +19,8 @@ class CommentController(
@Operation(summary = "๋Œ“๊ธ€ ์ „์ฒด ์กฐํšŒ")
@GetMapping("/posts/{postId}/comments")
fun getCommentList(@PathVariable postId: Long, @AuthenticationPrincipal userPrincipal: UserPrincipal?): ResponseEntity<CommentsResponse> {
return ResponseEntity.status(HttpStatus.OK).body(commentService.getCommentList(postId, userPrincipal))
val userId = userPrincipal?.id
return ResponseEntity.status(HttpStatus.OK).body(commentService.getCommentList(postId, userPrincipal, userId))
}

@Operation(summary = "๋Œ“๊ธ€ ์ž‘์„ฑ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ data class CommentResponse(
val content: String,
val likeCount: Int,
val isMyComment: Boolean,
val likeStatus: Boolean,
val createdAt: LocalDateTime
)
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.eatsfinder.domain.comment.dto

import com.eatsfinder.domain.comment.model.Comment
import com.eatsfinder.domain.like.model.CommentLikes
import com.eatsfinder.global.security.jwt.UserPrincipal

data class CommentsResponse(
val totalCommentCount: Int = 0,
val comments: List<CommentResponse>
) {
companion object {
fun from(comments: List<Comment>, userPrincipal: UserPrincipal?, commentCount: Int): CommentsResponse {
fun from(comments: List<Comment>, userPrincipal: UserPrincipal?, commentCount: Int, commentLikes: List<CommentLikes>?): CommentsResponse {
val res = comments.map { comment ->
CommentResponse(
id = comment.id!!,
Expand All @@ -17,6 +18,7 @@ data class CommentsResponse(
content = comment.content,
likeCount = comment.likeCount,
isMyComment = (userPrincipal != null && comment.userId.id == userPrincipal.id),
likeStatus = (commentLikes?.any { it.commentId.id == comment.id && it.userId.id == userPrincipal?.id } == true),
createdAt = comment.createdAt
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.eatsfinder.global.security.jwt.UserPrincipal

interface CommentService {

fun getCommentList(postId: Long, userId: UserPrincipal?): CommentsResponse
fun getCommentList(postId: Long, userPrincipal: UserPrincipal?, userId: Long?): CommentsResponse

fun createComment(postId: Long, req: CommentRequest, userId: Long): String

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.eatsfinder.domain.comment.dto.CommentsResponse
import com.eatsfinder.domain.comment.dto.CommentsResponse.Companion.from
import com.eatsfinder.domain.comment.model.Comment
import com.eatsfinder.domain.comment.repository.CommentRepository
import com.eatsfinder.domain.like.repository.CommentLikeRepository
import com.eatsfinder.domain.post.repository.PostRepository
import com.eatsfinder.domain.user.model.MyActiveType
import com.eatsfinder.domain.user.model.UserLog
Expand All @@ -21,20 +22,31 @@ class CommentServiceImpl(
private val userRepository: UserRepository,
private val postRepository: PostRepository,
private val commentRepository: CommentRepository,
private val commentLikeRepository: CommentLikeRepository,
private val userLogRepository: UserLogRepository
) : CommentService {

@Transactional(readOnly = true)
override fun getCommentList(postId: Long, userId: UserPrincipal?): CommentsResponse {
override fun getCommentList(postId: Long, userPrincipal: UserPrincipal?, userId: Long?): CommentsResponse {
val loginUser = userPrincipal?.id

val user = loginUser?.let {
userRepository.findByIdAndDeletedAt(it, null) ?: throw ModelNotFoundException(
"user",
"์ด ์œ ์ € ์•„์ด๋””(${userId})๋Š” ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."
)
}

val post = postRepository.findByIdAndDeletedAt(postId, null) ?: throw ModelNotFoundException(
"post",
"์ด ๊ฒŒ์‹œ๋ฌผ ์•„์ด๋””: (${postId})๋Š” ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."
)

val commentCount = commentRepository.countByPostIdAndDeletedAt(post, null) ?: 0
val comments = commentRepository.findByPostIdAndDeletedAt(post, null)
val commentLikes = user?.let { commentLikeRepository.findCommentLikesByUserId(it) } ?: emptyList()

return from(comments, userId, commentCount)
return from(comments, userPrincipal, commentCount, commentLikes)
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import org.springframework.data.jpa.repository.JpaRepository
interface CommentLikeRepository : JpaRepository<CommentLikes, Long> {
fun findByUserIdAndCommentId(userId: User, commentId: Comment): CommentLikes?

// fun findByUserId(userId: User): List<CommentLikes>
fun findCommentLikesByUserId(userId: User): List<CommentLikes>?
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.eatsfinder.domain.post.model.Post
import com.eatsfinder.domain.user.model.User
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import java.time.LocalDateTime

interface PostLikeRepository : JpaRepository<PostLikes, Long> {
fun findByUserIdAndPostId(userId: User, postId: Post): PostLikes?
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion src/main/kotlin/com/eatsfinder/domain/user/model/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import jakarta.persistence.*
import org.hibernate.annotations.ColumnDefault
import org.hibernate.annotations.SQLDelete
import java.time.LocalDate
import java.time.LocalDateTime

@Entity
@SQLDelete(sql = "UPDATE users SET deleted_at = CURRENT_TIMESTAMP WHERE id = ?")
Expand Down
Loading