From 25f411408be5fc88c1aa4964d778da9badb3cae7 Mon Sep 17 00:00:00 2001 From: Preta3418 Date: Tue, 5 Nov 2024 12:11:07 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20ViewCount=20=EB=A7=88=EC=9D=B4=EA=B7=B8?= =?UTF-8?q?=EB=A0=88=EC=9D=B4=EC=85=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 피드에 대한 조회수 기능을 추가했습니다 --- .../controller/ViewCountController.kt | 2 ++ .../viewCount/dto/ViewCountDTO.kt | 8 ++++--- .../viewCount/entity/ViewCount.kt | 23 ++++++------------- .../repository/ViewCountRepository.kt | 7 +++--- .../viewCount/service/ViewCountServiceImpl.kt | 11 ++++----- 5 files changed, 22 insertions(+), 29 deletions(-) diff --git a/src/main/kotlin/org/tenten/bittakotlin/feedInteraction/viewCount/controller/ViewCountController.kt b/src/main/kotlin/org/tenten/bittakotlin/feedInteraction/viewCount/controller/ViewCountController.kt index de5121f..b3999de 100644 --- a/src/main/kotlin/org/tenten/bittakotlin/feedInteraction/viewCount/controller/ViewCountController.kt +++ b/src/main/kotlin/org/tenten/bittakotlin/feedInteraction/viewCount/controller/ViewCountController.kt @@ -6,9 +6,11 @@ import org.tenten.bittakotlin.feedInteraction.viewCount.dto.ViewCountDTO import org.tenten.bittakotlin.feedInteraction.viewCount.service.ViewCountService + @RestController @RequestMapping("/api/v1/feed/view") class ViewCountController(private val viewCountService: ViewCountService) { + @PostMapping("/{feedId}") fun addView(@PathVariable feedId: Long): ResponseEntity { val viewCountDTO = viewCountService.addView(feedId) diff --git a/src/main/kotlin/org/tenten/bittakotlin/feedInteraction/viewCount/dto/ViewCountDTO.kt b/src/main/kotlin/org/tenten/bittakotlin/feedInteraction/viewCount/dto/ViewCountDTO.kt index c5eed63..9b60087 100644 --- a/src/main/kotlin/org/tenten/bittakotlin/feedInteraction/viewCount/dto/ViewCountDTO.kt +++ b/src/main/kotlin/org/tenten/bittakotlin/feedInteraction/viewCount/dto/ViewCountDTO.kt @@ -3,6 +3,8 @@ package org.tenten.bittakotlin.feedInteraction.viewCount.dto import lombok.Getter import lombok.Setter -@Setter -@Getter -class ViewCountDTO(private val feedId: Long?, private val viewCount: Long) \ No newline at end of file + +data class ViewCountDTO( + val feedId: Long?, + val viewCount: Long +) \ No newline at end of file diff --git a/src/main/kotlin/org/tenten/bittakotlin/feedInteraction/viewCount/entity/ViewCount.kt b/src/main/kotlin/org/tenten/bittakotlin/feedInteraction/viewCount/entity/ViewCount.kt index 0e8043a..7f7a1f8 100644 --- a/src/main/kotlin/org/tenten/bittakotlin/feedInteraction/viewCount/entity/ViewCount.kt +++ b/src/main/kotlin/org/tenten/bittakotlin/feedInteraction/viewCount/entity/ViewCount.kt @@ -9,24 +9,15 @@ import org.tenten.bittakotlin.feed.entity.Feed @Entity -@Data -@Builder -@NoArgsConstructor -@AllArgsConstructor class ViewCount( - @field:JoinColumn( - name = "feed_id", - nullable = false - ) @field:OneToOne(fetch = FetchType.LAZY) var feed: Feed, count: Long -) { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private val id: Long = 0 + val id: Long = 0L, - @Column(name = "count", nullable = false) - private var count = 0L + @OneToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "feed_id", nullable = false) + var feed: Feed, - init { - this.count = count - } -} + @Column(name = "count", nullable = false) + var count: Long = 0L +) \ No newline at end of file diff --git a/src/main/kotlin/org/tenten/bittakotlin/feedInteraction/viewCount/repository/ViewCountRepository.kt b/src/main/kotlin/org/tenten/bittakotlin/feedInteraction/viewCount/repository/ViewCountRepository.kt index f2e2a56..83c7405 100644 --- a/src/main/kotlin/org/tenten/bittakotlin/feedInteraction/viewCount/repository/ViewCountRepository.kt +++ b/src/main/kotlin/org/tenten/bittakotlin/feedInteraction/viewCount/repository/ViewCountRepository.kt @@ -5,7 +5,6 @@ import org.tenten.bittakotlin.feed.entity.Feed import org.tenten.bittakotlin.feedInteraction.viewCount.entity.ViewCount import java.util.* - -interface ViewCountRepository : JpaRepository { - fun findByFeed(feed: Feed?): Optional -} +interface ViewCountRepository : JpaRepository { + fun findByFeed(feed: Feed): Optional +} \ No newline at end of file diff --git a/src/main/kotlin/org/tenten/bittakotlin/feedInteraction/viewCount/service/ViewCountServiceImpl.kt b/src/main/kotlin/org/tenten/bittakotlin/feedInteraction/viewCount/service/ViewCountServiceImpl.kt index b8ef059..0df3825 100644 --- a/src/main/kotlin/org/tenten/bittakotlin/feedInteraction/viewCount/service/ViewCountServiceImpl.kt +++ b/src/main/kotlin/org/tenten/bittakotlin/feedInteraction/viewCount/service/ViewCountServiceImpl.kt @@ -14,19 +14,18 @@ class ViewCountServiceImpl( private val viewCountRepository: ViewCountRepository, private val feedRepository: FeedRepository ) : ViewCountService { + @Transactional override fun addView(feedId: Long): ViewCountDTO { val feed = feedRepository.findById(feedId) .orElseThrow { EntityNotFoundException("Feed not found for id: $feedId") } val viewCount = viewCountRepository.findByFeed(feed).orElseGet { - val newViewCount = ViewCount() - newViewCount.feed = feed - newViewCount.count = 0L + val newViewCount = ViewCount(feed = feed, count = 0L) viewCountRepository.save(newViewCount) } - viewCount.count = viewCount.count + 1 + viewCount.count += 1 viewCountRepository.save(viewCount) return ViewCountDTO(feed.id, viewCount.count) @@ -38,9 +37,9 @@ class ViewCountServiceImpl( .orElseThrow { EntityNotFoundException("Feed not found for id: $feedId") } val count = viewCountRepository.findByFeed(feed) - .map { obj: ViewCount -> obj.count } + .map { it.count } .orElse(0L) return ViewCountDTO(feed.id, count) } -} +} \ No newline at end of file