-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- kwj1270
- (#78)
- kwj1270-issue54
- (#78)
Showing
6 changed files
with
91 additions
and
12 deletions.
There are no files selected for viewing
39 changes: 38 additions & 1 deletion
39
src/main/java/com/study/realworld/domain/follow/application/FollowService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,55 @@ | ||
package com.study.realworld.domain.follow.application; | ||
|
||
import com.study.realworld.domain.follow.domain.Follow; | ||
import com.study.realworld.domain.follow.domain.FollowQueryDslRepository; | ||
import com.study.realworld.domain.follow.domain.FollowRepository; | ||
import com.study.realworld.domain.follow.dto.FollowableDto; | ||
import com.study.realworld.domain.user.domain.persist.User; | ||
import com.study.realworld.domain.user.domain.persist.UserRepository; | ||
import com.study.realworld.domain.user.error.exception.IdentityNotFoundException; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Transactional(readOnly = true) | ||
@Service | ||
public class FollowService { | ||
|
||
private final UserRepository userRepository; | ||
private final FollowRepository followRepository; | ||
private final FollowQueryDslRepository followQueryDslRepository; | ||
|
||
public FollowService(final FollowRepository followRepository, final FollowQueryDslRepository followQueryDslRepository) { | ||
public FollowService(final UserRepository userRepository, final FollowRepository followRepository, final FollowQueryDslRepository followQueryDslRepository) { | ||
this.userRepository = userRepository; | ||
this.followRepository = followRepository; | ||
this.followQueryDslRepository = followQueryDslRepository; | ||
} | ||
|
||
public Follow following(final Long myId, final Long targetId) { | ||
final User me = findUserById(myId); | ||
final User target = findUserById(targetId); | ||
final Follow follow = follow(target, me); | ||
me.addFollowing(follow); | ||
return followRepository.save(follow); | ||
} | ||
|
||
public User unfollowing(final Long myId, final Long targetId) { | ||
final User me = findUserById(myId); | ||
final User target = findUserById(targetId); | ||
final Follow follow = followRepository.findByFollowingAndFollower(target, me).orElseThrow(IllegalArgumentException::new); | ||
followRepository.delete(follow); | ||
return me; | ||
} | ||
|
||
private Follow follow(final User following, final User follower) { | ||
return Follow.Builder() | ||
.following(following) | ||
.follower(follower) | ||
.build(); | ||
} | ||
|
||
private User findUserById(final Long id) { | ||
return userRepository.findById(id).orElseThrow(() -> new IdentityNotFoundException(id)); | ||
} | ||
|
||
|
||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/study/realworld/domain/follow/domain/FollowRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
package com.study.realworld.domain.follow.domain; | ||
|
||
import com.study.realworld.domain.user.domain.persist.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface FollowRepository extends JpaRepository<Follow, Long> { | ||
Optional<Follow> findByFollowingAndFollower(User following, User follower); | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/study/realworld/domain/follow/domain/Followings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.study.realworld.domain.follow.domain; | ||
|
||
import javax.persistence.CascadeType; | ||
import javax.persistence.Embeddable; | ||
import javax.persistence.OneToMany; | ||
import java.util.HashSet; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
@Embeddable | ||
public class Followings { | ||
|
||
@OneToMany(mappedBy = "following", cascade = CascadeType.ALL, orphanRemoval = true) | ||
private Set<Follow> followings = new HashSet<>(); | ||
|
||
public Followings() { | ||
} | ||
|
||
public Followings(final Set<Follow> followings) { | ||
this.followings = followings; | ||
} | ||
|
||
public void add(final Follow following) { | ||
validateArgumentNull(following); | ||
followings.add(following); | ||
} | ||
|
||
private void validateArgumentNull(final Follow following) { | ||
if(Objects.isNull(following)) { | ||
throw new IllegalArgumentException(); | ||
} | ||
} | ||
|
||
public Set<Follow> getFollowings() { | ||
return followings; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters