From a1f9ba4f49868b6a7a3c5bd1e31f33f82298344e Mon Sep 17 00:00:00 2001 From: jonghun Date: Sat, 28 Oct 2023 22:56:27 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20post=20=EC=83=9D=EC=84=B1=20?= =?UTF-8?q?=EA=B4=80=EB=A0=A8=20=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hashtag/repository/HashtagRepository.java | 11 +++++++ .../hashtag/service/HashtagService.java | 23 +++++++++++++ .../post/service/PostService.java | 21 ++++++++++++ .../postHashtag/entity/PostHashtag.java | 11 +++++++ .../repository/PostHashtagRepository.java | 13 ++++++++ .../service/PostHashtagService.java | 32 +++++++++++++++++++ 6 files changed, 111 insertions(+) create mode 100644 src/main/java/com/snsIntegrationFeedService/hashtag/repository/HashtagRepository.java create mode 100644 src/main/java/com/snsIntegrationFeedService/hashtag/service/HashtagService.java create mode 100644 src/main/java/com/snsIntegrationFeedService/postHashtag/repository/PostHashtagRepository.java create mode 100644 src/main/java/com/snsIntegrationFeedService/postHashtag/service/PostHashtagService.java diff --git a/src/main/java/com/snsIntegrationFeedService/hashtag/repository/HashtagRepository.java b/src/main/java/com/snsIntegrationFeedService/hashtag/repository/HashtagRepository.java new file mode 100644 index 0000000..a2eb2c8 --- /dev/null +++ b/src/main/java/com/snsIntegrationFeedService/hashtag/repository/HashtagRepository.java @@ -0,0 +1,11 @@ +package com.snsIntegrationFeedService.hashtag.repository; + +import com.snsIntegrationFeedService.hashtag.entity.Hashtag; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface HashtagRepository extends JpaRepository { + + Long findIdByName(String hashtag); +} diff --git a/src/main/java/com/snsIntegrationFeedService/hashtag/service/HashtagService.java b/src/main/java/com/snsIntegrationFeedService/hashtag/service/HashtagService.java new file mode 100644 index 0000000..b4ddf19 --- /dev/null +++ b/src/main/java/com/snsIntegrationFeedService/hashtag/service/HashtagService.java @@ -0,0 +1,23 @@ +package com.snsIntegrationFeedService.hashtag.service; + +import com.snsIntegrationFeedService.hashtag.entity.Hashtag; +import com.snsIntegrationFeedService.hashtag.repository.HashtagRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class HashtagService { + + private final HashtagRepository hashtagRepository; + + public Hashtag createHashtag(String tag) { + Hashtag hashtag = Hashtag.builder().name(tag).build(); + + return hashtagRepository.save(hashtag); + } + + public Long getHashtagId(String hashtag) { + return hashtagRepository.findIdByName(hashtag); + } +} diff --git a/src/main/java/com/snsIntegrationFeedService/post/service/PostService.java b/src/main/java/com/snsIntegrationFeedService/post/service/PostService.java index 80d6a32..706353c 100644 --- a/src/main/java/com/snsIntegrationFeedService/post/service/PostService.java +++ b/src/main/java/com/snsIntegrationFeedService/post/service/PostService.java @@ -1,5 +1,10 @@ package com.snsIntegrationFeedService.post.service; +import com.snsIntegrationFeedService.hashtag.entity.Hashtag; +import com.snsIntegrationFeedService.hashtag.service.HashtagService; +import com.snsIntegrationFeedService.post.dto.request.CreatePostRequest; +import com.snsIntegrationFeedService.postHashtag.service.PostHashtagService; +import com.snsIntegrationFeedService.user.entity.User; import java.util.List; import org.springframework.stereotype.Service; @@ -21,6 +26,9 @@ public class PostService { private final PostRepository postRepository; + private final HashtagService hashtagService; + private final PostHashtagService postHashtagService; + @Transactional public PostDetailResponseDto getPostDetail(String postId) { // 예외 처리 @@ -37,4 +45,17 @@ public PostDetailResponseDto getPostDetail(String postId) { post.view(); return PostDetailResponseDto.from(post, hashTags); } + + public Post createPost(User user, CreatePostRequest request) { + // post 생성 + Post savedPost = postRepository.save(request.toEntity(user, request)); + + // hashtag 생성 + Hashtag hashtag = hashtagService.createHashtag(request.getHashtag()); + + // postHashtag 생성 + postHashtagService.createPostHashtag(savedPost, hashtag); + + return savedPost; + } } diff --git a/src/main/java/com/snsIntegrationFeedService/postHashtag/entity/PostHashtag.java b/src/main/java/com/snsIntegrationFeedService/postHashtag/entity/PostHashtag.java index 6fd99fd..0772fca 100644 --- a/src/main/java/com/snsIntegrationFeedService/postHashtag/entity/PostHashtag.java +++ b/src/main/java/com/snsIntegrationFeedService/postHashtag/entity/PostHashtag.java @@ -3,6 +3,7 @@ import com.snsIntegrationFeedService.hashtag.entity.Hashtag; import com.snsIntegrationFeedService.post.entity.Post; import jakarta.persistence.*; +import lombok.Builder; import lombok.Getter; @Entity @@ -19,4 +20,14 @@ public class PostHashtag { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "hashtag_id") private Hashtag hashtag; + + @Builder + public PostHashtag(Post post, Hashtag hashtag) { + this.post = post; + this.hashtag = hashtag; + } + + public PostHashtag() { + + } } diff --git a/src/main/java/com/snsIntegrationFeedService/postHashtag/repository/PostHashtagRepository.java b/src/main/java/com/snsIntegrationFeedService/postHashtag/repository/PostHashtagRepository.java new file mode 100644 index 0000000..cc19079 --- /dev/null +++ b/src/main/java/com/snsIntegrationFeedService/postHashtag/repository/PostHashtagRepository.java @@ -0,0 +1,13 @@ +package com.snsIntegrationFeedService.postHashtag.repository; + +import com.snsIntegrationFeedService.postHashtag.entity.PostHashtag; +import java.util.List; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +public interface PostHashtagRepository extends JpaRepository { + + @Query(value = "SELECT post_id FROM post_hashtag WHERE hashtag_id = :hashtagId", nativeQuery = true) + List findPostIdsByHashtagId(@Param("hashtagId") Long hashtagId); +} diff --git a/src/main/java/com/snsIntegrationFeedService/postHashtag/service/PostHashtagService.java b/src/main/java/com/snsIntegrationFeedService/postHashtag/service/PostHashtagService.java new file mode 100644 index 0000000..0aeee40 --- /dev/null +++ b/src/main/java/com/snsIntegrationFeedService/postHashtag/service/PostHashtagService.java @@ -0,0 +1,32 @@ +package com.snsIntegrationFeedService.postHashtag.service; + +import com.snsIntegrationFeedService.hashtag.entity.Hashtag; +import com.snsIntegrationFeedService.hashtag.service.HashtagService; +import com.snsIntegrationFeedService.post.entity.Post; +import com.snsIntegrationFeedService.postHashtag.entity.PostHashtag; +import com.snsIntegrationFeedService.postHashtag.repository.PostHashtagRepository; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class PostHashtagService { + + private final PostHashtagRepository postHashtagRepository; + private final HashtagService hashtagService; + + public PostHashtag createPostHashtag(Post post, Hashtag hashtag) { + PostHashtag postHashtag = PostHashtag.builder() + .post(post) + .hashtag(hashtag) + .build(); + return postHashtagRepository.save(postHashtag); + } + + public List getPostIdsByHashtag(String hashtag) { + Long hashtagId = hashtagService.getHashtagId(hashtag); + + return postHashtagRepository.findPostIdsByHashtagId(hashtagId); + } +}