Skip to content

Commit

Permalink
Merge pull request #41 from wafflestudio/feat/feed
Browse files Browse the repository at this point in the history
feat/feed
  • Loading branch information
alpakar02 authored Jan 20, 2025
2 parents 7be0c82 + bb38df9 commit 29c08b0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
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
Expand All @@ -16,6 +20,7 @@ import org.springframework.web.bind.annotation.RestController
@RequestMapping("/api")
class CommentController(
private val commentService: CommentService,
private val commentRepository: CommentRepository,
) {
@PostMapping("/comment/post/{feedId}")
fun postComment(
Expand Down Expand Up @@ -63,6 +68,18 @@ class CommentController(
commentService.unlikeComment(commentId, user.id)
return ResponseEntity.ok("Unliked Successfully")
}

@GetMapping("/myfeed/comment")
fun getFeedsByUserComments(
@AuthUser user: User,
): ResponseEntity<List<FeedPreview>> {
val feeds: List<FeedEntity> = commentService.getFeedsByUserComments(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,5 +1,21 @@
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>
interface CommentRepository : JpaRepository<CommentEntity, Long> {
@Query(
"""
SELECT DISTINCT c.feed
FROM comments c
WHERE c.user = :user
ORDER BY c.feed.id DESC
""",
)
fun findFeedsByUserComments(
@Param("user") user: UserEntity,
): List<FeedEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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 @@ -104,6 +105,13 @@ class CommentService(
commentLikesRepository.delete(toBeRemoved)
}

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

@Transactional
fun getCommentEntityById(commentId: Long): CommentEntity {
return commentRepository.findByIdOrNull(commentId) ?: throw CommentNotFoundException()
Expand Down

0 comments on commit 29c08b0

Please sign in to comment.