Skip to content

Commit 14f936e

Browse files
authored
Merge pull request #182 from team-gogo/feature/community-community-board-hits-add
[community] 게시글 조회수 적용
2 parents 45a785e + ebc7ef8 commit 14f936e

File tree

12 files changed

+135
-2
lines changed

12 files changed

+135
-2
lines changed

src/main/kotlin/gogo/gogostage/GogoStageApplication.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gogo.gogostage
22

33
import org.springframework.boot.autoconfigure.SpringBootApplication
44
import org.springframework.boot.runApplication
5+
import org.springframework.scheduling.annotation.EnableAsync
56
import org.springframework.scheduling.annotation.EnableScheduling
67

78
@EnableScheduling

src/main/kotlin/gogo/gogostage/domain/community/board/application/BoardProcessor.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class BoardProcessor(
2828
isFiltered = false,
2929
createdAt = LocalDateTime.now(),
3030
imageUrl = writeCommunityBoardDto.imageUrl,
31+
viewCount = 0
3132
)
3233

3334
return boardRepository.save(board)

src/main/kotlin/gogo/gogostage/domain/community/board/persistence/Board.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class Board(
3131
@Column(name = "like_count", nullable = false)
3232
var likeCount: Int,
3333

34+
@Column(name = "view_count", nullable = false)
35+
var viewCount: Int,
36+
3437
@Column(name = "is_filtered", nullable = false)
3538
var isFiltered: Boolean,
3639

@@ -57,4 +60,8 @@ class Board(
5760
isFiltered = true
5861
}
5962

63+
fun plusViewCount() {
64+
viewCount += 1
65+
}
66+
6067
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package gogo.gogostage.domain.community.boardview.persistence
2+
3+
import gogo.gogostage.domain.community.board.persistence.Board
4+
import jakarta.persistence.*
5+
6+
@Entity
7+
@Table(name = "tbl_board_view")
8+
class BoardView(
9+
10+
@Id
11+
@GeneratedValue(strategy = GenerationType.IDENTITY)
12+
@Column(name = "id", nullable = false)
13+
val id: Long = 0,
14+
15+
@ManyToOne(fetch = FetchType.LAZY)
16+
@JoinColumn(name = "board_id", nullable = false)
17+
val board: Board,
18+
19+
@Column(name = "student_id", nullable = false)
20+
val studentId: Long,
21+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package gogo.gogostage.domain.community.boardview.persistence
2+
3+
import org.springframework.data.jpa.repository.JpaRepository
4+
5+
interface BoardViewRepository: JpaRepository<BoardView, Long> {
6+
7+
fun existsByBoardIdAndStudentId(boardId: Long, studentId: Long): Boolean
8+
}

src/main/kotlin/gogo/gogostage/domain/community/root/application/CommunityProcessor.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package gogo.gogostage.domain.community.root.application
22

3+
import gogo.gogostage.domain.community.board.application.BoardReader
34
import gogo.gogostage.domain.community.board.persistence.Board
45
import gogo.gogostage.domain.community.board.persistence.BoardRepository
56
import gogo.gogostage.domain.community.boardlike.persistence.BoardLike
67
import gogo.gogostage.domain.community.boardlike.persistence.BoardLikeRepository
8+
import gogo.gogostage.domain.community.boardview.persistence.BoardView
9+
import gogo.gogostage.domain.community.boardview.persistence.BoardViewRepository
710
import gogo.gogostage.domain.community.comment.persistence.Comment
811
import gogo.gogostage.domain.community.comment.persistence.CommentRepository
912
import gogo.gogostage.domain.community.commentlike.persistence.CommentLike
1013
import gogo.gogostage.domain.community.commentlike.persistence.CommentLikeRepository
1114
import gogo.gogostage.domain.community.root.application.dto.*
15+
import gogo.gogostage.domain.community.root.event.BoardViewEvent
1216
import gogo.gogostage.global.error.StageException
1317
import gogo.gogostage.global.internal.student.stub.StudentByIdStub
1418
import org.springframework.data.repository.findByIdOrNull
@@ -24,6 +28,8 @@ class CommunityProcessor(
2428
private val commentLikeRepository: CommentLikeRepository,
2529
private val commentMapper: CommunityMapper,
2630
private val boardRepository: BoardRepository,
31+
private val boardViewRepository: BoardViewRepository,
32+
private val boardReader: BoardReader
2733
) {
2834

2935
fun likeBoard(studentId: Long, board: Board): LikeResDto {
@@ -130,4 +136,22 @@ class CommunityProcessor(
130136

131137
commentRepository.save(comment)
132138
}
139+
140+
@Transactional
141+
fun saveBoardView(event: BoardViewEvent) {
142+
val board = boardReader.read(event.boardId)
143+
144+
if (!boardViewRepository.existsByBoardIdAndStudentId(board.id, board.studentId)) {
145+
val newBoardView = BoardView(
146+
board = board,
147+
studentId = board.studentId,
148+
)
149+
150+
boardViewRepository.save(newBoardView)
151+
152+
board.plusViewCount()
153+
154+
boardRepository.save(board)
155+
}
156+
}
133157
}

src/main/kotlin/gogo/gogostage/domain/community/root/application/CommunityServiceImpl.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import gogo.gogostage.domain.community.board.application.BoardProcessor
44
import gogo.gogostage.domain.community.board.application.BoardReader
55
import gogo.gogostage.domain.community.root.application.dto.*
66
import gogo.gogostage.domain.community.root.event.BoardCreateEvent
7+
import gogo.gogostage.domain.community.root.event.BoardViewEvent
78
import gogo.gogostage.domain.community.root.event.CommentCreateEvent
89
import gogo.gogostage.domain.community.root.persistence.SortType
910
import gogo.gogostage.domain.game.persistence.GameCategory
@@ -55,7 +56,16 @@ class CommunityServiceImpl(
5556
val board = boardReader.read(boardId)
5657
stageValidator.validStage(student, board.community.stage.id)
5758
stageValidator.validProfanityFilter(student, board)
58-
return communityReader.readBoardInfo(isFiltered, board, student)
59+
val response = communityReader.readBoardInfo(isFiltered, board, student)
60+
61+
applicationEventPublisher.publishEvent(
62+
BoardViewEvent(
63+
boardId = boardId,
64+
studentId = student.studentId,
65+
)
66+
)
67+
68+
return response
5969
}
6070

6171
@Transactional

src/main/kotlin/gogo/gogostage/domain/community/root/application/dto/CommunityDto.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ data class GetCommunityBoardInfoResDto(
5050
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss")
5151
val createdAt: LocalDateTime,
5252
val imageUrl: String?,
53+
val viewCount: Int,
5354
val stage: StageDto,
5455
val commentCount: Int,
5556
val comment: List<CommentDto>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package gogo.gogostage.domain.community.root.event
2+
3+
data class BoardViewEvent(
4+
val boardId: Long,
5+
val studentId: Long
6+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package gogo.gogostage.domain.community.root.event.handler
2+
3+
import gogo.gogostage.domain.community.root.application.CommunityProcessor
4+
import gogo.gogostage.domain.community.root.event.BoardViewEvent
5+
import org.springframework.scheduling.annotation.Async
6+
import org.springframework.stereotype.Component
7+
import org.springframework.transaction.event.TransactionPhase
8+
import org.springframework.transaction.event.TransactionalEventListener
9+
10+
@Component
11+
class BoardViewEventHandler(
12+
private val communityProcessor: CommunityProcessor,
13+
) {
14+
15+
@Async("asyncExecutor")
16+
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
17+
fun eventHandler(event: BoardViewEvent) {
18+
communityProcessor.saveBoardView(event)
19+
}
20+
21+
}

0 commit comments

Comments
 (0)