Skip to content

Commit

Permalink
Merge pull request #69 from wafflestudio/feat/feed
Browse files Browse the repository at this point in the history
"내 동네생활 > 댓글 단 글" 에서 피드 목록을 불러오지 않던 문제를 수정하였음.
  • Loading branch information
alpakar02 authored Jan 25, 2025
2 parents d51a0b5 + 3701a7d commit 0258994
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
package com.toyProject7.karrot.comment.controller

import com.toyProject7.karrot.comment.persistence.CommentRepository
import com.toyProject7.karrot.comment.service.CommentService
import com.toyProject7.karrot.feed.controller.FeedPreview
import com.toyProject7.karrot.feed.persistence.FeedEntity
import com.toyProject7.karrot.user.AuthUser
import com.toyProject7.karrot.user.controller.User
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/api")
class CommentController(
private val commentService: CommentService,
private val commentRepository: CommentRepository,
) {
@PostMapping("/comment/post/{feedId}")
fun postComment(
Expand Down Expand Up @@ -69,19 +63,6 @@ class CommentController(
commentService.unlikeComment(commentId, user.id)
return ResponseEntity.ok("Unliked Successfully")
}

@GetMapping("/myfeed/comment")
fun getFeedsByUserComments(
@RequestParam("feedId") feedId: Long,
@AuthUser user: User,
): ResponseEntity<List<FeedPreview>> {
val feeds: List<FeedEntity> = commentService.getFeedsByUserComments(feedId, user.id)
val response =
feeds.map { feed ->
FeedPreview.fromEntity(feed)
}
return ResponseEntity.ok(response)
}
}

data class CommentRequest(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
package com.toyProject7.karrot.comment.persistence

import com.toyProject7.karrot.feed.persistence.FeedEntity
import com.toyProject7.karrot.user.persistence.UserEntity
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param

interface CommentRepository : JpaRepository<CommentEntity, Long> {
@Query(
"""
SELECT DISTINCT c.feed
FROM comments c
WHERE c.user = :user AND c.feed.id < :feedId
ORDER BY c.feed.id DESC
""",
)
fun findFeedsByUserComments(
@Param("user") user: UserEntity,
@Param("feedId") feedId: Long,
): List<FeedEntity>
fun findByUserId(userId: String): List<CommentEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.toyProject7.karrot.comment.persistence.CommentEntity
import com.toyProject7.karrot.comment.persistence.CommentLikesEntity
import com.toyProject7.karrot.comment.persistence.CommentLikesRepository
import com.toyProject7.karrot.comment.persistence.CommentRepository
import com.toyProject7.karrot.feed.persistence.FeedEntity
import com.toyProject7.karrot.feed.service.FeedService
import com.toyProject7.karrot.user.service.UserService
import org.springframework.data.repository.findByIdOrNull
Expand Down Expand Up @@ -106,13 +105,8 @@ class CommentService(
}

@Transactional
fun getFeedsByUserComments(
feedId: Long,
id: String,
): List<FeedEntity> {
val user = userService.getUserEntityById(id)
val feeds = commentRepository.findFeedsByUserComments(user, feedId)
return feeds
fun getCommentsByUser(id: String): List<CommentEntity> {
return commentRepository.findByUserId(id)
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ class FeedController(
}
return ResponseEntity.ok(response)
}

@GetMapping("/myfeed/comment")
fun getFeedsThatUserComments(
@RequestParam("feedId") feedId: Long,
@AuthUser user: User,
): ResponseEntity<List<FeedPreview>> {
val feeds: List<FeedEntity> = feedService.getFeedsThatUserComments(user.id, feedId)
val response =
feeds.map { feed ->
FeedPreview.fromEntity(feed)
}
return ResponseEntity.ok(response)
}
}

data class PostFeedRequest(
Expand Down
16 changes: 16 additions & 0 deletions src/main/kotlin/com/toyProject7/karrot/feed/service/FeedService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,22 @@ class FeedService(
return feeds
}

@Transactional
fun getFeedsThatUserComments(
id: String,
feedId: Long,
): List<FeedEntity> {
val comments: List<CommentEntity> = commentService.getCommentsByUser(id)
val feeds: List<FeedEntity> =
comments
.map { it.feed }
.filter { it.id!! < feedId }
.distinctBy { it.id }
.sortedByDescending { it.id }
if (feeds.size < 10) return feeds
return feeds.subList(0, 10)
}

@Transactional
fun getFeedEntityById(feedId: Long): FeedEntity {
return feedRepository.findByIdOrNull(feedId) ?: throw FeedNotFoundException()
Expand Down

0 comments on commit 0258994

Please sign in to comment.