diff --git a/Titto_Backend/src/main/java/com/example/titto_backend/questionBoard/controller/QuestionController.java b/Titto_Backend/src/main/java/com/example/titto_backend/questionBoard/controller/QuestionController.java index 0d6ab61..d91561e 100644 --- a/Titto_Backend/src/main/java/com/example/titto_backend/questionBoard/controller/QuestionController.java +++ b/Titto_Backend/src/main/java/com/example/titto_backend/questionBoard/controller/QuestionController.java @@ -76,12 +76,8 @@ public ResponseEntity> getAllQuestions(@RequestParam( public ResponseEntity getQuestionById(@PathVariable("postId") Long postId, HttpServletRequest request, HttpServletResponse response) { - try { - QuestionDTO.Response question = questionService.findById(postId, request, response); - return ResponseEntity.ok(question); - } catch (Exception e) { - return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); - } + QuestionDTO.Response question = questionService.findById(postId, request, response); + return ResponseEntity.ok(question); } @GetMapping("/category/{category}") diff --git a/Titto_Backend/src/main/java/com/example/titto_backend/questionBoard/service/QuestionService.java b/Titto_Backend/src/main/java/com/example/titto_backend/questionBoard/service/QuestionService.java index 0cfd20a..bf86a33 100644 --- a/Titto_Backend/src/main/java/com/example/titto_backend/questionBoard/service/QuestionService.java +++ b/Titto_Backend/src/main/java/com/example/titto_backend/questionBoard/service/QuestionService.java @@ -20,7 +20,6 @@ import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZoneOffset; -import java.util.Arrays; import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; @@ -150,30 +149,30 @@ private void validateAuthorIsLoggedInUser(Long id, User user) { // 조회수 증가 private void validViewCount(Question question, HttpServletRequest request, HttpServletResponse response) { Cookie[] cookies = request.getCookies(); - System.out.println("cookies = " + Arrays.toString(cookies)); Cookie cookie = null; boolean isCookie = false; - for (Cookie value : cookies) { - if (value.getName().equals("viewCookie")) { - cookie = value; - if (!cookie.getValue().contains("[" + question.getId() + "]")) { - question.addViewCount(); - cookie.setValue(cookie.getValue() + "[" + question.getId() + "]"); + if (cookies != null) { + for (Cookie value : cookies) { + if (value.getName().equals("viewCookie")) { + cookie = value; + if (!cookie.getValue().contains("[" + question.getId() + "]")) { + question.addViewCount(); + cookie.setValue(cookie.getValue() + "[" + question.getId() + "]"); + } + isCookie = true; + break; } - isCookie = true; - break; } + System.out.println("isCookie = " + isCookie); + if (!isCookie) { + question.addViewCount(); + cookie = new Cookie("viewCookie", "[" + question.getId() + "]"); + } + long todayEndSecond = LocalDate.now().atTime(LocalTime.MAX).toEpochSecond(ZoneOffset.UTC); + long currentSecond = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC); + cookie.setPath("/"); + cookie.setMaxAge((int) (todayEndSecond - currentSecond)); + response.addCookie(cookie); } - System.out.println("isCookie = " + isCookie); - if (!isCookie) { - question.addViewCount(); - cookie = new Cookie("viewCookie", "[" + question.getId() + "]"); - } - long todayEndSecond = LocalDate.now().atTime(LocalTime.MAX).toEpochSecond(ZoneOffset.UTC); - long currentSecond = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC); - cookie.setPath("/"); - cookie.setMaxAge((int) (todayEndSecond - currentSecond)); - response.addCookie(cookie); } - }