Skip to content

Commit

Permalink
feat : post 생성 관련 기능
Browse files Browse the repository at this point in the history
  • Loading branch information
rivkode committed Oct 28, 2023
1 parent 7f7871b commit a1f9ba4
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<Hashtag, Long> {

Long findIdByName(String hashtag);
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
// 예외 처리
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {

}
}
Original file line number Diff line number Diff line change
@@ -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<PostHashtag, Long> {

@Query(value = "SELECT post_id FROM post_hashtag WHERE hashtag_id = :hashtagId", nativeQuery = true)
List<Long> findPostIdsByHashtagId(@Param("hashtagId") Long hashtagId);
}
Original file line number Diff line number Diff line change
@@ -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<Long> getPostIdsByHashtag(String hashtag) {
Long hashtagId = hashtagService.getHashtagId(hashtag);

return postHashtagRepository.findPostIdsByHashtagId(hashtagId);
}
}

0 comments on commit a1f9ba4

Please sign in to comment.