-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
backend/src/main/java/net/pengcook/follow/entity/Follow.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,39 @@ | ||
package net.pengcook.follow.entity; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.Table; | ||
import jakarta.persistence.UniqueConstraint; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import net.pengcook.user.domain.User; | ||
|
||
@Entity | ||
@Getter | ||
@Table(uniqueConstraints = {@UniqueConstraint(columnNames = {"follower_id", "followee_id"})}) | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class Follow { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private long id; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "follower_id") | ||
private User follower; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "followee_id") | ||
private User followee; | ||
|
||
public Follow(User follower, User followee) { | ||
this(0L, follower, followee); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
backend/src/main/java/net/pengcook/follow/exception/UserNotFoundException.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,11 @@ | ||
package net.pengcook.follow.exception; | ||
|
||
import net.pengcook.exception.DomainException; | ||
import org.springframework.http.HttpStatus; | ||
|
||
public class UserNotFoundException extends DomainException { | ||
|
||
public UserNotFoundException(String message) { | ||
super(HttpStatus.NOT_FOUND, message); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
backend/src/main/java/net/pengcook/follow/repository/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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package net.pengcook.follow.repository; | ||
|
||
import net.pengcook.follow.entity.Follow; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface FollowRepository extends JpaRepository<Follow, Long> { | ||
|
||
void deleteByFollowerIdAndFolloweeId(long followerId, long followeeId); | ||
} |
31 changes: 31 additions & 0 deletions
31
backend/src/main/java/net/pengcook/follow/service/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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package net.pengcook.follow.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import net.pengcook.authentication.domain.UserInfo; | ||
import net.pengcook.follow.entity.Follow; | ||
import net.pengcook.follow.exception.UserNotFoundException; | ||
import net.pengcook.follow.repository.FollowRepository; | ||
import net.pengcook.user.domain.User; | ||
import net.pengcook.user.repository.UserRepository; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class FollowService { | ||
|
||
private final FollowRepository followRepository; | ||
private final UserRepository userRepository; | ||
|
||
public void follow(UserInfo userInfo, long followeeId) { | ||
User follower = userRepository.findById(userInfo.getId()) | ||
.orElseThrow(() -> new UserNotFoundException("νλ‘μκ° μ‘΄μ¬νμ§ μμ΅λλ€.")); | ||
User followee = userRepository.findById(followeeId) | ||
.orElseThrow(() -> new UserNotFoundException("νλ‘μ΄κ° μ‘΄μ¬νμ§ μμ΅λλ€.")); | ||
|
||
followRepository.save(new Follow(follower, followee)); | ||
} | ||
|
||
public void unfollow(UserInfo userInfo, long followeeId) { | ||
followRepository.deleteByFollowerIdAndFolloweeId(userInfo.getId(), followeeId); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
backend/src/test/java/net/pengcook/follow/service/FollowServiceTest.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,15 @@ | ||
package net.pengcook.follow.service; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
|
||
@DataJpaTest | ||
class FollowServiceTest { | ||
|
||
@Test | ||
@DisplayName("ν μ¬λμ΄ λ€λ₯Έ μ¬λμ νλ‘μ° νλ€.") | ||
void follow() { | ||
|
||
} | ||
} |
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,18 @@ | ||
SET | ||
REFERENTIAL_INTEGRITY FALSE; | ||
|
||
TRUNCATE TABLE users; | ||
ALTER TABLE users | ||
ALTER COLUMN id RESTART; | ||
|
||
TRUNCATE TABLE follow; | ||
ALTER TABLE follow | ||
ALTER COLUMN id RESTART; | ||
|
||
SET | ||
REFERENTIAL_INTEGRITY TRUE; | ||
|
||
INSERT INTO users (email, username, nickname, image, region) | ||
VALUES ('[email protected]', 'ela', 'μλΌ', 'ela.jpg', 'KOREA'), | ||
('[email protected]', 'loki', 'λ‘ν€', 'loki.jpg', 'KOREA'), | ||
('[email protected]', 'seyang', 'μμ', 'seyang.jpg', 'GERMANY'); |