Skip to content

Commit

Permalink
Merge pull request #239 from urinaner/229
Browse files Browse the repository at this point in the history
feat : 조회수 로직 추가
  • Loading branch information
urinaner authored Jan 3, 2025
2 parents 630e45f + 1d95330 commit 0d8df52
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -60,7 +61,12 @@ public ResponseDto<List<BoardResDto>> getBoardsByCategory(@PathVariable("categor

@Operation(summary = "게시판 상세 정보 반환 API", description = "게시판 상세 정보 반환")
@GetMapping("/{boardId}")
public ResponseEntity<BoardResDto> getBoard(@PathVariable(name = "boardId") Long boardId) {
public ResponseEntity<BoardResDto> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Board, Long> {
Page<Board> 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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,4 +86,40 @@ private void fileUpload(BoardReqDto boardReqDto, List<MultipartFile> 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);
}
}

0 comments on commit 0d8df52

Please sign in to comment.