Skip to content

Commit

Permalink
[#112] 리팩토링
Browse files Browse the repository at this point in the history
- 팔로우 Hard Delete로 변경, unique 조건 추가
- 팔로우 알림 중복 불가 처리 로직 수정
  • Loading branch information
JAEHEE25 committed Dec 17, 2024
1 parent 2753aa2 commit 40ab04f
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package io.driver.codrive.modules.follow.domain;

import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLRestriction;

import io.driver.codrive.global.entity.BaseEntity;
import io.driver.codrive.modules.user.domain.User;
import jakarta.persistence.*;
Expand All @@ -16,8 +13,6 @@
@Builder
@AllArgsConstructor
@NoArgsConstructor
@SQLRestriction("canceled = false")
@SQLDelete(sql = "UPDATE follow SET canceled = true WHERE follow_id = ?")
public class Follow extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -31,14 +26,10 @@ public class Follow extends BaseEntity {
@JoinColumn(name = "follower_id", nullable = false)
private User follower;

@Column(nullable = false)
private Boolean canceled;

public static Follow toFollow(User following, User follower) {
return Follow.builder()
.following(following)
.follower(follower)
.canceled(false)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import io.driver.codrive.modules.user.domain.User;
Expand All @@ -15,14 +13,4 @@ public interface FollowRepository extends JpaRepository<Follow, Long>, FollowRep
Optional<Follow> findByFollowingAndFollower(User following, User follower);

boolean existsByFollowingAndFollower(User following, User follower);

@Query(value = """
SELECT COUNT(*)
FROM follow f
WHERE f.following_id = :targetId
AND f.follower_id = :currentUserId
AND f.canceled = true
""",
nativeQuery = true)
Long getCanceledFollowCount(@Param("targetId") Long targetId, @Param("currentUserId") Long currentUserId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,8 @@ public void follow(String nickname) {
throw new InternalServerErrorApplicationException("팔로우할 수 없습니다.");
}

if (followRepository.getCanceledFollowCount(target.getUserId(), currentUser.getUserId()) == 0) {
notificationService.sendNotification(target, currentUser.getUserId(), NotificationType.FOLLOW,
notificationService.sendFollowNotification(target, currentUser.getUserId(), NotificationType.FOLLOW,
currentUser.getNickname());
}

}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface NotificationRepository extends JpaRepository<Notification, Long
List<Notification> findByUserOrderByCreatedAtDesc(User user);
List<Notification> findAllByDataId(Long dataId);
List<Notification> findAllByDataIdIn(List<Long> dataIds);

boolean existsByUserAndDataIdAndNotificationType(User user, Long dataId, NotificationType notificationType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ public void sendNotification(User user, Long dataId, NotificationType type, Stri
}
}

public void sendFollowNotification(User user, Long dataId, NotificationType type, String... args) {
if (!notificationRepository.existsByUserAndDataIdAndNotificationType(user, dataId, type)) {
sendNotification(user, dataId, type, args);
}
}


protected Notification createNotification(User user, Long dataId, NotificationType type, String... args) {
Notification notification = Notification.create(user, dataId, type, args);
return notificationRepository.save(notification);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,14 +510,12 @@ void getFollowers_success_following() {
Follow follow_other_to_current = Follow.builder() //다른 유저(mockOtherUser) -> 현재 로그인한 유저(mockUser) 팔로우
.following(mockUser)
.follower(mockOtherUser)
.canceled(false)
.build();
mockUser.addFollower(follow_other_to_current);

Follow follow_current_to_other = Follow.builder() //현재 로그인한 유저(mockUser) -> 다른 유저(mockOtherUser) 팔로우
.following(mockOtherUser)
.follower(mockUser)
.canceled(false)
.build();
mockUser.addFollowing(follow_current_to_other);

Expand Down

0 comments on commit 40ab04f

Please sign in to comment.