From 82fc96ec5f2f414363337079ea092cc34ddda34b Mon Sep 17 00:00:00 2001 From: Dongseok Kang Date: Sun, 31 Dec 2023 19:02:33 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20=ED=91=B8=EB=B0=94=EC=98=A4=20=EB=B0=9B?= =?UTF-8?q?=EC=9D=80=20=EC=82=AC=EB=9E=91=20(#61)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit redis에 있는 값 가져오고 없으면 rds에서 가져온 후 redis에 저장 #60 --- .../project/domain/api/post/PostController.java | 5 +++++ .../dto/response/PostGetFubaoLoveResponse.java | 16 ++++++++++++++++ .../project/domain/service/PostService.java | 2 ++ .../project/domain/service/PostServiceImp.java | 11 +++++++++++ 4 files changed, 34 insertions(+) create mode 100644 src/main/java/com/fubao/project/domain/api/post/dto/response/PostGetFubaoLoveResponse.java diff --git a/src/main/java/com/fubao/project/domain/api/post/PostController.java b/src/main/java/com/fubao/project/domain/api/post/PostController.java index 8bcda6d..33e370d 100644 --- a/src/main/java/com/fubao/project/domain/api/post/PostController.java +++ b/src/main/java/com/fubao/project/domain/api/post/PostController.java @@ -113,4 +113,9 @@ public ResponseEntity> postFubaoLove() { postService.addFubaoLove(); return ResponseEntity.ok(DataResponse.of(CustomResponseCode.FUBAO_LOVE_ADD)); } + @Operation(summary = "푸바오받은 사랑") + @GetMapping(value = "/fubao/love") + public ResponseEntity> getFubaoLove() { + return ResponseEntity.ok(DataResponse.of(postService.getFubaoLove())); + } } \ No newline at end of file diff --git a/src/main/java/com/fubao/project/domain/api/post/dto/response/PostGetFubaoLoveResponse.java b/src/main/java/com/fubao/project/domain/api/post/dto/response/PostGetFubaoLoveResponse.java new file mode 100644 index 0000000..918d299 --- /dev/null +++ b/src/main/java/com/fubao/project/domain/api/post/dto/response/PostGetFubaoLoveResponse.java @@ -0,0 +1,16 @@ +package com.fubao.project.domain.api.post.dto.response; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.time.LocalDateTime; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class PostGetFubaoLoveResponse { + @Schema(description = "푸바오가 받은 사랑", example = "123456788") + private Long love; +} diff --git a/src/main/java/com/fubao/project/domain/service/PostService.java b/src/main/java/com/fubao/project/domain/service/PostService.java index 8f5e64d..9e4249a 100644 --- a/src/main/java/com/fubao/project/domain/service/PostService.java +++ b/src/main/java/com/fubao/project/domain/service/PostService.java @@ -25,4 +25,6 @@ public interface PostService { void deletePost(Long postId, UUID memberId); void addFubaoLove(); + + PostGetFubaoLoveResponse getFubaoLove(); } diff --git a/src/main/java/com/fubao/project/domain/service/PostServiceImp.java b/src/main/java/com/fubao/project/domain/service/PostServiceImp.java index 514267a..ba111b2 100644 --- a/src/main/java/com/fubao/project/domain/service/PostServiceImp.java +++ b/src/main/java/com/fubao/project/domain/service/PostServiceImp.java @@ -151,6 +151,17 @@ public void addFubaoLove() { addFubaoLove(1L); } + @Override + @Transactional + public PostGetFubaoLoveResponse getFubaoLove() { + FubaoLove fubaoLove = fubaoLoveRepository.getReferenceById(1L); + if (!redisUtil.hasKey(FUBAO_LOVE)) { + redisUtil.setStringData(FUBAO_LOVE, String.valueOf(fubaoLove.getLove()), Duration.ofHours(6)); + return new PostGetFubaoLoveResponse(fubaoLove.getLove()); + } + return new PostGetFubaoLoveResponse(Long.valueOf(redisUtil.getData(FUBAO_LOVE))); + } + private Post findPostById(Long postId) { return postRepository.findById(postId).orElseThrow(() -> new CustomException(ResponseCode.POST_NOT_FOUND)); }