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

Refactor/#87/user space pay 관련 api customexception 적용 #94

18 changes: 8 additions & 10 deletions src/main/java/space/space_spring/controller/SpaceController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@
import space.space_spring.argumentResolver.jwtLogin.JwtLoginAuth;
import space.space_spring.dto.space.GetSpaceJoinDto;
import space.space_spring.dto.space.PostSpaceJoinDto;
import space.space_spring.dto.space.request.PostSpaceCreateDto;
import space.space_spring.dto.space.response.GetUserInfoBySpaceResponse;
import space.space_spring.dto.space.request.PostSpaceCreateRequest;

import space.space_spring.dto.userSpace.GetUserProfileInSpaceDto;
import space.space_spring.dto.userSpace.PutUserProfileInSpaceDto;
import space.space_spring.entity.UserSpace;
import space.space_spring.entity.Space;
import space.space_spring.exception.CustomException;
import space.space_spring.exception.MultipartFileException;
import space.space_spring.exception.SpaceException;
import space.space_spring.response.BaseResponse;
import space.space_spring.service.S3Uploader;
import space.space_spring.service.SpaceService;
Expand All @@ -42,18 +40,18 @@ public class SpaceController {
private final UserSpaceUtils userSpaceUtils;

@PostMapping("")
public BaseResponse<String> createSpace(@JwtLoginAuth Long userId, @Validated @ModelAttribute PostSpaceCreateRequest postSpaceCreateRequest, BindingResult bindingResult) throws IOException {
public BaseResponse<PostSpaceCreateDto.Response> createSpace(@JwtLoginAuth Long userId, @Validated @ModelAttribute PostSpaceCreateDto.Request postSpaceCreateRequest, BindingResult bindingResult) throws IOException {
if (bindingResult.hasErrors()) {
throw new SpaceException(INVALID_SPACE_CREATE, getErrorMessage(bindingResult));
throw new CustomException(INVALID_SPACE_CREATE, getErrorMessage(bindingResult));
}

// TODO 1. 스페이스 썸네일을 s3에 upload
String spaceImgUrl = processSpaceImage(postSpaceCreateRequest.getSpaceProfileImg());

// TODO 2. s3에 저장하고 받은 이미지 url 정보와 spaceName 정보로 space create 작업 수행
spaceService.createSpace(userId, postSpaceCreateRequest.getSpaceName(), spaceImgUrl);
Space createSpace = spaceService.createSpace(userId, postSpaceCreateRequest.getSpaceName(), spaceImgUrl);

return new BaseResponse<>("스페이스 생성 성공");
return new BaseResponse<>(new PostSpaceCreateDto.Response(createSpace.getSpaceId()));
}

private String processSpaceImage(MultipartFile spaceProfileImg) throws IOException {
Expand All @@ -67,7 +65,7 @@ private String processSpaceImage(MultipartFile spaceProfileImg) throws IOExcepti

private void validateImageFile(MultipartFile spaceProfileImg) {
if (!s3Uploader.isFileImage(spaceProfileImg)) {
throw new MultipartFileException(IS_NOT_IMAGE_FILE);
throw new CustomException(IS_NOT_IMAGE_FILE);
}
}

Expand Down Expand Up @@ -133,7 +131,7 @@ private void validateIsUserInSpace(Long userId, Long spaceId) {
@PutMapping("/{spaceId}/member-profile")
public BaseResponse<PutUserProfileInSpaceDto.Response> updateUserProfileInSpace(@JwtLoginAuth Long userId, @PathVariable Long spaceId, @Validated @ModelAttribute PutUserProfileInSpaceDto.Request request, BindingResult bindingResult) throws IOException {
if (bindingResult.hasErrors()) {
throw new SpaceException(INVALID_USER_SPACE_PROFILE, getErrorMessage(bindingResult));
throw new CustomException(INVALID_USER_SPACE_PROFILE, getErrorMessage(bindingResult));
}

// TODO 1. 유저가 스페이스에 가입되어 있는지 검증
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/space/space_spring/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
Expand All @@ -11,11 +12,13 @@
import space.space_spring.dto.user.request.PostUserLoginRequest;
import space.space_spring.dto.user.request.PostUserSignupRequest;
import space.space_spring.dto.user.response.GetSpaceInfoForUserResponse;
import space.space_spring.exception.UserException;
import space.space_spring.exception.CustomException;
import space.space_spring.response.BaseResponse;
import space.space_spring.service.UserService;
import space.space_spring.util.userSpace.UserSpaceUtils;

import java.util.stream.Collectors;

import static space.space_spring.response.status.BaseExceptionResponseStatus.*;
import static space.space_spring.util.bindingResult.BindingResultUtils.getErrorMessage;

Expand All @@ -34,7 +37,7 @@ public class UserController {
@PostMapping("/signup")
public BaseResponse<String> signup(@Validated @RequestBody PostUserSignupRequest postUserSignupRequest, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
throw new UserException(INVALID_USER_SIGNUP, getErrorMessage(bindingResult));
throw new CustomException(INVALID_USER_SIGNUP, getErrorMessage(bindingResult));
}

userService.signup(postUserSignupRequest);
Expand All @@ -48,7 +51,7 @@ public BaseResponse<String> signup(@Validated @RequestBody PostUserSignupRequest
@PostMapping("/login")
public BaseResponse<String> login(@Validated @RequestBody PostUserLoginRequest postUserLoginRequest, BindingResult bindingResult, HttpServletResponse response) {
if (bindingResult.hasErrors()) {
throw new UserException(INVALID_USER_LOGIN, getErrorMessage(bindingResult));
throw new CustomException(INVALID_USER_LOGIN, getErrorMessage(bindingResult));
}

String jwtLogin = userService.login(postUserLoginRequest);
Expand All @@ -75,8 +78,6 @@ public BaseResponse<GetSpaceInfoForUserResponse> showUserSpaceList(@JwtLoginAuth
@GetMapping("/profile")
public BaseResponse<GetUserProfileListDto.Response> showUserProfileList(@JwtLoginAuth Long userId) {

log.info("userId = {}", userId);

return new BaseResponse<>(userService.getUserProfileList(userId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ public class PayReceiveInfoDto {
private String payCreatorName;

private int requestAmount;

private String bankName;

private String bankAccountNum;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package space.space_spring.dto.space.request;

import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.validator.constraints.Length;
import org.springframework.web.multipart.MultipartFile;

public class PostSpaceCreateDto {

@Getter
@Setter
@NoArgsConstructor
public static class Request {
@Length(min = 1, max = 10, message = "이름은 10자이내의 문자열이어야 합니다.")
@NotBlank(message = "스페이스 이름은 공백일 수 없습니다.")
private String spaceName;

@Nullable
private MultipartFile spaceProfileImg; // 스페이스 프로필 이미지 (썸네일)
}

@Getter
@AllArgsConstructor
public static class Response {

private Long spaceId;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class PostUserSignupRequest {

@Pattern(
regexp = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,16}$",
message = "8~16글자의 영문 대/소문자, 숫자, 특수문자가 포함되어야 합니다."
message = "비밀번호에는 8~16글자의 영문 대/소문자, 숫자, 특수문자가 포함되어야 합니다."
)
@NotBlank
private String password;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.Getter;
import space.space_spring.response.status.ResponseStatus;

@Getter
public class CustomException extends RuntimeException{

Expand Down

This file was deleted.

21 changes: 0 additions & 21 deletions src/main/java/space/space_spring/exception/SpaceException.java

This file was deleted.

20 changes: 0 additions & 20 deletions src/main/java/space/space_spring/exception/UserException.java

This file was deleted.

20 changes: 0 additions & 20 deletions src/main/java/space/space_spring/exception/UserSpaceException.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class CustomExceptionControllerAdvice {
@ExceptionHandler(CustomException.class)
public ResponseEntity<BaseErrorResponse> handle_JwtBadRequestException(CustomException e) {
log.error("[handle_JwtBadRequestException]", e);
BaseErrorResponse errorResponse = new BaseErrorResponse(e.getExceptionStatus());
BaseErrorResponse errorResponse = new BaseErrorResponse(e.getExceptionStatus(), e.getMessage());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분 잘 찾아 주셨네요! 좋습니다

return ResponseEntity.status(e.getExceptionStatus().getStatus()).body(errorResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import space.space_spring.entity.User;
import space.space_spring.entity.UserSpace;
import space.space_spring.exception.CustomException;
import space.space_spring.exception.UserSpaceException;

import java.util.Map;
import java.util.Optional;
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/space/space_spring/service/PayService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import space.space_spring.dto.pay.response.GetRecentPayRequestBankInfoResponse;
import space.space_spring.dto.pay.response.PostPayCompleteResponse;
import space.space_spring.entity.*;
import space.space_spring.exception.UserSpaceException;
import space.space_spring.exception.CustomException;
import space.space_spring.util.space.SpaceUtils;
import space.space_spring.util.user.UserUtils;

Expand Down Expand Up @@ -107,7 +107,11 @@ private PayReceiveInfoDto createPayReceiveInfoDto(PayRequestTarget payRequestTar
String payCreatorName = payRequestTarget.getPayRequest().getPayCreateUser().getUserName(); // 리펙토링 필요
int requestAmount = payRequestTarget.getRequestAmount();

return new PayReceiveInfoDto(payRequestTarget.getPayRequestTargetId(), payCreatorName, requestAmount);
// 정산 생성자가 요청한 은행 정보도 response에 추가
String bankName = payRequestTarget.getPayRequest().getBankName();
String bankAccountNum = payRequestTarget.getPayRequest().getBankAccountNum();

return new PayReceiveInfoDto(payRequestTarget.getPayRequestTargetId(), payCreatorName, requestAmount, bankName, bankAccountNum);
}

@Transactional
Expand Down Expand Up @@ -186,7 +190,7 @@ private PayTargetInfoDto createPayTargetInfoDto(PayRequestTarget payRequestTarge
User userByUserId = userDao.findUserByUserId(targetUserId);

UserSpace userSpace = userSpaceDao.findUserSpaceByUserAndSpace(userByUserId, space)
.orElseThrow(() -> new UserSpaceException(USER_IS_NOT_IN_SPACE));
.orElseThrow(() -> new CustomException(USER_IS_NOT_IN_SPACE));

String userName = userSpace.getUserName();
String userProfileImg = userSpace.getUserProfileImg();
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/space/space_spring/service/SpaceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import space.space_spring.entity.User;
import space.space_spring.entity.UserSpace;
import space.space_spring.entity.enumStatus.UserSpaceAuth;
import space.space_spring.exception.UserSpaceException;
import space.space_spring.exception.CustomException;
import space.space_spring.util.space.SpaceUtils;
import space.space_spring.util.user.UserUtils;
import space.space_spring.util.userSpace.UserSpaceUtils;
Expand All @@ -38,7 +38,7 @@ public class SpaceService {
private final UserUtils userUtils;

@Transactional
public Long createSpace(Long userId, String spaceName, String spaceImgUrl) {
public Space createSpace(Long userId, String spaceName, String spaceImgUrl) {

// TODO 1. 스페이스 생성 정보 db insert
Space saveSpace = spaceDao.saveSpace(spaceName, spaceImgUrl);
Expand All @@ -47,7 +47,7 @@ public Long createSpace(Long userId, String spaceName, String spaceImgUrl) {
User manager = userDao.findUserByUserId(userId);
UserSpace userSpace = userSpaceDao.createUserSpace(manager, saveSpace, UserSpaceAuth.MANAGER);

return saveSpace.getSpaceId();
return saveSpace;
}

@Transactional
Expand Down Expand Up @@ -106,7 +106,7 @@ public GetUserProfileInSpaceDto.Response getUserProfileInSpace(Long userId, Long

// userSpaceUtils.isUserInSpace 메서드에서도 해당 에러를 던지기는 하지만
// 컴파일 에러의 방지를 위해 일단 이중으로 예외를 던지도록 구현했습니다
throw new UserSpaceException(USER_IS_NOT_IN_SPACE);
throw new CustomException(USER_IS_NOT_IN_SPACE);
}

@Transactional
Expand Down Expand Up @@ -134,7 +134,7 @@ public PutUserProfileInSpaceDto.Response changeUserProfileInSpace(Long userId, L

// userSpaceUtils.isUserInSpace 메서드에서도 해당 에러를 던지기는 하지만
// 컴파일 에러의 방지를 위해 일단 이중으로 예외를 던지도록 구현했습니다
throw new UserSpaceException(USER_IS_NOT_IN_SPACE);
throw new CustomException(USER_IS_NOT_IN_SPACE);

}

Expand Down
Loading
Loading