Skip to content

Commit

Permalink
feat: 등록된 서재 기본 페이지 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
geneaky committed Oct 3, 2024
1 parent fbb65e8 commit b6b0b41
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 52 deletions.
3 changes: 2 additions & 1 deletion lombok.config
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
lombok.addLombokGeneratedAnnotation = true
lombok.addLombokGeneratedAnnotation = true
lombok.copyableAnnotations += org.springframework.beans.factory.annotation.Qualifier
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ public BookShelf toTarget() {
return BookShelf.builder()
.readingStatus(this.readingStatus)
.star(this.star)
.pages(0)
.build();
}
return BookShelf.builder()
.readingStatus(this.readingStatus)
.pages(0)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ public void userSignUp(@Valid @RequestPart UserSignUpRequest userSignUpRequest,
@RequestHeader(OIDC) @NotBlank @Pattern(regexp = "^(Bearer)\\s.+") String bearerToken) {
String oauth2MemberNumber = idTokenManager.getOAuth2MemberNumberFromIdToken(bearerToken,
userSignUpRequest.getOauth2Provider());
String userEmail = idTokenManager.getUserEmailFromToken(bearerToken,
userSignUpRequest.getOauth2Provider());
String userEmail = idTokenManager.getUserEmailFromToken(bearerToken, userSignUpRequest.getOauth2Provider());

userService.registerNewUser(userSignUpRequest, userProfileImage, oauth2MemberNumber, userEmail);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.time.format.DateTimeFormatter;
import java.util.UUID;
import java.util.function.Consumer;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -25,26 +26,19 @@
import toy.bookchat.bookchat.infrastructure.fcm.service.PushService;
import toy.bookchat.bookchat.infrastructure.s3.StorageService;

@RequiredArgsConstructor

@Service
public class UserService {

private final UserRepository userRepository;
private final UserReader userReader;
private final DeviceRepository deviceRepository;
private final PushService pushService;
@Qualifier("userProfileStorageService")
private final StorageService storageService;
private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

public UserService(UserRepository userRepository, UserReader userReader,
DeviceRepository deviceRepository, PushService pushService,
@Qualifier("userProfileStorageService") StorageService storageService) {
this.userRepository = userRepository;
this.userReader = userReader;
this.deviceRepository = deviceRepository;
this.pushService = pushService;
this.storageService = storageService;
}

@Transactional(readOnly = true)
public boolean isDuplicatedName(String nickname) {
return userRepository.existsByNickname(nickname);
Expand All @@ -53,25 +47,13 @@ public boolean isDuplicatedName(String nickname) {
@Transactional
public void registerNewUser(UserSignUpRequest userSignUpRequest, MultipartFile userProfileImage, String userName,
String userEmail) {
if (imageExistent(userProfileImage)) {
if (doesImageExist(userProfileImage)) {
uploadWithImage(userSignUpRequest, userProfileImage, userName, userEmail);
return;
}
uploadWithoutImage(userSignUpRequest, userName, userEmail);
}

private void uploadWithImage(UserSignUpRequest userSignUpRequest,
MultipartFile userProfileImage, String userName, String userEmail) {
String uploadFileUrl = storageService.upload(userProfileImage, UUID.randomUUID().toString(),
LocalDateTime.now().format(dateTimeFormatter));
saveUser(userSignUpRequest, userName, userEmail, uploadFileUrl);
}

private void uploadWithoutImage(UserSignUpRequest userSignUpRequest, String userName,
String userEmail) {
saveUser(userSignUpRequest, userName, userEmail, null);
}

@Transactional(readOnly = true)
public UserEntity findUserByUsername(String oauth2MemberNumber) {
return userReader.readUserEntity(oauth2MemberNumber);
Expand All @@ -87,15 +69,51 @@ public void deleteUser(Long userId) {
public void updateUserProfile(ChangeUserNicknameRequest changeUserNicknameRequest, MultipartFile userProfileImage,
Long userId) {
UserEntity userEntity = userReader.readUserEntity(userId);
if (imageExistent(userProfileImage)) {
if (doesImageExist(userProfileImage)) {
updateNicknameWithProfileImage(changeUserNicknameRequest, userProfileImage, userEntity);
return;
}
userEntity.changeUserNickname(changeUserNicknameRequest.getNickname());
userEntity.changeProfileImageUrl(null);
}

private boolean imageExistent(MultipartFile userProfileImage) {
@Transactional
public void checkDevice(UserSignInRequest userSignInRequest, Long userId) {
deviceRepository.findByUserId(userId)
.ifPresentOrElse(changeDeviceIfApproved(userSignInRequest),
registerUserDevice(userSignInRequest, userId));
}

@Transactional
public void deleteDevice(Long userId) {
deviceRepository.deleteByUserId(userId);
}

@Transactional(readOnly = true)
public MemberProfileResponse getMemberProfile(Long memberId) {
UserEntity userEntity = userReader.readUserEntity(memberId);
return MemberProfileResponse.of(userEntity);
}

@Transactional(readOnly = true)
public User findUser(Long userId) {
return userReader.readUser(userId);

}

private void uploadWithImage(UserSignUpRequest userSignUpRequest, MultipartFile userProfileImage, String userName,
String userEmail) {
String uploadFileUrl = storageService.upload(userProfileImage, UUID.randomUUID().toString(),
LocalDateTime.now().format(dateTimeFormatter));
saveUser(userSignUpRequest, userName, userEmail, uploadFileUrl);
}

private void uploadWithoutImage(UserSignUpRequest userSignUpRequest, String userName,
String userEmail) {
saveUser(userSignUpRequest, userName, userEmail, null);
}

private boolean doesImageExist(MultipartFile userProfileImage) {
return userProfileImage != null;
}

Expand All @@ -107,8 +125,7 @@ private void updateNicknameWithProfileImage(ChangeUserNicknameRequest changeUser
userEntity.changeProfileImageUrl(uploadFileUrl);
}

private void saveUser(UserSignUpRequest userSignUpRequest, String userName,
String email, String profileImageUrl) {
private void saveUser(UserSignUpRequest userSignUpRequest, String userName, String email, String profileImageUrl) {
userRepository.findByName(userName).ifPresentOrElse(user -> {
throw new UserAlreadySignUpException();
}, () -> {
Expand All @@ -117,13 +134,6 @@ private void saveUser(UserSignUpRequest userSignUpRequest, String userName,
});
}

@Transactional
public void checkDevice(UserSignInRequest userSignInRequest, Long userId) {
deviceRepository.findByUserId(userId)
.ifPresentOrElse(changeDeviceIfApproved(userSignInRequest),
registerUserDevice(userSignInRequest, userId));
}

private Runnable registerUserDevice(UserSignInRequest userSignInRequest, Long userId) {
return () -> deviceRepository.save(userSignInRequest.createDevice(userId));
}
Expand All @@ -142,19 +152,4 @@ private Consumer<DeviceEntity> changeDeviceIfApproved(UserSignInRequest userSign
};
}

@Transactional
public void deleteDevice(Long userId) {
deviceRepository.deleteByUserId(userId);
}

@Transactional(readOnly = true)
public MemberProfileResponse getMemberProfile(Long memberId) {
UserEntity userEntity = userReader.readUserEntity(memberId);
return MemberProfileResponse.of(userEntity);
}

@Transactional(readOnly = true)
public User findUser(Long userId) {
return userReader.readUser(userId);
}
}

0 comments on commit b6b0b41

Please sign in to comment.