Skip to content

Commit

Permalink
feat: ProfileController Facade & DCI 패턴 (#243)
Browse files Browse the repository at this point in the history
- ProfileService 제거
- ProfileFacade & ProfileFacadeService 구현
- 테스트 코드 DCI 패턴 도입
  • Loading branch information
kimdozzi authored Aug 14, 2024
1 parent 909ce59 commit 8eb7648
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import com.genius.gitget.global.file.service.FilesService;
import com.genius.gitget.global.security.dto.AuthResponse;
import com.genius.gitget.global.util.exception.BusinessException;
import com.genius.gitget.global.util.exception.ErrorCode;
import com.genius.gitget.signout.Signout;
import com.genius.gitget.signout.SignoutRepository;
import com.genius.gitget.store.item.domain.Item;
import com.genius.gitget.store.item.service.OrdersService;
import java.util.List;
Expand All @@ -33,6 +36,7 @@ public class UserService {
private final OrdersService ordersService;
private final FilesService filesService;
private final EncryptUtil encryptUtil;
private final SignoutRepository signoutRepository;

@Value("${admin.githubId}")
private List<String> adminIds;
Expand All @@ -53,6 +57,24 @@ public Long save(User user) {
return userRepository.saveAndFlush(user).getId();
}


public void delete(Long userId, String identifier, String reason) {
userRepository.deleteById(userId);
signoutRepository.save(
Signout.builder()
.identifier(identifier)
.reason(reason)
.build());
}

// 포인트 조회
public Long getUserPoint(Long userId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new BusinessException(ErrorCode.MEMBER_NOT_FOUND));

return user.getPoint();
}

@Transactional
public Long signup(SignupRequest requestUser) {
User user = findUserByIdentifier(requestUser.identifier());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import com.genius.gitget.profile.dto.UserInterestUpdateRequest;
import com.genius.gitget.profile.dto.UserPointResponse;
import com.genius.gitget.profile.dto.UserSignoutRequest;
import com.genius.gitget.profile.service.ProfileService;
import com.genius.gitget.profile.service.ProfileFacade;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
Expand All @@ -30,13 +30,13 @@
@RequiredArgsConstructor
@RequestMapping("/api/profile")
public class ProfileController {
private final ProfileService profileService;
private final ProfileFacade profileFacade;

// 마이페이지 - 사용자 상세 정보 조회
@GetMapping
public ResponseEntity<SingleResponse<UserDetailsInformationResponse>> getUserDetailsInformation(
@AuthenticationPrincipal UserPrincipal userPrincipal) {
UserDetailsInformationResponse userInformation = profileService.getUserDetailsInformation(
UserDetailsInformationResponse userInformation = profileFacade.getUserDetailsInformation(
userPrincipal.getUser());
return ResponseEntity.ok()
.body(new SingleResponse<>(SUCCESS.getStatus(), SUCCESS.getMessage(),
Expand All @@ -48,7 +48,7 @@ public ResponseEntity<SingleResponse<UserDetailsInformationResponse>> getUserDet
@PostMapping
public ResponseEntity<SingleResponse<UserInformationResponse>> getUserInformation(
@RequestBody UserInformationRequest userInformationRequest) {
UserInformationResponse userInformation = profileService.getUserInformation(userInformationRequest.getUserId());
UserInformationResponse userInformation = profileFacade.getUserInformation(userInformationRequest.getUserId());
return ResponseEntity.ok()
.body(new SingleResponse<>(SUCCESS.getStatus(), SUCCESS.getMessage(),
userInformation)
Expand All @@ -61,7 +61,7 @@ public ResponseEntity<SingleResponse<UserIndexResponse>> updateUserInformation(
@AuthenticationPrincipal UserPrincipal userPrincipal,
@RequestBody UserInformationUpdateRequest userInformationUpdateRequest) {

Long userId = profileService.updateUserInformation(userPrincipal.getUser(), userInformationUpdateRequest);
Long userId = profileFacade.updateUserInformation(userPrincipal.getUser(), userInformationUpdateRequest);
UserIndexResponse userIndexResponse = new UserIndexResponse(userId);

return ResponseEntity.ok().body(
Expand All @@ -73,7 +73,7 @@ public ResponseEntity<SingleResponse<UserIndexResponse>> updateUserInformation(
@GetMapping("/interest")
public ResponseEntity<SingleResponse<UserInterestResponse>> getUserInterest(
@AuthenticationPrincipal UserPrincipal userPrincipal) {
UserInterestResponse userInterest = profileService.getUserInterest(userPrincipal.getUser());
UserInterestResponse userInterest = profileFacade.getUserInterest(userPrincipal.getUser());

return ResponseEntity.ok()
.body(new SingleResponse<>(SUCCESS.getStatus(), SUCCESS.getMessage(),
Expand All @@ -85,7 +85,7 @@ public ResponseEntity<SingleResponse<UserInterestResponse>> getUserInterest(
@PostMapping("/interest")
public ResponseEntity<CommonResponse> updateUserTags(@AuthenticationPrincipal UserPrincipal userPrincipal,
@RequestBody UserInterestUpdateRequest userInterestUpdateRequest) {
profileService.updateUserTags(userPrincipal.getUser(), userInterestUpdateRequest);
profileFacade.updateUserTags(userPrincipal.getUser(), userInterestUpdateRequest);

return ResponseEntity.ok()
.body(new CommonResponse(SUCCESS.getStatus(), SUCCESS.getMessage()));
Expand All @@ -96,7 +96,7 @@ public ResponseEntity<CommonResponse> updateUserTags(@AuthenticationPrincipal Us
@GetMapping("/challenges")
public ResponseEntity<SingleResponse<UserChallengeResultResponse>> getUserChallengeResult(
@AuthenticationPrincipal UserPrincipal userPrincipal) {
UserChallengeResultResponse userChallengeResult = profileService.getUserChallengeResult(
UserChallengeResultResponse userChallengeResult = profileFacade.getUserChallengeResult(
userPrincipal.getUser());
return ResponseEntity.ok()
.body(new SingleResponse<>(SUCCESS.getStatus(), SUCCESS.getMessage(),
Expand All @@ -108,7 +108,7 @@ public ResponseEntity<SingleResponse<UserChallengeResultResponse>> getUserChalle
@DeleteMapping
public ResponseEntity<CommonResponse> deleteUserInformation(@AuthenticationPrincipal UserPrincipal userPrincipal,
@RequestBody UserSignoutRequest userSignoutRequest) {
profileService.deleteUserInformation(userPrincipal.getUser(), userSignoutRequest.getReason());
profileFacade.deleteUserInformation(userPrincipal.getUser(), userSignoutRequest.getReason());

return ResponseEntity.ok()
.body(new CommonResponse(SUCCESS.getStatus(), SUCCESS.getMessage()));
Expand All @@ -119,7 +119,7 @@ public ResponseEntity<CommonResponse> deleteUserInformation(@AuthenticationPrinc
@GetMapping("/point")
public ResponseEntity<SingleResponse<UserPointResponse>> getUserPoint(
@AuthenticationPrincipal UserPrincipal userPrincipal) {
UserPointResponse userPoint = profileService.getUserPoint(userPrincipal.getUser());
UserPointResponse userPoint = profileFacade.getUserPoint(userPrincipal.getUser());

return ResponseEntity.ok()
.body(new SingleResponse<>(SUCCESS.getStatus(), SUCCESS.getMessage(),
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/genius/gitget/profile/service/ProfileFacade.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.genius.gitget.profile.service;

import com.genius.gitget.challenge.user.domain.User;
import com.genius.gitget.profile.dto.UserChallengeResultResponse;
import com.genius.gitget.profile.dto.UserDetailsInformationResponse;
import com.genius.gitget.profile.dto.UserInformationResponse;
import com.genius.gitget.profile.dto.UserInformationUpdateRequest;
import com.genius.gitget.profile.dto.UserInterestResponse;
import com.genius.gitget.profile.dto.UserInterestUpdateRequest;
import com.genius.gitget.profile.dto.UserPointResponse;

public interface ProfileFacade {
public UserPointResponse getUserPoint(User user);

public UserInformationResponse getUserInformation(Long userId);

public UserDetailsInformationResponse getUserDetailsInformation(User user);

public Long updateUserInformation(User user, UserInformationUpdateRequest userInformationUpdateRequest);

public void deleteUserInformation(User user, String reason);

public void updateUserTags(User user, UserInterestUpdateRequest userInterestUpdateRequest);

public UserInterestResponse getUserInterest(User user);

public UserChallengeResultResponse getUserChallengeResult(User user);

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,60 +9,58 @@
import com.genius.gitget.challenge.participant.domain.JoinResult;
import com.genius.gitget.challenge.participant.domain.Participant;
import com.genius.gitget.challenge.user.domain.User;
import com.genius.gitget.challenge.user.repository.UserRepository;
import com.genius.gitget.challenge.user.service.UserService;
import com.genius.gitget.global.file.dto.FileResponse;
import com.genius.gitget.global.file.service.FilesService;
import com.genius.gitget.global.util.exception.BusinessException;
import com.genius.gitget.global.util.exception.ErrorCode;
import com.genius.gitget.profile.dto.UserChallengeResultResponse;
import com.genius.gitget.profile.dto.UserDetailsInformationResponse;
import com.genius.gitget.profile.dto.UserInformationResponse;
import com.genius.gitget.profile.dto.UserInformationUpdateRequest;
import com.genius.gitget.profile.dto.UserInterestResponse;
import com.genius.gitget.profile.dto.UserInterestUpdateRequest;
import com.genius.gitget.profile.dto.UserPointResponse;
import com.genius.gitget.signout.Signout;
import com.genius.gitget.signout.SignoutRepository;
import com.genius.gitget.store.item.service.OrdersService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Slf4j
@Transactional(readOnly = true)
public class ProfileService {
private final UserRepository userRepository;
private final FilesService filesService;
private final SignoutRepository signoutRepository;
import org.springframework.stereotype.Component;

@Component
public class ProfileFacadeService implements ProfileFacade {
private final UserService userService;
private final OrdersService ordersService;
private final FilesService filesService;

// 포인트 조회
public ProfileFacadeService(UserService userService, OrdersService ordersService,
FilesService filesService) {
this.userService = userService;
this.ordersService = ordersService;
this.filesService = filesService;
}

@Override
public UserPointResponse getUserPoint(User user) {
Long userPoint = userService.getUserPoint(user.getId());
return UserPointResponse.builder()
.identifier(user.getIdentifier())
.point(user.getPoint())
.point(userPoint)
.build();
}

// 사용자 정보 조회
@Override
public UserInformationResponse getUserInformation(Long userId) {
User findUser = getUserById(userId);
User findUser = userService.findUserById(userId);
Long frameId = ordersService.getUsingFrameItem(userId).getId();

FileResponse fileResponse = filesService.convertToFileResponse(findUser.getFiles());
return UserInformationResponse.createByEntity(findUser, frameId, fileResponse);
}

// 마이페이지 - 사용자 정보 상세 조회
@Override
public UserDetailsInformationResponse getUserDetailsInformation(User user) {
User findUser = getUserByIdentifier(user.getIdentifier());
User findUser = userService.findUserByIdentifier(user.getIdentifier());

int participantCount = 0;
List<Participant> participantInfoList = findUser.getParticipantList();

Expand All @@ -76,49 +74,39 @@ public UserDetailsInformationResponse getUserDetailsInformation(User user) {
return UserDetailsInformationResponse.createByEntity(findUser, participantCount, fileResponse);
}

// 마이페이지 - 사용자 정보 수정
@Transactional
@Override
public Long updateUserInformation(User user, UserInformationUpdateRequest userInformationUpdateRequest) {
User findUser = getUserByIdentifier(user.getIdentifier());
User findUser = userService.findUserByIdentifier(user.getIdentifier());
findUser.updateUserInformation(
userInformationUpdateRequest.getNickname(),
userInformationUpdateRequest.getInformation());

User updatedUser = userRepository.save(findUser);
return updatedUser.getId();
return userService.save(findUser);
}

// 마이페이지 - 회원 탈퇴
@Transactional
@Override
public void deleteUserInformation(User user, String reason) {
User findUser = getUserByIdentifier(user.getIdentifier());
User findUser = userService.findUserByIdentifier(user.getIdentifier());

filesService.deleteFile(findUser.getFiles());
findUser.setFiles(null);

findUser.deleteLikesList();
userRepository.deleteById(findUser.getId());
signoutRepository.save(
Signout.builder()
.identifier(user.getIdentifier())
.reason(reason)
.build()
);

userService.delete(findUser.getId(), user.getIdentifier(), reason);
}

// 마이페이지 - 관심사 수정
@Transactional
@Override
public void updateUserTags(User user, UserInterestUpdateRequest userInterestUpdateRequest) {
if (userInterestUpdateRequest.getTags() == null) {
throw new BusinessException();
}
User findUser = getUserByIdentifier(user.getIdentifier());
User findUser = userService.findUserByIdentifier(user.getIdentifier());
String interest = String.join(",", userInterestUpdateRequest.getTags());
findUser.updateUserTags(interest);
userRepository.save(findUser);
userService.save(findUser);
}

// 관심사 조회
@Override
public UserInterestResponse getUserInterest(User user) {
String tags = user.getTags();
String[] tagsList = tags.split(",");
Expand All @@ -131,9 +119,9 @@ public UserInterestResponse getUserInterest(User user) {
.build();
}

// 마이페이지 - 챌린지 현황
@Override
public UserChallengeResultResponse getUserChallengeResult(User user) {
User findUser = getUserByIdentifier(user.getIdentifier());
User findUser = userService.findUserByIdentifier(user.getIdentifier());
HashMap<JoinResult, List<Long>> participantHashMap = new HashMap<>() {
{
put(READY, new ArrayList<>());
Expand Down Expand Up @@ -166,14 +154,4 @@ public UserChallengeResultResponse getUserChallengeResult(User user) {
.processing(processing)
.build();
}

private User getUserByIdentifier(String identifier) {
return userRepository.findByIdentifier(identifier)
.orElseThrow(() -> new BusinessException(ErrorCode.MEMBER_NOT_FOUND));
}

private User getUserById(Long userId) {
return userRepository.findById(userId)
.orElseThrow(() -> new BusinessException(ErrorCode.MEMBER_NOT_FOUND));
}
}
Loading

0 comments on commit 8eb7648

Please sign in to comment.