Skip to content

Commit

Permalink
[feat] 루트 사용자 팔로우 로직 추가 (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinkonu committed Jul 15, 2024
1 parent 1116297 commit fa1086d
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.recordy.server.user.service.impl;

import lombok.RequiredArgsConstructor;
import org.recordy.server.auth.domain.Auth;
import org.recordy.server.auth.domain.AuthPlatform;
import org.recordy.server.auth.service.AuthService;
import org.recordy.server.auth.service.AuthTokenService;
import org.recordy.server.common.message.ErrorMessage;
import org.recordy.server.record.repository.RecordRepository;
import org.recordy.server.subscribe.domain.Subscribe;
import org.recordy.server.subscribe.repository.SubscribeRepository;
import org.recordy.server.user.controller.dto.request.TermsAgreement;
import org.recordy.server.user.domain.usecase.UserProfile;
Expand All @@ -17,20 +17,38 @@
import org.recordy.server.user.exception.UserException;
import org.recordy.server.user.repository.UserRepository;
import org.recordy.server.user.service.UserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Optional;

@RequiredArgsConstructor
@Service
public class UserServiceImpl implements UserService {

private final Long rootUserId;

private final UserRepository userRepository;
private final SubscribeRepository subscribeRepository;
private final RecordRepository recordRepository;
private final AuthService authService;
private final AuthTokenService authTokenService;

public UserServiceImpl(
@Value("${user.root.id}") Long rootUserId,
UserRepository userRepository,
SubscribeRepository subscribeRepository,
RecordRepository recordRepository,
AuthService authService,
AuthTokenService authTokenService
) {
this.rootUserId = rootUserId;
this.userRepository = userRepository;
this.subscribeRepository = subscribeRepository;
this.recordRepository = recordRepository;
this.authService = authService;
this.authTokenService = authTokenService;
}

@Override
public Auth signIn(UserSignIn userSignIn) {
AuthPlatform platform = authService.getPlatform(userSignIn);
Expand Down Expand Up @@ -59,10 +77,21 @@ public User signUp(UserSignUp userSignUp) {
User pendingUser = userRepository.findById(userSignUp.userId())
.orElseThrow(() -> new UserException(ErrorMessage.USER_NOT_FOUND));
User updatedUser = pendingUser.activate(userSignUp);
followRoot(updatedUser);

return userRepository.save(updatedUser);
}

private void followRoot(User user) {
userRepository.findById(rootUserId)
.ifPresent(rootUser ->
subscribeRepository.save(Subscribe.builder()
.subscribingUser(user)
.subscribedUser(rootUser)
.build())
);
}

@Override
public String reissueToken(String refreshToken) {
String platformId = authTokenService.getPlatformIdFromRefreshToken(refreshToken);
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/recordy/server/mock/FakeContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.List;

import static org.mockito.Mockito.mock;
import static org.recordy.server.util.DomainFixture.ROOT_USER_ID;

public class FakeContainer {

Expand Down Expand Up @@ -117,7 +118,7 @@ public FakeContainer() {
authRepository
);
this.authService = new AuthServiceImpl(authRepository, authPlatformServiceFactory, authTokenService);
this.userService = new UserServiceImpl(userRepository, subscribeRepository, recordRepository, authService, authTokenService);
this.userService = new UserServiceImpl(ROOT_USER_ID, userRepository, subscribeRepository, recordRepository, authService, authTokenService);
this.fileService = new FakeFileService();
this.recordService = new RecordServiceImpl(recordRepository, viewRepository, fileService, userService);
this.keywordService = new KeywordServiceImpl(keywordRepository);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class FakeKakaoFeignClient implements KakaoFeignClient {

@Override
public KakaoPlatformInfo getKakaoAccessTokenInfo(String accessToken) {
if (!accessToken.equals(DomainFixture.PLATFORM_TOKEN)) {
if (!accessToken.equals(DomainFixture.PLATFORM_TOKEN) && !accessToken.equals("root")) {
throw new FeignException.Unauthorized("Unauthorized",
Request.create(
Request.HttpMethod.GET,
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/org/recordy/server/user/service/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,26 @@ void init() {
assertThat(result.getStatus()).isEqualTo(UserStatus.ACTIVE);
}

@Test
void signUp_통해_가입된_사용자가_ROOT_사용자를_팔로우할__있다() {
// given
UserSignIn rootSignIn = new UserSignIn("root", AuthPlatform.Type.KAKAO);
userService.signIn(rootSignIn);
UserSignUp rootSignUp = new UserSignUp(1L, "유영", TermsAgreement.of(true, true, true));
userService.signUp(rootSignUp);

// when
UserSignIn userSignIn = DomainFixture.createUserSignIn(AuthPlatform.Type.APPLE);
userService.signIn(userSignIn);
UserSignUp userSignUp = DomainFixture.createUserSignUp();
User result = userService.signUp(userSignUp);

// then
assertAll(
() -> assertThat(userRepository.findById(3)).isNotEmpty()
);
}

@Test
void reissueToken_통해_accessToken_재발급_받을__있다() {
//given
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/org/recordy/server/util/DomainFixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public final class DomainFixture {
* USER
*/
public static final Long USER_ID = 1L;
public static final Long ROOT_USER_ID = 1L;
public static final UserStatus DEFAULT_USER_STATUS = UserStatus.ACTIVE;
public static final String USER_PROFILE_IMAGE_URL = "profile";
public static final String USER_NICKNAME = "레코디";
Expand Down Expand Up @@ -123,6 +124,30 @@ public static UserSignUp createUserSignUp() {
);
}

public static UserSignUp createUserSignUp(Long id) {
return new UserSignUp(
id,
USER_NICKNAME,
TermsAgreement.of(
USE_TERM_AGREEMENT,
PERSONAL_INFO_TERM_AGREEMENT,
AGE_TERM_AGREEMENT
)
);
}

public static UserSignUp createUserSignUp(Long id, String nickname) {
return new UserSignUp(
id,
nickname,
TermsAgreement.of(
USE_TERM_AGREEMENT,
PERSONAL_INFO_TERM_AGREEMENT,
AGE_TERM_AGREEMENT
)
);
}

public static User createUser() {
return User.builder()
.id(USER_ID)
Expand All @@ -133,6 +158,16 @@ public static User createUser() {
.build();
}

public static User createUser(String nickname) {
return User.builder()
.id(USER_ID)
.authPlatform(createAuthPlatform())
.status(DEFAULT_USER_STATUS)
.nickname(nickname)
.termsAgreement(TermsAgreement.of(USE_TERM_AGREEMENT, PERSONAL_INFO_TERM_AGREEMENT, AGE_TERM_AGREEMENT))
.build();
}

public static User createUser(long id) {
return User.builder()
.id(id)
Expand All @@ -143,6 +178,16 @@ public static User createUser(long id) {
.build();
}

public static User createUser(long id, String nickname) {
return User.builder()
.id(id)
.authPlatform(createAuthPlatform())
.status(DEFAULT_USER_STATUS)
.nickname(nickname)
.termsAgreement(TermsAgreement.of(USE_TERM_AGREEMENT, PERSONAL_INFO_TERM_AGREEMENT, AGE_TERM_AGREEMENT))
.build();
}

public static User createUser(UserStatus userStatus) {
return User.builder()
.id(USER_ID)
Expand Down

0 comments on commit fa1086d

Please sign in to comment.