Skip to content

Commit

Permalink
Merge pull request #452 from GBGreenBravo/refactor/#451_Add_Image_Upl…
Browse files Browse the repository at this point in the history
…oad_When_Signing_Up

Refactor : 회원가입 시, 프로필이미지 업로드 가능하도록 수정
  • Loading branch information
kkkapuq authored May 1, 2024
2 parents 2e1aa77 + a808007 commit 57d529a
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
import org.orury.domain.auth.domain.dto.SignUpDto;
import org.orury.domain.user.domain.dto.UserDto;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
@RequiredArgsConstructor
public class AuthFacade {
private final AuthService authService;

public SignUpResponse signUp(UserDto userDto) {
SignUpDto signUpDto = authService.signUp(userDto);
public SignUpResponse signUp(UserDto userDto, MultipartFile image) {
SignUpDto signUpDto = authService.signUp(userDto, image);
return SignUpResponse.of(signUpDto);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import org.orury.domain.auth.domain.dto.LoginDto;
import org.orury.domain.auth.domain.dto.SignUpDto;
import org.orury.domain.user.domain.dto.UserDto;
import org.springframework.web.multipart.MultipartFile;

public interface AuthService {
SignUpDto signUp(UserDto userDto);
SignUpDto signUp(UserDto userDto, MultipartFile file);

LoginDto login(LoginRequest loginRequest);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.orury.domain.auth.domain.dto.JwtToken;
import org.orury.domain.auth.domain.dto.LoginDto;
import org.orury.domain.auth.domain.dto.SignUpDto;
import org.orury.domain.image.domain.ImageStore;
import org.orury.domain.user.domain.UserReader;
import org.orury.domain.user.domain.UserStore;
import org.orury.domain.user.domain.dto.UserDto;
Expand All @@ -22,22 +23,27 @@
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;

import static org.orury.common.util.S3Folder.USER;

@RequiredArgsConstructor
@Service
public class AuthServiceImpl implements AuthService {
private final UserReader userReader;
private final UserStore userStore;
private final JwtTokenService jwtTokenService;
private final OAuthServiceManager oAuthServiceManager;
private final ImageStore imageStore;

@Transactional
@Override
public SignUpDto signUp(UserDto userDto) {
public SignUpDto signUp(UserDto userDto, MultipartFile file) {
String image = imageStore.upload(USER, file);
try {
userStore.saveAndFlush(userDto.toEntity());
userStore.saveAndFlush(userDto.toEntity(image));
} catch (DataIntegrityViolationException e) {
throw new BusinessException(UserErrorCode.DUPLICATED_USER);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
import org.orury.client.auth.interfaces.request.SignUpRequest;
import org.orury.common.error.code.AuthErrorCode;
import org.orury.domain.base.converter.ApiResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import static org.orury.client.auth.interfaces.message.AuthMessage.*;

Expand All @@ -26,8 +24,8 @@ public class AuthController {

@Operation(summary = "회원가입", description = "소셜 로그인을 통해 전달받은 정보를 기반으로 회원가입 수행")
@PostMapping("/sign-up")
public ApiResponse signUp(@Valid @RequestBody SignUpRequest request) {
var signUpResponse = authFacade.signUp(request.toDto());
public ApiResponse signUp(@Valid @RequestPart SignUpRequest request, @RequestPart(required = false) MultipartFile image) {
var signUpResponse = authFacade.signUp(request.toDto(), image);
return ApiResponse.of(SIGNUP_SUCCESS.getMessage(), signUpResponse);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public record SignUpRequest(
String nickname,
int gender,
LocalDate birthday,
String profileImage,

@Size(min = 1, max = 3, message = "활동 지역은 최소 1개, 최대 3개까지만 추가할 수 있습니다.")
@EnumValues(enumClass = Region.class, message = "유효하지 않은 지역이 포함되어 있습니다.")
Expand All @@ -25,8 +24,8 @@ public record SignUpRequest(
// UUID 비밀번호를 암호화 시키기 위한 PasswordEncoder
private static final BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();

public static SignUpRequest of(int signUpType, String email, String nickname, int gender, LocalDate birthday, String profileImage, List<Region> regions) {
return new SignUpRequest(signUpType, email, nickname, gender, birthday, profileImage, regions);
public static SignUpRequest of(int signUpType, String email, String nickname, int gender, LocalDate birthday, List<Region> regions) {
return new SignUpRequest(signUpType, email, nickname, gender, birthday, regions);
}

public UserDto toDto() {
Expand All @@ -38,7 +37,7 @@ public UserDto toDto() {
signUpType,
gender,
birthday,
profileImage,
null,
null,
null,
UserStatus.ENABLE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
import org.orury.domain.auth.domain.dto.SignUpDto;
import org.orury.domain.user.domain.dto.UserDto;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.multipart.MultipartFile;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.orury.client.ClientFixtureFactory.TestLoginRequest.createLoginRequest;
import static org.orury.domain.AuthDomainFixture.TestLoginDto.createLoginDto;
Expand All @@ -32,16 +34,17 @@ void when_UserDto_Then_SignUpAndRetrieveSignUpResponse() {
// given
UserDto userDto = createUserDto().build().get();
SignUpDto signUpDto = createSignUpDto().build().get();
MultipartFile image = mock(MultipartFile.class);

given(authService.signUp(userDto))
given(authService.signUp(userDto, image))
.willReturn(signUpDto);

// when
authFacade.signUp(userDto);
authFacade.signUp(userDto, image);

// then
then(authService).should(times(1))
.signUp(userDto);
.signUp(any(UserDto.class), any(MultipartFile.class));
}

@DisplayName("정상 회원에 대해 LoginRequest를 받아 로그인하고, LoginResponse를 반환한다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
import org.orury.common.error.code.UserErrorCode;
import org.orury.common.error.exception.AuthException;
import org.orury.common.error.exception.BusinessException;
import org.orury.common.util.S3Folder;
import org.orury.domain.auth.domain.dto.LoginDto;
import org.orury.domain.user.domain.dto.UserDto;
import org.orury.domain.user.domain.dto.UserStatus;
import org.orury.domain.user.domain.entity.User;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.multipart.MultipartFile;

import java.util.Optional;

Expand Down Expand Up @@ -48,9 +50,11 @@ void when_NotDuplicatedEmail_Then_SaveUserAndRetrieveJwtToken() {
.willReturn(Optional.of(user));

// when
authService.signUp(userDto);
authService.signUp(userDto, mock(MultipartFile.class));

// then
then(imageStore).should(only())
.upload(any(S3Folder.class), any(MultipartFile.class));
then(userStore).should(only())
.saveAndFlush(any());
then(userReader).should(times(1))
Expand All @@ -72,10 +76,12 @@ void when_DuplicatedEmail_Then_DuplicatedUserException() {

// when & then
Exception exception = assertThrows(BusinessException.class,
() -> authService.signUp(userDto));
() -> authService.signUp(userDto, mock(MultipartFile.class)));

assertEquals(UserErrorCode.DUPLICATED_USER.getMessage(), exception.getMessage());

then(imageStore).should(only())
.upload(any(S3Folder.class), any(MultipartFile.class));
then(userStore).should(only())
.saveAndFlush(any());
then(userReader).should(never())
Expand All @@ -97,10 +103,12 @@ void when_UserWithSignUpEmailDoesNotExist_Then_NotExistingUserAccountException()

// when & then
Exception exception = assertThrows(AuthException.class,
() -> authService.signUp(userDto));
() -> authService.signUp(userDto, mock(MultipartFile.class)));

assertEquals(AuthErrorCode.NOT_EXISTING_USER_ACCOUNT.getMessage(), exception.getMessage());

then(imageStore).should(only())
.upload(any(S3Folder.class), any(MultipartFile.class));
then(userStore).should(times(1))
.saveAndFlush(any());
then(userReader).should(times(1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ void setUp() {
reviewStore = mock(ReviewStore.class);

//services
authService = new AuthServiceImpl(userReader, userStore, jwtTokenService, oAuthServiceManager);
authService = new AuthServiceImpl(userReader, userStore, jwtTokenService, oAuthServiceManager, imageStore);
commentService = new CommentServiceImpl(commentReader, commentStore);
postService = new PostServiceImpl(postReader, postStore, imageStore);
crewService = new CrewServiceImpl(crewReader, crewStore, crewTagReader, crewTagStore, crewMemberReader, crewMemberStore, crewApplicationReader, crewApplicationStore, meetingStore, meetingMemberStore, userReader, imageStore, crewPolicy, crewCreatePolicy, crewUpdatePolicy, crewApplicationPolicy);
Expand Down

0 comments on commit 57d529a

Please sign in to comment.