diff --git a/backend/src/main/java/org/example/backend/board/controller/BoardController.java b/backend/src/main/java/org/example/backend/board/controller/BoardController.java index f8689ef7..b6d45161 100644 --- a/backend/src/main/java/org/example/backend/board/controller/BoardController.java +++ b/backend/src/main/java/org/example/backend/board/controller/BoardController.java @@ -2,6 +2,8 @@ import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import jakarta.validation.Valid; import java.util.List; import lombok.RequiredArgsConstructor; @@ -21,7 +23,6 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @@ -60,7 +61,12 @@ public ResponseDto> getBoardsByCategory(@PathVariable("categor @Operation(summary = "게시판 상세 정보 반환 API", description = "게시판 상세 정보 반환") @GetMapping("/{boardId}") - public ResponseEntity getBoard(@PathVariable(name = "boardId") Long boardId) { + public ResponseEntity getBoard( + @PathVariable(name = "boardId") Long boardId, + HttpServletRequest request, + HttpServletResponse response + ) { + boardService.incrementViewCount(boardId, request, response); BoardResDto boardResDto = boardService.getBoard(boardId); return new ResponseEntity<>(boardResDto, HttpStatus.OK); } diff --git a/backend/src/main/java/org/example/backend/board/repository/BoardRepository.java b/backend/src/main/java/org/example/backend/board/repository/BoardRepository.java index ec3c76d8..cb690a12 100644 --- a/backend/src/main/java/org/example/backend/board/repository/BoardRepository.java +++ b/backend/src/main/java/org/example/backend/board/repository/BoardRepository.java @@ -5,7 +5,14 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; public interface BoardRepository extends JpaRepository { Page findAllByCategory(Category category, Pageable pageable); + @Modifying + @Query("UPDATE Board b SET b.viewCount = b.viewCount + 1 WHERE b.id = :boardId") + void incrementViewCount(@Param("boardId") Long boardId); + } diff --git a/backend/src/main/java/org/example/backend/board/service/BoardService.java b/backend/src/main/java/org/example/backend/board/service/BoardService.java index 663b1600..f971d096 100644 --- a/backend/src/main/java/org/example/backend/board/service/BoardService.java +++ b/backend/src/main/java/org/example/backend/board/service/BoardService.java @@ -2,6 +2,9 @@ import static org.example.backend.board.exception.BoardExceptionType.NOT_FOUND_BOARD; +import jakarta.servlet.http.Cookie; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.util.ArrayList; import java.util.List; import lombok.RequiredArgsConstructor; @@ -83,4 +86,40 @@ private void fileUpload(BoardReqDto boardReqDto, List multipartFi boardReqDto.setFileList(updateImageUrlList); } } + + @Transactional + public void incrementViewCount(Long boardId, HttpServletRequest request, HttpServletResponse response) { + Cookie oldCookie = null; + Cookie[] cookies = request.getCookies(); + + if (cookies != null) { + for (Cookie cookie : cookies) { + if (cookie.getName().equals("postView")) { + oldCookie = cookie; + break; + } + } + } + + if (oldCookie != null) { + if (!oldCookie.getValue().contains("[" + boardId + "]")) { + readCount(boardId); + oldCookie.setValue(oldCookie.getValue() + "_[" + boardId + "]"); + oldCookie.setPath("/"); + oldCookie.setMaxAge(60 * 60 * 24); + response.addCookie(oldCookie); + } + } else { + readCount(boardId); + Cookie newCookie = new Cookie("postView", "[" + boardId + "]"); + newCookie.setPath("/"); + newCookie.setMaxAge(60 * 60 * 24); + response.addCookie(newCookie); + } + } + + @Transactional + public void readCount(Long boardId) { + boardRepository.incrementViewCount(boardId); + } } \ No newline at end of file