Skip to content

Commit

Permalink
#119 refactor: 게시글 서비스 리팩터링
Browse files Browse the repository at this point in the history
- @transactional 어노테이션 적용
- 사용하지 않는 메서드 제거
- queryDsl 적용
  • Loading branch information
rivkode committed Sep 9, 2024
1 parent 5becb60 commit 08be5ef
Show file tree
Hide file tree
Showing 34 changed files with 388 additions and 631 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

@Builder
public record GetCommentResponse(
String commentId,
String userId,
String postId,
String commentToken,
String userToken,
String postToken,
String comment,
LocalDateTime updateAt
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.seoultech.synergybe.domain.common.paging.ListResponse;
import com.seoultech.synergybe.domain.notification.service.NotificationService;
import com.seoultech.synergybe.domain.post.Post;
import com.seoultech.synergybe.domain.post.implement.PostReader;
import com.seoultech.synergybe.domain.post.repository.PostRepository;
import com.seoultech.synergybe.domain.user.User;
import com.seoultech.synergybe.domain.user.service.UserServiceImpl;
import lombok.RequiredArgsConstructor;
Expand All @@ -25,15 +25,15 @@
@Transactional
@RequiredArgsConstructor
public class CommentService {
private final PostRepository postRepository;
private final CommentRepository commentRepository;
private final PostReader postReader;
private final UserServiceImpl userService;
private final NotificationService notificationService;
private final IdGenerator idGenerator;
private final TokenGenerator tokenGenerator;

public GetCommentResponse createComment(String userId, CreateCommentRequest request) {
Post post = postReader.read(request.postId());
Post post = postRepository.findByPostToken(request.postId());
User user = userService.getUserByToken(userId);
Long commentId = idGenerator.generateId();
String commentToken = tokenGenerator.generateToken(IdPrefix.COMMENT);
Expand All @@ -47,10 +47,9 @@ public GetCommentResponse createComment(String userId, CreateCommentRequest requ

Comment savedComment = commentRepository.save(comment);
savedComment.addPost(post);
// User postUser = post.getUser();
// notificationService.send(postUser, NotificationType.COMMENT, "댓글이 생성되었습니다", post.getId());
return GetCommentResponse.builder()
.commentId(savedComment.getCommentToken())
.commentToken(savedComment.getCommentToken())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,4 @@ public ResponseEntity<ListResponse<String>> getFollowingIds(@LoginUser String us

return ResponseEntity.status(HttpStatus.OK).body(followService.getFollowingIds(userId));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class FollowReader {
private final FollowRepository followRepository;

public List<String> readFollowingIds(String userId) {
return followRepository.findFollowingIdsByFollowerId(userId);
public List<Long> readFollowingIds(String userToken) {
return followRepository.findFollowingIdsByFollowerToken(userToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@
import java.util.Optional;

@Repository
public interface FollowRepository extends JpaRepository<Follow, Long> {
public interface FollowRepository extends JpaRepository<Follow, Long>, FollowRepositoryCustom {

@Query(value = "SELECT following_id FROM follow WHERE follower_id = :userId AND status = 'FOLLOW'", nativeQuery = true)
List<String> findFollowingIdsByFollowerId(@Param("userId") String userId);

@Query(value = "SELECT follower_id FROM follow WHERE following_id = :userId AND status = 'FOLLOW'", nativeQuery = true)
List<String> findFollowerIdsByFollowingId(@Param("userId") String userId);

@Query(value = "SELECT * FROM follow WHERE follower_id = :userId AND following_id = :followingId FOR UPDATE", nativeQuery = true)
Optional<Follow> findByFollowerIdAndFollowingId(@Param("userId") String userId, @Param("followingId") String followingId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.seoultech.synergybe.domain.follow.repository;

import com.seoultech.synergybe.domain.follow.Follow;

import java.util.List;

public interface FollowRepositoryCustom {
List<Long> findFollowingIdsByFollowerToken(String followerToken);
List<Long> findFollowerIdsByFollowingToken(String followingToken);
Follow findByFollowerTokenAndFollowingToken(String userToken, String followingToken);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.seoultech.synergybe.domain.follow.repository;

import com.querydsl.jpa.impl.JPAQueryFactory;
import com.seoultech.synergybe.domain.follow.Follow;
import com.seoultech.synergybe.domain.follow.FollowStatus;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

import static com.seoultech.synergybe.domain.follow.QFollow.follow;

@RequiredArgsConstructor
@Repository
public class FollowRepositoryCustomImpl implements FollowRepositoryCustom {
private final JPAQueryFactory queryFactory;

@Override
public List<Long> findFollowingIdsByFollowerToken(String followerToken) {
return queryFactory
.select(follow.follower.id)
.where(
follow.follower.userToken.eq(followerToken).and(follow.status.eq(FollowStatus.FOLLOW)))
.fetch();
}

@Override
public List<Long> findFollowerIdsByFollowingToken(String followingToken) {
return queryFactory
.select(follow.follower.id)
.where(
follow.following.userToken.eq(followingToken).and(follow.status.eq(FollowStatus.FOLLOW)))
.fetch();
}

@Override
public Follow findByFollowerTokenAndFollowingToken(String userToken, String followingToken) {
return queryFactory
.selectFrom(follow)
.where(
follow.follower.userToken.eq(userToken).and(follow.following.userToken.eq(followingToken))
)
.fetchOne();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ public class FollowService {
private final IdGenerator idGenerator;
private final TokenGenerator tokenGenerator;

public List<String> findFollowingIdsByUserId(String userId) {
return followRepository.findFollowingIdsByFollowerId(userId);
}

/**
*
* @param user 신청한 유저
Expand Down Expand Up @@ -64,14 +60,14 @@ public GetFollowResponse updateFollow(User user, String followingToken, CreateFo
}

public synchronized Follow update(User user, String followingId, FollowStatus status) {
Optional<Follow> followOptional = followRepository.findByFollowerIdAndFollowingId(user.getUserToken(), followingId);
Follow followOptional = followRepository.findByFollowerTokenAndFollowingToken(user.getUserToken(), followingId);

if (followOptional.isPresent()) {
followOptional.get().updateStatus(status);
if (followOptional != null) {
followOptional.updateStatus(status);
User following = userService.getUserByToken(followingId);
// notificationService.send(following, NotificationType.FOLLOW, "팔로우 신청이 완료되었습니다.", Long.valueOf(followingId));

return followOptional.get();
return followOptional;
} else {
User following = userService.getUserByToken(followingId);
Long followId = idGenerator.generateId();
Expand All @@ -88,27 +84,23 @@ public synchronized Follow update(User user, String followingId, FollowStatus st
}
}

public List<String> getFollowerIdList(String userId) {
return followRepository.findFollowerIdsByFollowingId(userId);
public List<Long> getFollowerIdList(String userToken) {
return followRepository.findFollowerIdsByFollowingToken(userToken);
}

public List<String> getFollowingIdList(String userId) {
return followRepository.findFollowingIdsByFollowerId(userId);
public List<Long> getFollowingIdList(String userToken) {
return followRepository.findFollowingIdsByFollowerToken(userToken);
}

public ListResponse<String> getFollowerIds(String userId) {
List<String> getFollowerIdList = getFollowerIdList(userId);
public ListResponse<String> getFollowerIds(String userToken) {
List<Long> getFollowerIdList = getFollowerIdList(userToken);

ListResponse<String> getUserIdListResponses = new ListResponse(getFollowerIdList);

return getUserIdListResponses;
return new ListResponse(getFollowerIdList);
}

public ListResponse<String> getFollowingIds(String userId) {
List<String> getFollowingIdList = getFollowingIdList(userId);

ListResponse<String> getUserIdListResponses = new ListResponse(getFollowingIdList);
public ListResponse<String> getFollowingIds(String userToken) {
List<Long> getFollowingIdList = getFollowingIdList(userToken);

return getUserIdListResponses;
return new ListResponse(getFollowingIdList);
}
}

This file was deleted.

Loading

0 comments on commit 08be5ef

Please sign in to comment.