Skip to content

Commit

Permalink
Feat: ViewCount 마이그레이션
Browse files Browse the repository at this point in the history
피드에 대한 조회수 기능을 추가했습니다
  • Loading branch information
Preta3418 committed Nov 5, 2024
1 parent c1c9dcc commit 25f4114
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ViewCountDTO> {
val viewCountDTO = viewCountService.addView(feedId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

data class ViewCountDTO(
val feedId: Long?,
val viewCount: Long
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Original file line number Diff line number Diff line change
Expand Up @@ -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<ViewCount?, Long?> {
fun findByFeed(feed: Feed?): Optional<ViewCount>
}
interface ViewCountRepository : JpaRepository<ViewCount, Long> {
fun findByFeed(feed: Feed): Optional<ViewCount>
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
}
}

0 comments on commit 25f4114

Please sign in to comment.