Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] feat: 사용자 기본 닉네임 정책 추가 (#972) #974

Merged
merged 1 commit into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.festago.auth.domain.RefreshToken;
import com.festago.auth.domain.UserInfo;
import com.festago.auth.domain.UserInfoMemberMapper;
import com.festago.auth.dto.event.MemberDeletedEvent;
import com.festago.auth.dto.v1.LoginResult;
import com.festago.auth.dto.v1.TokenRefreshResult;
Expand All @@ -28,6 +29,7 @@ public class MemberAuthCommandService {
private final MemberRepository memberRepository;
private final RefreshTokenRepository refreshTokenRepository;
private final ApplicationEventPublisher eventPublisher;
private final UserInfoMemberMapper userInfoMemberMapper;
private final Clock clock;

public LoginResult oAuth2Login(UserInfo userInfo) {
Expand All @@ -44,7 +46,8 @@ public LoginResult oAuth2Login(UserInfo userInfo) {
}

private Member signUp(UserInfo userInfo) {
return memberRepository.save(userInfo.toMember());
Member member = userInfoMemberMapper.toMember(userInfo);
return memberRepository.save(member);
}

private RefreshToken saveRefreshToken(Long memberId) {
Expand Down
9 changes: 0 additions & 9 deletions backend/src/main/java/com/festago/auth/domain/UserInfo.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.festago.auth.domain;

import com.festago.member.domain.Member;
import lombok.Builder;

@Builder
Expand All @@ -11,12 +10,4 @@ public record UserInfo(
String profileImage
) {

public Member toMember() {
return new Member(
socialId,
socialType,
nickname,
profileImage
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.festago.auth.domain;

import com.festago.member.domain.DefaultNicknamePolicy;
import com.festago.member.domain.Member;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
@RequiredArgsConstructor
public class UserInfoMemberMapper {

private final DefaultNicknamePolicy defaultNicknamePolicy;

public Member toMember(UserInfo userInfo) {
String nickname = userInfo.nickname();
return new Member(
userInfo.socialId(),
userInfo.socialType(),
StringUtils.hasText(nickname) ? nickname : defaultNicknamePolicy.generate(),
userInfo.profileImage()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.festago.member.config;

import com.festago.member.domain.DefaultNicknamePolicy;
import com.festago.member.infrastructure.DefaultNicknamePolicyImpl;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DefaultNicknamePolicyConfig {

@Bean
public DefaultNicknamePolicy defaultNicknamePolicy() {
List<String> adjectives = List.of(
"츄러스를 먹는", "노래 부르는", "때창하는", "응원하는",
"응원봉을 든", "타코야끼를 먹는", "공연에 심취한", "신나는",
"춤추는", "행복한", "즐거운", "신나는", "흥겨운"
);
List<String> nouns = List.of(
"다람쥐", "토끼", "고양이", "펭귄",
"캥거루", "사슴", "미어캣", "호랑이",
"여우", "판다", "고슴도치", "토끼",
"햄스터", "얼룩말", "너구리", "치타"
);
return new DefaultNicknamePolicyImpl(adjectives, nouns);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.festago.member.domain;

public interface DefaultNicknamePolicy {

String generate();
}
4 changes: 2 additions & 2 deletions backend/src/main/java/com/festago/member/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Member extends BaseTimeEntity {

private static final String DEFAULT_NICKNAME = "FestivalLover";
private static final int MAX_SOCIAL_ID_LENGTH = 255;
private static final int MAX_NICKNAME_LENGTH = 30;
private static final int MAX_PROFILE_IMAGE_LENGTH = 255;
Expand Down Expand Up @@ -76,7 +75,7 @@ public Member(Long id, String socialId, SocialType socialType, String nickname,
this.id = id;
this.socialId = socialId;
this.socialType = socialType;
this.nickname = (StringUtils.hasText(nickname)) ? nickname : DEFAULT_NICKNAME;
this.nickname = nickname;
this.profileImage = ImageUrlHelper.getBlankStringIfBlank(profileImage);
}

Expand All @@ -100,6 +99,7 @@ private void validateSocialType(SocialType socialType) {
private void validateNickname(String nickname) {
String fieldName = "nickname";
Validator.maxLength(nickname, MAX_NICKNAME_LENGTH, fieldName);
Validator.notBlank(nickname, fieldName);
}

private void validateProfileImage(String profileImage) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.festago.member.infrastructure;

import com.festago.member.domain.DefaultNicknamePolicy;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class DefaultNicknamePolicyImpl implements DefaultNicknamePolicy {

private final List<String> adjectives;
private final List<String> nouns;

@Override
public String generate() {
Random random = ThreadLocalRandom.current();
String adjective = adjectives.get(random.nextInt(adjectives.size()));
String noun = nouns.get(random.nextInt(nouns.size()));
return adjective + " " + noun;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import com.festago.auth.domain.RefreshToken;
import com.festago.auth.domain.SocialType;
import com.festago.auth.domain.UserInfo;
import com.festago.auth.domain.UserInfoMemberMapper;
import com.festago.auth.repository.MemoryRefreshTokenRepository;
import com.festago.auth.repository.RefreshTokenRepository;
import com.festago.common.exception.ErrorCode;
import com.festago.common.exception.NotFoundException;
import com.festago.common.exception.UnauthorizedException;
import com.festago.member.domain.DefaultNicknamePolicy;
import com.festago.member.domain.Member;
import com.festago.member.repository.MemberRepository;
import com.festago.member.repository.MemoryMemberRepository;
Expand Down Expand Up @@ -46,10 +48,12 @@ void setUp() {
clock = spy(Clock.systemDefaultZone());
memberRepository = new MemoryMemberRepository();
refreshTokenRepository = new MemoryRefreshTokenRepository();
DefaultNicknamePolicy defaultNicknamePolicy = () -> "nickname";
memberAuthCommandService = new MemberAuthCommandService(
memberRepository,
refreshTokenRepository,
mock(ApplicationEventPublisher.class),
new UserInfoMemberMapper(defaultNicknamePolicy),
clock
);
}
Expand Down
10 changes: 4 additions & 6 deletions backend/src/test/java/com/festago/member/domain/MemberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,10 @@ class MemberTest {
@ParameterizedTest
@NullSource
@ValueSource(strings = {"", " ", "\t", "\n"})
void nickname이_null_또는_공백이면_기본_닉네임_생성(String nickname) {
// given && when
Member member = new Member(1L, "12345", SocialType.FESTAGO, nickname, "profileImage.png");

// then
assertThat(member.getNickname()).isEqualTo("FestivalLover");
void nickname이_null_또는_공백이면_예외(String nickname) {
// when & then
assertThatThrownBy(() -> new Member(1L, "12345", SocialType.FESTAGO, nickname, "profileImage.png"))
.isInstanceOf(ValidException.class);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.festago.member.infrastructure;

import static org.assertj.core.api.Assertions.*;

import com.festago.member.domain.DefaultNicknamePolicy;
import java.util.List;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@SuppressWarnings("NonAsciiCharacters")
class DefaultNicknamePolicyImplTest {

@Test
void 형용사와_명사가_합쳐진_닉네임이_반환된다() {
// given
DefaultNicknamePolicy defaultNicknamePolicy = new DefaultNicknamePolicyImpl(
List.of("춤추는"),
List.of("다람쥐")
);

// when
String nickname = defaultNicknamePolicy.generate();

// then
assertThat(nickname).isEqualTo("춤추는 다람쥐");
}
}
Loading