Skip to content

Commit

Permalink
✨ init basic logic of follow
Browse files Browse the repository at this point in the history
  • Loading branch information
geoje committed Aug 14, 2024
1 parent e1140a7 commit 73d8783
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 0 deletions.
39 changes: 39 additions & 0 deletions backend/src/main/java/net/pengcook/follow/entity/Follow.java
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);
}
}
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);
}
}
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);
}
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);
}
}
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() {

}
}
18 changes: 18 additions & 0 deletions backend/src/test/resources/data/follow.sql
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');

0 comments on commit 73d8783

Please sign in to comment.