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); + } +}