From 5d7326ed4b9e609d351deecc916489f15ec585b0 Mon Sep 17 00:00:00 2001 From: Jinhui Date: Sat, 24 Aug 2024 22:27:41 +0900 Subject: [PATCH 01/10] =?UTF-8?q?refactor=20:=20=ED=8C=8C=EC=9D=BC?= =?UTF-8?q?=EB=AA=85=EA=B3=BC=20=EB=B3=80=EC=88=98=EB=AA=85=EC=97=90?= =?UTF-8?q?=EC=84=9C=20=EA=B2=8C=EC=8B=9C=EB=AC=BC=EC=9D=84=20=EB=9C=BB?= =?UTF-8?q?=ED=95=98=EB=8A=94=20content=EB=A5=BC=20post=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../media/content/repository/ContentRepository.java | 7 ------- .../java/wanted/media/content/service/ContentService.java | 7 ------- .../controller/PostController.java} | 6 +++--- .../domain/Content.java => post/domain/Post.java} | 8 ++++---- .../java/wanted/media/{content => post}/domain/Type.java | 2 +- .../java/wanted/media/post/repository/PostRepository.java | 7 +++++++ src/main/java/wanted/media/post/service/PostService.java | 7 +++++++ 7 files changed, 22 insertions(+), 22 deletions(-) delete mode 100644 src/main/java/wanted/media/content/repository/ContentRepository.java delete mode 100644 src/main/java/wanted/media/content/service/ContentService.java rename src/main/java/wanted/media/{content/controller/ContentController.java => post/controller/PostController.java} (58%) rename src/main/java/wanted/media/{content/domain/Content.java => post/domain/Post.java} (89%) rename src/main/java/wanted/media/{content => post}/domain/Type.java (63%) create mode 100644 src/main/java/wanted/media/post/repository/PostRepository.java create mode 100644 src/main/java/wanted/media/post/service/PostService.java diff --git a/src/main/java/wanted/media/content/repository/ContentRepository.java b/src/main/java/wanted/media/content/repository/ContentRepository.java deleted file mode 100644 index 510a593..0000000 --- a/src/main/java/wanted/media/content/repository/ContentRepository.java +++ /dev/null @@ -1,7 +0,0 @@ -package wanted.media.content.repository; - -import org.springframework.data.jpa.repository.JpaRepository; -import wanted.media.content.domain.Content; - -public interface ContentRepository extends JpaRepository { -} diff --git a/src/main/java/wanted/media/content/service/ContentService.java b/src/main/java/wanted/media/content/service/ContentService.java deleted file mode 100644 index b505adc..0000000 --- a/src/main/java/wanted/media/content/service/ContentService.java +++ /dev/null @@ -1,7 +0,0 @@ -package wanted.media.content.service; - -import org.springframework.stereotype.Service; - -@Service -public class ContentService { -} diff --git a/src/main/java/wanted/media/content/controller/ContentController.java b/src/main/java/wanted/media/post/controller/PostController.java similarity index 58% rename from src/main/java/wanted/media/content/controller/ContentController.java rename to src/main/java/wanted/media/post/controller/PostController.java index 00583de..97212e6 100644 --- a/src/main/java/wanted/media/content/controller/ContentController.java +++ b/src/main/java/wanted/media/post/controller/PostController.java @@ -1,9 +1,9 @@ -package wanted.media.content.controller; +package wanted.media.post.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController -@RequestMapping("/contents") -public class ContentController { +@RequestMapping("/posts") +public class PostController { } diff --git a/src/main/java/wanted/media/content/domain/Content.java b/src/main/java/wanted/media/post/domain/Post.java similarity index 89% rename from src/main/java/wanted/media/content/domain/Content.java rename to src/main/java/wanted/media/post/domain/Post.java index fed9efd..5f937db 100644 --- a/src/main/java/wanted/media/content/domain/Content.java +++ b/src/main/java/wanted/media/post/domain/Post.java @@ -1,4 +1,4 @@ -package wanted.media.content.domain; +package wanted.media.post.domain; import jakarta.persistence.*; import jakarta.validation.constraints.NotNull; @@ -13,15 +13,15 @@ @Entity @Getter -@Table(name = "contents") +@Table(name = "posts") @NoArgsConstructor(access = AccessLevel.PROTECTED) @AllArgsConstructor @Builder @EntityListeners(AuditingEntityListener.class) -public class Content { +public class Post { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "content_id", nullable = false) + @Column(name = "post_id", nullable = false) private Long id; @Column(name = "like_count") diff --git a/src/main/java/wanted/media/content/domain/Type.java b/src/main/java/wanted/media/post/domain/Type.java similarity index 63% rename from src/main/java/wanted/media/content/domain/Type.java rename to src/main/java/wanted/media/post/domain/Type.java index 539f7fa..55a18e9 100644 --- a/src/main/java/wanted/media/content/domain/Type.java +++ b/src/main/java/wanted/media/post/domain/Type.java @@ -1,4 +1,4 @@ -package wanted.media.content.domain; +package wanted.media.post.domain; public enum Type { FACEBOOK, TWITTER, INSTAGRAM, THREADS; diff --git a/src/main/java/wanted/media/post/repository/PostRepository.java b/src/main/java/wanted/media/post/repository/PostRepository.java new file mode 100644 index 0000000..fb74f17 --- /dev/null +++ b/src/main/java/wanted/media/post/repository/PostRepository.java @@ -0,0 +1,7 @@ +package wanted.media.post.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import wanted.media.post.domain.Post; + +public interface PostRepository extends JpaRepository { +} diff --git a/src/main/java/wanted/media/post/service/PostService.java b/src/main/java/wanted/media/post/service/PostService.java new file mode 100644 index 0000000..cb52ee0 --- /dev/null +++ b/src/main/java/wanted/media/post/service/PostService.java @@ -0,0 +1,7 @@ +package wanted.media.post.service; + +import org.springframework.stereotype.Service; + +@Service +public class PostService { +} From 82c10bc2355407a016213b60ae7b31c889644ecc Mon Sep 17 00:00:00 2001 From: Jinhui Date: Sat, 24 Aug 2024 22:29:21 +0900 Subject: [PATCH 02/10] =?UTF-8?q?refactor=20:=20Long=EC=97=90=EC=84=9C=20S?= =?UTF-8?q?tring=EC=9C=BC=EB=A1=9C=20=EC=9E=90=EB=A3=8C=ED=98=95=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/wanted/media/post/domain/Post.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/wanted/media/post/domain/Post.java b/src/main/java/wanted/media/post/domain/Post.java index 5f937db..858f054 100644 --- a/src/main/java/wanted/media/post/domain/Post.java +++ b/src/main/java/wanted/media/post/domain/Post.java @@ -22,7 +22,7 @@ public class Post { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "post_id", nullable = false) - private Long id; + private String id; @Column(name = "like_count") private Long likeCount; From e0ac706d4513782d3c3cfb9326238175d950a974 Mon Sep 17 00:00:00 2001 From: Jinhui Date: Sun, 25 Aug 2024 23:34:52 +0900 Subject: [PATCH 03/10] =?UTF-8?q?fix=20:=20String=20=ED=83=80=EC=9E=85?= =?UTF-8?q?=EC=9D=80=20IDENTITY=20=EC=A0=81=EC=9A=A9=ED=95=A0=20=EC=88=98?= =?UTF-8?q?=20=EC=97=86=EC=96=B4=EC=84=9C=20=EC=A0=9C=EA=B1=B0=ED=95=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/wanted/media/post/domain/Post.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/wanted/media/post/domain/Post.java b/src/main/java/wanted/media/post/domain/Post.java index 858f054..6a24fdb 100644 --- a/src/main/java/wanted/media/post/domain/Post.java +++ b/src/main/java/wanted/media/post/domain/Post.java @@ -20,7 +20,6 @@ @EntityListeners(AuditingEntityListener.class) public class Post { @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "post_id", nullable = false) private String id; From 22cfabc2b3f08acec88482be958bae187444639b Mon Sep 17 00:00:00 2001 From: Jinhui Date: Sun, 25 Aug 2024 23:54:54 +0900 Subject: [PATCH 04/10] =?UTF-8?q?feat=20:=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20?= =?UTF-8?q?=EC=A2=8B=EC=95=84=EC=9A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/controller/PostLikeController.java | 37 ++++++++++++++ .../post/repository/PostLikeRepository.java | 16 ++++++ .../media/post/service/PostLikeService.java | 49 +++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 src/main/java/wanted/media/post/controller/PostLikeController.java create mode 100644 src/main/java/wanted/media/post/repository/PostLikeRepository.java create mode 100644 src/main/java/wanted/media/post/service/PostLikeService.java diff --git a/src/main/java/wanted/media/post/controller/PostLikeController.java b/src/main/java/wanted/media/post/controller/PostLikeController.java new file mode 100644 index 0000000..af10b57 --- /dev/null +++ b/src/main/java/wanted/media/post/controller/PostLikeController.java @@ -0,0 +1,37 @@ +package wanted.media.post.controller; + +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +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.RestController; +import wanted.media.post.service.PostLikeService; + +@RestController +@RequestMapping("/api/posts") +@RequiredArgsConstructor +public class PostLikeController { + + private final PostLikeService postLikeService; + + @PostMapping("/likes/{postId}") + public ResponseEntity getLikes(@PathVariable(name = "postId") String postId) { + if (postId == null) { + return ResponseEntity.badRequest().body("잘못된 요청입니다."); + } + + // 외부 SNS API 호출 부분 (기능 개발을 위한 요소로, 실제 동작하지 않음) + try { + String endpoint = postLikeService.makeEndpoint(postId); + // 요구사항 시나리오에 따라 필요하지만 실제 동작하지 않기에 주석 처리함 +// RestTemplate restTemplate = new RestTemplate(); +// String response = restTemplate.postForObject(endpoint, null, String.class); + // 외부 데이터와 동기 시키는게 정상적인 동작이나, 본 과제에서는 내부에서 좋아요 수를 증가 시키도록 처리함 + postLikeService.increaseLike(postId); + return ResponseEntity.ok().body("좋아요 수 증가 완료"); + } catch (Exception e) { + return ResponseEntity.internalServerError().body("SNS API 호출 실패: " + e.getMessage()); + } + } +} diff --git a/src/main/java/wanted/media/post/repository/PostLikeRepository.java b/src/main/java/wanted/media/post/repository/PostLikeRepository.java new file mode 100644 index 0000000..cd71f07 --- /dev/null +++ b/src/main/java/wanted/media/post/repository/PostLikeRepository.java @@ -0,0 +1,16 @@ +package wanted.media.post.repository; + +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; +import org.springframework.transaction.annotation.Transactional; +import wanted.media.post.domain.Post; + +public interface PostLikeRepository extends JpaRepository { + + @Transactional + @Modifying(clearAutomatically = true) + @Query("UPDATE Post p SET p.likeCount = p.likeCount + 1 WHERE p.id = :postId") + void incrementLikeCount(@Param("postId") String postId); +} diff --git a/src/main/java/wanted/media/post/service/PostLikeService.java b/src/main/java/wanted/media/post/service/PostLikeService.java new file mode 100644 index 0000000..81c529e --- /dev/null +++ b/src/main/java/wanted/media/post/service/PostLikeService.java @@ -0,0 +1,49 @@ +package wanted.media.post.service; + +import jakarta.persistence.EntityNotFoundException; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Isolation; +import org.springframework.transaction.annotation.Transactional; +import wanted.media.exception.ErrorCode; +import wanted.media.post.domain.Post; +import wanted.media.post.repository.PostLikeRepository; + +@Service +@RequiredArgsConstructor +public class PostLikeService { + + private final PostLikeRepository postLikeRepository; + + @Transactional(readOnly = true) + public String makeEndpoint(String postId) { + // contentId를 통해 게시물의 SNS 유형 조회 + Post post = postLikeRepository.findById(postId).orElseThrow(() -> new EntityNotFoundException(ErrorCode.ENTITY_NOT_FOUND.getMessage())); + String snsType = post.getType().name(); + + String endpoint = ""; + // SNS 유형에 따른 외부 엔드포인트 구성 + switch (snsType.toLowerCase()) { + case "facebook": + endpoint = "https://www.facebook.com/likes/" + postId; + break; + case "twitter": + endpoint = "https://www.twitter.com/likes/" + postId; + break; + case "instagram": + endpoint = "https://www.instagram.com/likes/" + postId; + break; + case "threads": + endpoint = "https://www.threads.net/likes/" + postId; + break; + default: + endpoint = null; + } + return endpoint; + } + + @Transactional(isolation = Isolation.READ_COMMITTED) + public void increaseLike(String postId) { + postLikeRepository.incrementLikeCount(postId); + } +} From 44cf1980cffb28172227ea7853f51698b64eeb90 Mon Sep 17 00:00:00 2001 From: Jinhui Date: Mon, 26 Aug 2024 12:08:01 +0900 Subject: [PATCH 05/10] =?UTF-8?q?fix=20:=20=EB=8F=99=EC=8B=9C=EC=84=B1=20?= =?UTF-8?q?=EB=AC=B8=EC=A0=9C=EB=A1=9C=20=EC=9D=B8=ED=95=9C=20=EC=97=90?= =?UTF-8?q?=EB=9F=AC=20=ED=95=B4=EA=B2=B0=20-=20@Query=20=EB=8C=80?= =?UTF-8?q?=EC=8B=A0=20addLikeCount()=EB=A5=BC=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=ED=95=98=EC=97=AC=20JPA=20=EB=B3=80=EA=B2=BD=20=EA=B0=90?= =?UTF-8?q?=EC=A7=80=EB=A5=BC=20=ED=86=B5=ED=95=B4=20=EC=97=85=EB=8D=B0?= =?UTF-8?q?=EC=9D=B4=ED=8A=B8=20=EB=B0=98=EC=98=81=ED=95=98=EC=97=AC=20?= =?UTF-8?q?=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/controller/PostLikeController.java | 16 ++++++------- .../java/wanted/media/post/domain/Post.java | 3 +++ .../post/repository/PostLikeRepository.java | 9 ------- .../media/post/service/PostLikeService.java | 24 ++++++++++++------- 4 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/main/java/wanted/media/post/controller/PostLikeController.java b/src/main/java/wanted/media/post/controller/PostLikeController.java index af10b57..e82fda5 100644 --- a/src/main/java/wanted/media/post/controller/PostLikeController.java +++ b/src/main/java/wanted/media/post/controller/PostLikeController.java @@ -8,6 +8,9 @@ import org.springframework.web.bind.annotation.RestController; import wanted.media.post.service.PostLikeService; +import java.util.HashMap; +import java.util.Map; + @RestController @RequestMapping("/api/posts") @RequiredArgsConstructor @@ -21,15 +24,12 @@ public ResponseEntity getLikes(@PathVariable(name = "postId") String postId) { return ResponseEntity.badRequest().body("잘못된 요청입니다."); } - // 외부 SNS API 호출 부분 (기능 개발을 위한 요소로, 실제 동작하지 않음) try { - String endpoint = postLikeService.makeEndpoint(postId); - // 요구사항 시나리오에 따라 필요하지만 실제 동작하지 않기에 주석 처리함 -// RestTemplate restTemplate = new RestTemplate(); -// String response = restTemplate.postForObject(endpoint, null, String.class); - // 외부 데이터와 동기 시키는게 정상적인 동작이나, 본 과제에서는 내부에서 좋아요 수를 증가 시키도록 처리함 - postLikeService.increaseLike(postId); - return ResponseEntity.ok().body("좋아요 수 증가 완료"); + String id = postLikeService.makeEndpoint(postId); + Map response = new HashMap<>(); + response.put("postId", id); + response.put("message", "좋아요 수 증가 완료"); + return ResponseEntity.ok().body(response); } catch (Exception e) { return ResponseEntity.internalServerError().body("SNS API 호출 실패: " + e.getMessage()); } diff --git a/src/main/java/wanted/media/post/domain/Post.java b/src/main/java/wanted/media/post/domain/Post.java index 6a24fdb..a59ff76 100644 --- a/src/main/java/wanted/media/post/domain/Post.java +++ b/src/main/java/wanted/media/post/domain/Post.java @@ -53,4 +53,7 @@ public class Post { @NotNull private User user; + public void addLikeCount(Long count) { + this.likeCount = count + 1; + } } diff --git a/src/main/java/wanted/media/post/repository/PostLikeRepository.java b/src/main/java/wanted/media/post/repository/PostLikeRepository.java index cd71f07..aab6cdb 100644 --- a/src/main/java/wanted/media/post/repository/PostLikeRepository.java +++ b/src/main/java/wanted/media/post/repository/PostLikeRepository.java @@ -1,16 +1,7 @@ package wanted.media.post.repository; 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; -import org.springframework.transaction.annotation.Transactional; import wanted.media.post.domain.Post; public interface PostLikeRepository extends JpaRepository { - - @Transactional - @Modifying(clearAutomatically = true) - @Query("UPDATE Post p SET p.likeCount = p.likeCount + 1 WHERE p.id = :postId") - void incrementLikeCount(@Param("postId") String postId); } diff --git a/src/main/java/wanted/media/post/service/PostLikeService.java b/src/main/java/wanted/media/post/service/PostLikeService.java index 81c529e..f40293f 100644 --- a/src/main/java/wanted/media/post/service/PostLikeService.java +++ b/src/main/java/wanted/media/post/service/PostLikeService.java @@ -3,7 +3,6 @@ import jakarta.persistence.EntityNotFoundException; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Transactional; import wanted.media.exception.ErrorCode; import wanted.media.post.domain.Post; @@ -12,16 +11,27 @@ @Service @RequiredArgsConstructor public class PostLikeService { - private final PostLikeRepository postLikeRepository; - @Transactional(readOnly = true) + @Transactional public String makeEndpoint(String postId) { // contentId를 통해 게시물의 SNS 유형 조회 Post post = postLikeRepository.findById(postId).orElseThrow(() -> new EntityNotFoundException(ErrorCode.ENTITY_NOT_FOUND.getMessage())); + String snsType = post.getType().name(); + // 외부 SNS API 호출 부분 (기능 개발을 위한 요소로, 실제 동작하지 않음) + snsApi(snsType, postId); + + // 좋아요 수 증가 + Long like = post.getLikeCount(); + post.addLikeCount(like); + return postId; + } + // 외부 SNS API 호출 부분 (기능 개발을 위한 요소로, 실제 동작하지 않음) + public void snsApi(String snsType, String postId) { String endpoint = ""; + // SNS 유형에 따른 외부 엔드포인트 구성 switch (snsType.toLowerCase()) { case "facebook": @@ -39,11 +49,9 @@ public String makeEndpoint(String postId) { default: endpoint = null; } - return endpoint; - } - @Transactional(isolation = Isolation.READ_COMMITTED) - public void increaseLike(String postId) { - postLikeRepository.incrementLikeCount(postId); + // 요구사항 시나리오에 따라 필요하지만 실제 동작하지 않기에 주석 처리함 +// RestTemplate restTemplate = new RestTemplate(); +// String response = restTemplate.postForObject(endpoint, null, String.class); } } From 1f12dd4659e29e5f86f47db6b889a2d8c809f181 Mon Sep 17 00:00:00 2001 From: Jinhui Date: Mon, 26 Aug 2024 12:29:00 +0900 Subject: [PATCH 06/10] =?UTF-8?q?refactor=20:=20=EA=B2=8C=EC=8B=9C?= =?UTF-8?q?=EB=AC=BC=20=EC=A2=8B=EC=95=84=EC=9A=94=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=ED=8C=8C=EC=9D=BC=20=EB=B3=80=EA=B2=BD=20?= =?UTF-8?q?PostLike~=20=E2=86=92=20Post~?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../media/post/controller/PostController.java | 30 +++++++++- .../post/controller/PostLikeController.java | 37 ------------ .../post/repository/PostLikeRepository.java | 7 --- .../media/post/service/PostLikeService.java | 57 ------------------- .../media/post/service/PostService.java | 50 ++++++++++++++++ 5 files changed, 79 insertions(+), 102 deletions(-) delete mode 100644 src/main/java/wanted/media/post/controller/PostLikeController.java delete mode 100644 src/main/java/wanted/media/post/repository/PostLikeRepository.java delete mode 100644 src/main/java/wanted/media/post/service/PostLikeService.java diff --git a/src/main/java/wanted/media/post/controller/PostController.java b/src/main/java/wanted/media/post/controller/PostController.java index 97212e6..e12ac29 100644 --- a/src/main/java/wanted/media/post/controller/PostController.java +++ b/src/main/java/wanted/media/post/controller/PostController.java @@ -1,9 +1,37 @@ package wanted.media.post.controller; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +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.RestController; +import wanted.media.post.service.PostService; + +import java.util.HashMap; +import java.util.Map; @RestController -@RequestMapping("/posts") +@RequestMapping("/api/posts") +@RequiredArgsConstructor public class PostController { + + private final PostService postService; + + @PostMapping("/likes/{postId}") + public ResponseEntity getLikes(@PathVariable(name = "postId") String postId) { + if (postId == null) { + return ResponseEntity.badRequest().body("잘못된 요청입니다."); + } + + try { + String id = postService.increaseLike(postId); + Map response = new HashMap<>(); + response.put("postId", id); + response.put("message", "좋아요 수 증가 완료"); + return ResponseEntity.ok().body(response); + } catch (Exception e) { + return ResponseEntity.internalServerError().body("좋아요 수 증가 실패 : " + e.getMessage()); + } + } } diff --git a/src/main/java/wanted/media/post/controller/PostLikeController.java b/src/main/java/wanted/media/post/controller/PostLikeController.java deleted file mode 100644 index e82fda5..0000000 --- a/src/main/java/wanted/media/post/controller/PostLikeController.java +++ /dev/null @@ -1,37 +0,0 @@ -package wanted.media.post.controller; - -import lombok.RequiredArgsConstructor; -import org.springframework.http.ResponseEntity; -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.RestController; -import wanted.media.post.service.PostLikeService; - -import java.util.HashMap; -import java.util.Map; - -@RestController -@RequestMapping("/api/posts") -@RequiredArgsConstructor -public class PostLikeController { - - private final PostLikeService postLikeService; - - @PostMapping("/likes/{postId}") - public ResponseEntity getLikes(@PathVariable(name = "postId") String postId) { - if (postId == null) { - return ResponseEntity.badRequest().body("잘못된 요청입니다."); - } - - try { - String id = postLikeService.makeEndpoint(postId); - Map response = new HashMap<>(); - response.put("postId", id); - response.put("message", "좋아요 수 증가 완료"); - return ResponseEntity.ok().body(response); - } catch (Exception e) { - return ResponseEntity.internalServerError().body("SNS API 호출 실패: " + e.getMessage()); - } - } -} diff --git a/src/main/java/wanted/media/post/repository/PostLikeRepository.java b/src/main/java/wanted/media/post/repository/PostLikeRepository.java deleted file mode 100644 index aab6cdb..0000000 --- a/src/main/java/wanted/media/post/repository/PostLikeRepository.java +++ /dev/null @@ -1,7 +0,0 @@ -package wanted.media.post.repository; - -import org.springframework.data.jpa.repository.JpaRepository; -import wanted.media.post.domain.Post; - -public interface PostLikeRepository extends JpaRepository { -} diff --git a/src/main/java/wanted/media/post/service/PostLikeService.java b/src/main/java/wanted/media/post/service/PostLikeService.java deleted file mode 100644 index f40293f..0000000 --- a/src/main/java/wanted/media/post/service/PostLikeService.java +++ /dev/null @@ -1,57 +0,0 @@ -package wanted.media.post.service; - -import jakarta.persistence.EntityNotFoundException; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; -import wanted.media.exception.ErrorCode; -import wanted.media.post.domain.Post; -import wanted.media.post.repository.PostLikeRepository; - -@Service -@RequiredArgsConstructor -public class PostLikeService { - private final PostLikeRepository postLikeRepository; - - @Transactional - public String makeEndpoint(String postId) { - // contentId를 통해 게시물의 SNS 유형 조회 - Post post = postLikeRepository.findById(postId).orElseThrow(() -> new EntityNotFoundException(ErrorCode.ENTITY_NOT_FOUND.getMessage())); - - String snsType = post.getType().name(); - // 외부 SNS API 호출 부분 (기능 개발을 위한 요소로, 실제 동작하지 않음) - snsApi(snsType, postId); - - // 좋아요 수 증가 - Long like = post.getLikeCount(); - post.addLikeCount(like); - return postId; - } - - // 외부 SNS API 호출 부분 (기능 개발을 위한 요소로, 실제 동작하지 않음) - public void snsApi(String snsType, String postId) { - String endpoint = ""; - - // SNS 유형에 따른 외부 엔드포인트 구성 - switch (snsType.toLowerCase()) { - case "facebook": - endpoint = "https://www.facebook.com/likes/" + postId; - break; - case "twitter": - endpoint = "https://www.twitter.com/likes/" + postId; - break; - case "instagram": - endpoint = "https://www.instagram.com/likes/" + postId; - break; - case "threads": - endpoint = "https://www.threads.net/likes/" + postId; - break; - default: - endpoint = null; - } - - // 요구사항 시나리오에 따라 필요하지만 실제 동작하지 않기에 주석 처리함 -// RestTemplate restTemplate = new RestTemplate(); -// String response = restTemplate.postForObject(endpoint, null, String.class); - } -} diff --git a/src/main/java/wanted/media/post/service/PostService.java b/src/main/java/wanted/media/post/service/PostService.java index cb52ee0..31aeb32 100644 --- a/src/main/java/wanted/media/post/service/PostService.java +++ b/src/main/java/wanted/media/post/service/PostService.java @@ -1,7 +1,57 @@ package wanted.media.post.service; +import jakarta.persistence.EntityNotFoundException; +import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import wanted.media.exception.ErrorCode; +import wanted.media.post.domain.Post; +import wanted.media.post.repository.PostRepository; @Service +@RequiredArgsConstructor public class PostService { + private final PostRepository postRepository; + + @Transactional + public String increaseLike(String postId) { + // contentId를 통해 게시물의 SNS 유형 조회 + Post post = postRepository.findById(postId).orElseThrow(() -> new EntityNotFoundException(ErrorCode.ENTITY_NOT_FOUND.getMessage())); + + String snsType = post.getType().name(); + // 외부 SNS API 호출 부분 (기능 개발을 위한 요소로, 실제 동작하지 않음) + snsApi(snsType, postId); + + // 좋아요 수 증가 + Long like = post.getLikeCount(); + post.addLikeCount(like); + return postId; + } + + // 외부 SNS API 호출 부분 (기능 개발을 위한 요소로, 실제 동작하지 않음) + public void snsApi(String snsType, String postId) { + String endpoint = ""; + + // SNS 유형에 따른 외부 엔드포인트 구성 + switch (snsType.toLowerCase()) { + case "facebook": + endpoint = "https://www.facebook.com/likes/" + postId; + break; + case "twitter": + endpoint = "https://www.twitter.com/likes/" + postId; + break; + case "instagram": + endpoint = "https://www.instagram.com/likes/" + postId; + break; + case "threads": + endpoint = "https://www.threads.net/likes/" + postId; + break; + default: + endpoint = null; + } + + // 요구사항 시나리오에 따라 필요하지만 실제 동작하지 않기에 주석 처리함 +// RestTemplate restTemplate = new RestTemplate(); +// String response = restTemplate.postForObject(endpoint, null, String.class); + } } From cee297593403c54f743bf24cfbea07eb30550431 Mon Sep 17 00:00:00 2001 From: Jinhui Date: Mon, 26 Aug 2024 12:44:03 +0900 Subject: [PATCH 07/10] =?UTF-8?q?feat=20:=20PostIdResponse=EB=A5=BC=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=ED=95=98=EC=97=AC=20=EC=9D=91=EB=8B=B5=20?= =?UTF-8?q?=ED=98=95=EC=8B=9D=20=EA=B5=AC=EC=A1=B0=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wanted/media/post/controller/PostController.java | 10 ++-------- .../java/wanted/media/post/dto/PostIdResponse.java | 11 +++++++++++ 2 files changed, 13 insertions(+), 8 deletions(-) create mode 100644 src/main/java/wanted/media/post/dto/PostIdResponse.java diff --git a/src/main/java/wanted/media/post/controller/PostController.java b/src/main/java/wanted/media/post/controller/PostController.java index e12ac29..0cc2ade 100644 --- a/src/main/java/wanted/media/post/controller/PostController.java +++ b/src/main/java/wanted/media/post/controller/PostController.java @@ -6,16 +6,13 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import wanted.media.post.dto.PostIdResponse; import wanted.media.post.service.PostService; -import java.util.HashMap; -import java.util.Map; - @RestController @RequestMapping("/api/posts") @RequiredArgsConstructor public class PostController { - private final PostService postService; @PostMapping("/likes/{postId}") @@ -26,10 +23,7 @@ public ResponseEntity getLikes(@PathVariable(name = "postId") String postId) { try { String id = postService.increaseLike(postId); - Map response = new HashMap<>(); - response.put("postId", id); - response.put("message", "좋아요 수 증가 완료"); - return ResponseEntity.ok().body(response); + return ResponseEntity.ok().body(new PostIdResponse(id, "좋아요 수 증가 완료")); } catch (Exception e) { return ResponseEntity.internalServerError().body("좋아요 수 증가 실패 : " + e.getMessage()); } diff --git a/src/main/java/wanted/media/post/dto/PostIdResponse.java b/src/main/java/wanted/media/post/dto/PostIdResponse.java new file mode 100644 index 0000000..8c00f46 --- /dev/null +++ b/src/main/java/wanted/media/post/dto/PostIdResponse.java @@ -0,0 +1,11 @@ +package wanted.media.post.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class PostIdResponse { + private final String postId; + private final String messgae; +} From fb58e7389c9ad67fd53b426bf9a60acd99b34f1c Mon Sep 17 00:00:00 2001 From: Jinhui Date: Mon, 26 Aug 2024 14:39:53 +0900 Subject: [PATCH 08/10] =?UTF-8?q?refactor=20:=20addLikeCount()=EC=9D=98=20?= =?UTF-8?q?=ED=8C=8C=EB=9D=BC=EB=AF=B8=ED=84=B0=20=EC=A0=9C=EA=B1=B0?= =?UTF-8?q?=EB=A1=9C=20=EA=B0=84=EA=B2=B0=ED=95=98=EA=B2=8C=20=EA=B0=9C?= =?UTF-8?q?=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/wanted/media/post/domain/Post.java | 4 ++-- src/main/java/wanted/media/post/service/PostService.java | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/wanted/media/post/domain/Post.java b/src/main/java/wanted/media/post/domain/Post.java index a59ff76..d07fc7c 100644 --- a/src/main/java/wanted/media/post/domain/Post.java +++ b/src/main/java/wanted/media/post/domain/Post.java @@ -53,7 +53,7 @@ public class Post { @NotNull private User user; - public void addLikeCount(Long count) { - this.likeCount = count + 1; + public void addLikeCount() { + this.likeCount++; } } diff --git a/src/main/java/wanted/media/post/service/PostService.java b/src/main/java/wanted/media/post/service/PostService.java index 31aeb32..eef9606 100644 --- a/src/main/java/wanted/media/post/service/PostService.java +++ b/src/main/java/wanted/media/post/service/PostService.java @@ -23,8 +23,7 @@ public String increaseLike(String postId) { snsApi(snsType, postId); // 좋아요 수 증가 - Long like = post.getLikeCount(); - post.addLikeCount(like); + post.addLikeCount(); return postId; } From b93636e7512a75b04bb59ff2882222729272b593 Mon Sep 17 00:00:00 2001 From: Jinhui Date: Mon, 26 Aug 2024 15:30:37 +0900 Subject: [PATCH 09/10] no message --- .../media/exception/CustomException.java | 15 +++++++++++++++ .../wanted/media/exception/ErrorCode.java | 11 ++++++----- .../handler/GlobalExceptionHandler.java | 19 +++++++++++++------ .../media/post/service/PostService.java | 8 ++++++-- 4 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 src/main/java/wanted/media/exception/CustomException.java diff --git a/src/main/java/wanted/media/exception/CustomException.java b/src/main/java/wanted/media/exception/CustomException.java new file mode 100644 index 0000000..bcd7af3 --- /dev/null +++ b/src/main/java/wanted/media/exception/CustomException.java @@ -0,0 +1,15 @@ +package wanted.media.exception; + +import lombok.Getter; + +@Getter +public class CustomException extends RuntimeException { + private final ErrorCode errorCode; + private String customMessage; + + public CustomException(ErrorCode errorCode) { + super(errorCode.getMessage()); + this.errorCode = errorCode; + this.customMessage = errorCode.getMessage(); + } +} diff --git a/src/main/java/wanted/media/exception/ErrorCode.java b/src/main/java/wanted/media/exception/ErrorCode.java index dade125..adaec97 100644 --- a/src/main/java/wanted/media/exception/ErrorCode.java +++ b/src/main/java/wanted/media/exception/ErrorCode.java @@ -1,15 +1,16 @@ package wanted.media.exception; -import org.springframework.http.HttpStatus; - import lombok.Getter; import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; @Getter @RequiredArgsConstructor public enum ErrorCode { - ENTITY_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 엔티티입니다."); + ENTITY_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 엔티티입니다."), + // 클라이언트의 입력 값에 대한 일반적인 오류 (@PathVariable, @RequestParam가 잘못되었을 때) + INVALID_INPUT_VALUE(HttpStatus.BAD_REQUEST, "클라이언트의 입력 값을 확인해주세요."); - private final HttpStatus status; - private final String message; + private final HttpStatus status; + private final String message; } diff --git a/src/main/java/wanted/media/exception/handler/GlobalExceptionHandler.java b/src/main/java/wanted/media/exception/handler/GlobalExceptionHandler.java index c96a472..db9f567 100644 --- a/src/main/java/wanted/media/exception/handler/GlobalExceptionHandler.java +++ b/src/main/java/wanted/media/exception/handler/GlobalExceptionHandler.java @@ -4,15 +4,22 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; - +import wanted.media.exception.CustomException; import wanted.media.exception.ErrorResponse; @RestControllerAdvice public class GlobalExceptionHandler { - @ExceptionHandler(BadRequestException.class) - public ResponseEntity handleBadRequestException(BadRequestException e) { - return ResponseEntity.badRequest() - .body(new ErrorResponse(400, e.getMessage())); - } + @ExceptionHandler(BadRequestException.class) + public ResponseEntity handleBadRequestException(BadRequestException e) { + return ResponseEntity.badRequest() + .body(new ErrorResponse(400, e.getMessage())); + } + + @ExceptionHandler(CustomException.class) + protected ResponseEntity handleCustomException(final CustomException e) { + return ResponseEntity + .status(e.getErrorCode().getStatus().value()) + .body(new ErrorResponse(e.getErrorCode().getStatus().value(), e.getCustomMessage())); + } } diff --git a/src/main/java/wanted/media/post/service/PostService.java b/src/main/java/wanted/media/post/service/PostService.java index eef9606..99f796b 100644 --- a/src/main/java/wanted/media/post/service/PostService.java +++ b/src/main/java/wanted/media/post/service/PostService.java @@ -1,9 +1,9 @@ package wanted.media.post.service; -import jakarta.persistence.EntityNotFoundException; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import wanted.media.exception.CustomException; import wanted.media.exception.ErrorCode; import wanted.media.post.domain.Post; import wanted.media.post.repository.PostRepository; @@ -15,8 +15,12 @@ public class PostService { @Transactional public String increaseLike(String postId) { + if (postId == null) { + new CustomException(ErrorCode.INVALID_INPUT_VALUE); + } + // contentId를 통해 게시물의 SNS 유형 조회 - Post post = postRepository.findById(postId).orElseThrow(() -> new EntityNotFoundException(ErrorCode.ENTITY_NOT_FOUND.getMessage())); + Post post = postRepository.findById(postId).orElseThrow(() -> new CustomException(ErrorCode.ENTITY_NOT_FOUND)); String snsType = post.getType().name(); // 외부 SNS API 호출 부분 (기능 개발을 위한 요소로, 실제 동작하지 않음) From cbdc2bce3b7d071fa1d98ca4b500961c3500cfb9 Mon Sep 17 00:00:00 2001 From: Jinhui Date: Mon, 26 Aug 2024 16:05:01 +0900 Subject: [PATCH 10/10] =?UTF-8?q?refactor=20:=20PostIdResponse=EB=A5=BC=20?= =?UTF-8?q?=EC=97=90=EB=9F=AC=20=EB=B0=98=ED=99=98=EC=97=90=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9=ED=95=98=EC=97=AC=20=EC=9D=91=EB=8B=B5=20=ED=98=95?= =?UTF-8?q?=EC=8B=9D=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../media/post/controller/PostController.java | 16 +++++++++------- .../wanted/media/post/dto/PostIdResponse.java | 4 ++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/java/wanted/media/post/controller/PostController.java b/src/main/java/wanted/media/post/controller/PostController.java index 0cc2ade..3074064 100644 --- a/src/main/java/wanted/media/post/controller/PostController.java +++ b/src/main/java/wanted/media/post/controller/PostController.java @@ -9,6 +9,9 @@ import wanted.media.post.dto.PostIdResponse; import wanted.media.post.service.PostService; +import java.util.HashMap; +import java.util.Map; + @RestController @RequestMapping("/api/posts") @RequiredArgsConstructor @@ -16,16 +19,15 @@ public class PostController { private final PostService postService; @PostMapping("/likes/{postId}") - public ResponseEntity getLikes(@PathVariable(name = "postId") String postId) { - if (postId == null) { - return ResponseEntity.badRequest().body("잘못된 요청입니다."); - } - + public ResponseEntity getLikes(@PathVariable(name = "postId") String postId) { try { String id = postService.increaseLike(postId); - return ResponseEntity.ok().body(new PostIdResponse(id, "좋아요 수 증가 완료")); + return ResponseEntity.ok().body(new PostIdResponse<>(id, "좋아요 수 증가 완료")); } catch (Exception e) { - return ResponseEntity.internalServerError().body("좋아요 수 증가 실패 : " + e.getMessage()); + Map errorMessage = new HashMap<>(); + errorMessage.put("좋아요 수 증가 실패", e.getMessage()); + return ResponseEntity.internalServerError().body(new PostIdResponse<>(postId, errorMessage)); } } + } diff --git a/src/main/java/wanted/media/post/dto/PostIdResponse.java b/src/main/java/wanted/media/post/dto/PostIdResponse.java index 8c00f46..b2a57b6 100644 --- a/src/main/java/wanted/media/post/dto/PostIdResponse.java +++ b/src/main/java/wanted/media/post/dto/PostIdResponse.java @@ -5,7 +5,7 @@ @Getter @AllArgsConstructor -public class PostIdResponse { +public class PostIdResponse { private final String postId; - private final String messgae; + private final T messgae; }