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/QFEED-67-Refactor-Structure-Exception #6

Merged
merged 7 commits into from
Nov 29, 2024
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.wsws.moduleapi.controller.user;
package com.wsws.moduleapi.auth.controller;

import com.wsws.moduleapi.dto.auth.*;
import com.wsws.moduleapplication.dto.user.AuthServiceResponse;
import com.wsws.moduleapplication.dto.auth.LoginServiceResponse;
import com.wsws.moduleapplication.service.user.AuthService;
import com.wsws.moduleapi.auth.dto.*;
import com.wsws.moduleapplication.auth.dto.LoginServiceResponse;
import com.wsws.moduleapplication.auth.service.AuthService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.wsws.moduleapi.dto.auth;
package com.wsws.moduleapi.auth.dto;

public record AuthResponse(String message) {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.wsws.moduleapi.dto.auth;
package com.wsws.moduleapi.auth.dto;

import com.wsws.moduleapplication.dto.auth.EmailVerificationCheckServiceDto;
import com.wsws.moduleapplication.auth.dto.EmailVerificationCheckServiceDto;

public record EmailVerificationCheckRequest(String email, String code) {
public EmailVerificationCheckServiceDto toServiceDto() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.wsws.moduleapi.dto.auth;
package com.wsws.moduleapi.auth.dto;

import com.wsws.moduleapplication.dto.auth.EmailVerificationServiceDto;
import com.wsws.moduleapplication.auth.dto.EmailVerificationServiceDto;

public record EmailVerificationRequest(String email) {
public EmailVerificationServiceDto toServiceDto() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.wsws.moduleapi.dto.auth;
package com.wsws.moduleapi.auth.dto;

import com.wsws.moduleapplication.dto.auth.LoginServiceRequest;
import com.wsws.moduleapplication.auth.dto.LoginServiceRequest;

public record LoginRequest(String email, String password) {
public LoginServiceRequest toServiceDto() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.wsws.moduleapi.dto.auth;
package com.wsws.moduleapi.auth.dto;

import com.wsws.moduleapplication.dto.auth.LoginServiceResponse;
import com.wsws.moduleapplication.auth.dto.LoginServiceResponse;

public record LoginResponse(String accessToken, String refreshToken) {
public LoginResponse(LoginServiceResponse response) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.wsws.moduleapi.dto.auth;
package com.wsws.moduleapi.auth.dto;

public record LogoutRequest(String refreshToken) {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.wsws.moduleapi.dto.auth;
package com.wsws.moduleapi.auth.dto;

import com.wsws.moduleapplication.dto.auth.PasswordResetConfirmServiceDto;
import com.wsws.moduleapplication.auth.dto.PasswordResetConfirmServiceDto;

public record PasswordResetConfirmRequest(String email, String code, String newPassword) {
public PasswordResetConfirmServiceDto toServiceDto() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.wsws.moduleapi.dto.auth;
package com.wsws.moduleapi.auth.dto;

import com.wsws.moduleapplication.dto.auth.PasswordResetRequestServiceDto;
import com.wsws.moduleapplication.auth.dto.PasswordResetRequestServiceDto;

public record PasswordResetRequest(String email) {
public PasswordResetRequestServiceDto toServiceDto() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.wsws.moduleapi.dto.auth;
package com.wsws.moduleapi.auth.dto;

public record TokenReissueRequest(String refreshToken) {
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.wsws.moduleapi.controller.follow;
package com.wsws.moduleapi.follow.controller;

import com.wsws.moduleapplication.dto.follow.FollowServiceRequestDto;
import com.wsws.moduleapplication.service.follow.FollowService;
import com.wsws.moduleapplication.follow.dto.FollowServiceRequestDto;
import com.wsws.moduleapplication.follow.service.FollowService;
import com.wsws.moduleinfra.repo.follow.FollowReadRepository;
import com.wsws.moduleinfra.repo.follow.dto.FollowResponseInfraDto;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.wsws.moduleapi.dto.follow;
package com.wsws.moduleapi.follow.dto;

import jakarta.validation.constraints.NotBlank;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.wsws.moduleapi.dto.follow;
package com.wsws.moduleapi.follow.dto;

public record FollowResponseDto(
String userId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.wsws.moduleapi.controller.user;
package com.wsws.moduleapi.user.controller;

import com.wsws.moduleapi.dto.auth.AuthResponse;
import com.wsws.moduleapi.dto.user.*;
import com.wsws.moduleapplication.dto.user.PasswordChangeServiceDto;
import com.wsws.moduleapplication.dto.user.RegisterUserRequest;
import com.wsws.moduleapplication.dto.user.UpdateProfileServiceDto;
import com.wsws.moduleapplication.service.user.UserQueryService;
import com.wsws.moduleapplication.service.user.UserService;
import com.wsws.moduleapi.auth.dto.AuthResponse;
import com.wsws.moduleapi.user.dto.UserProfileApiResponse;
import com.wsws.moduleapplication.user.dto.PasswordChangeServiceDto;
import com.wsws.moduleapplication.user.dto.RegisterUserRequest;
import com.wsws.moduleapplication.user.dto.UpdateProfileServiceDto;
import com.wsws.moduleapplication.user.service.UserQueryService;
import com.wsws.moduleapplication.user.service.UserService;
import com.wsws.modulesecurity.security.UserPrincipal;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
Expand All @@ -23,9 +24,9 @@ public class UserController {

// ํšŒ์›๊ฐ€์ž…
@PostMapping("/signup")
public ResponseEntity<Void> signup(@RequestBody RegisterUserRequest request) {
public ResponseEntity<AuthResponse> signup(@RequestBody RegisterUserRequest request) {
userService.register(request);
return ResponseEntity.ok().build();
return ResponseEntity.status(HttpStatus.CREATED).body(new AuthResponse("ํšŒ์›๊ฐ€์ž…์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค."));
}

// ์‚ฌ์šฉ์ž ์ •๋ณด ๊ฒ€์ƒ‰
Expand All @@ -45,20 +46,20 @@ public ResponseEntity<AuthResponse> changePassword(@AuthenticationPrincipal User

//์‚ฌ์šฉ์ž ์ •๋ณด ์—…๋ฐ์ดํŠธ
@PatchMapping
public ResponseEntity<Void> updateProfile(
public ResponseEntity<AuthResponse> updateProfile(
@AuthenticationPrincipal UserPrincipal userPrincipal,
@RequestBody UpdateProfileServiceDto dto) {
String userId = userPrincipal.getId();
userService.updateProfile(dto,userId);
return ResponseEntity.ok().build();
return ResponseEntity.ok(new AuthResponse("์‚ฌ์šฉ์ž ์ •๋ณด ์ˆ˜์ •์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค."));
}

// ํšŒ์› ์ •๋ณด ์‚ญ์ œ
@DeleteMapping
public ResponseEntity<AuthResponse> deleteUser(@AuthenticationPrincipal UserPrincipal userPrincipal) {
String userId = userPrincipal.getId();
userService.deleteUser(userId);
return ResponseEntity.ok(new AuthResponse("ํšŒ์› ์ •๋ณด๊ฐ€ ์‚ญ์ œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค."));
return ResponseEntity.ok(new AuthResponse("ํšŒ์› ํƒˆํ‡ด๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค."));
}

// ๊ณผ๊ฑฐ ์งˆ๋ฌธ ๋ฐ ๋‹ต๋ณ€ ๋ฆฌ์ŠคํŠธ ์กฐํšŒ
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.wsws.moduleapi.dto.user;
package com.wsws.moduleapi.user.dto;

import com.wsws.moduleapplication.dto.user.UserProfileResponse;
import com.wsws.moduleapplication.user.dto.UserProfileResponse;

public record UserProfileApiResponse(
String userId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.wsws.moduleapi.dto.user;
package com.wsws.moduleapi.user.dto;

import java.util.List;

Expand Down
8 changes: 5 additions & 3 deletions module-api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
spring:
application:
name: module-api
config:
import:
- file:/Users/lee/final-project/WEB1_1_Q-Feed_BE/module-infra/src/main/resources/application-infra.yml
- file:/Users/lee/final-project/WEB1_1_Q-Feed_BE/module-security/src/main/resources/application-security.yml

- file:${ABSOLUTE_PATH}/module-infra/src/main/resources/application-infra.yml
- file:${ABSOLUTE_PATH}/module-security/src/main/resources/application-security.yml
- file:${ABSOLUTE_PATH}/module-external-api/src/main/resources/application-external-api.yml


Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.wsws.moduleapplication.dto.auth;
package com.wsws.moduleapplication.auth.dto;

public record EmailVerificationCheckServiceDto(String email, String code) {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.wsws.moduleapplication.dto.auth;
package com.wsws.moduleapplication.auth.dto;

public record EmailVerificationServiceDto(String email) {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.wsws.moduleapplication.dto.auth;
package com.wsws.moduleapplication.auth.dto;

public record LoginServiceRequest(
String email,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.wsws.moduleapplication.dto.auth;
package com.wsws.moduleapplication.auth.dto;

public record LoginServiceResponse(
String accessToken,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.wsws.moduleapplication.dto.auth;
package com.wsws.moduleapplication.auth.dto;

public record PasswordResetConfirmServiceDto(String email, String code, String newPassword) {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.wsws.moduleapplication.dto.auth;
package com.wsws.moduleapplication.auth.dto;

public record PasswordResetRequestServiceDto(String email) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.wsws.moduleapplication.auth.exception;


import com.wsws.moduleapi.exception.BaseErrorCode;
import com.wsws.moduleapi.exception.ErrorInfo;
import lombok.RequiredArgsConstructor;

import static com.wsws.moduleapi.constants.ErrorCodeConstants.UNAUTHORIZED;

@RequiredArgsConstructor
public enum AuthServiceErrorCode implements BaseErrorCode {

EMAIL_NOT_FOUND(UNAUTHORIZED, "AUTH_401_1", "์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ด๋ฉ”์ผ์ž…๋‹ˆ๋‹ค."),
PASSWORD_MISMATCH(UNAUTHORIZED, "AUTH_401_2", "๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."),
REFRESH_TOKEN_NOT_FOUND(UNAUTHORIZED, "AUTH_401_3", "์œ ํšจํ•˜์ง€ ์•Š์€ Refresh Token์ž…๋‹ˆ๋‹ค."),
REFRESH_TOKEN_EXPIRED(UNAUTHORIZED, "AUTH_401_4", "Refresh Token์ด ๋งŒ๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");

private final Integer status;
private final String errorCode;
private final String message;

@Override
public ErrorInfo getErrorInfo() {
return ErrorInfo.of(status, errorCode, message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.wsws.moduleapplication.auth.exception;

import com.wsws.moduleapi.exception.ApplicationException;

public class EmailNotFoundException extends ApplicationException {
public static final EmailNotFoundException EXCEPTION = new EmailNotFoundException();

private EmailNotFoundException() {
super(AuthServiceErrorCode.EMAIL_NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.wsws.moduleapplication.auth.exception;

import com.wsws.moduleapi.exception.ApplicationException;

public class InvalidRefreshTokenException extends ApplicationException {
public static final InvalidRefreshTokenException EXCEPTION = new InvalidRefreshTokenException();

private InvalidRefreshTokenException() {
super(AuthServiceErrorCode.REFRESH_TOKEN_NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.wsws.moduleapplication.auth.exception;

import com.wsws.moduleapi.exception.ApplicationException;

public class PasswordMismatchException extends ApplicationException {
public static final PasswordMismatchException EXCEPTION = new PasswordMismatchException();

private PasswordMismatchException() {
super(AuthServiceErrorCode.PASSWORD_MISMATCH);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.wsws.moduleapplication.auth.exception;


import com.wsws.moduleapi.exception.ApplicationException;

public class RefreshTokenExpiredException extends ApplicationException {
public static final RefreshTokenExpiredException EXCEPTION = new RefreshTokenExpiredException();

private RefreshTokenExpiredException() {
super(AuthServiceErrorCode.REFRESH_TOKEN_EXPIRED);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.wsws.moduleapplication.service.user;
package com.wsws.moduleapplication.auth.service;

import com.wsws.moduleapplication.dto.user.AuthServiceResponse;
import com.wsws.moduleapplication.dto.auth.LoginServiceRequest;
import com.wsws.moduleapplication.dto.auth.LoginServiceResponse;
import com.wsws.moduleapplication.auth.exception.EmailNotFoundException;
import com.wsws.moduleapplication.auth.exception.RefreshTokenExpiredException;
import com.wsws.moduleapplication.user.dto.AuthServiceResponse;
import com.wsws.moduleapplication.auth.dto.LoginServiceRequest;
import com.wsws.moduleapplication.auth.dto.LoginServiceResponse;
import com.wsws.moduledomain.auth.RefreshToken;
import com.wsws.moduledomain.auth.TokenProvider;
import com.wsws.moduledomain.auth.exception.InvalidRefreshTokenException;
Expand All @@ -27,43 +29,43 @@ public class AuthService {
private final TokenProvider tokenProvider;
private final PasswordEncoder passwordEncoder;

//๋กœ๊ทธ์ธ
// ๋กœ๊ทธ์ธ
public LoginServiceResponse login(LoginServiceRequest loginServiceRequest) {
Email email = Email.from(loginServiceRequest.email());
User user = userRepository.findByEmail(email)
.orElseThrow(() -> new IllegalArgumentException("์ด๋ฉ”์ผ ๋˜๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค."));
.orElseThrow(() -> EmailNotFoundException.EXCEPTION);

if(!passwordEncoder.matches(loginServiceRequest.password(),user.getPassword().getValue())){
throw new IllegalArgumentException("์ด๋ฉ”์ผ ๋˜๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
}
// ๋น„๋ฐ€๋ฒˆํ˜ธ ๊ฒ€์ฆ
user.getPassword().matches(loginServiceRequest.password(), passwordEncoder);

// JWT ํ† ํฐ ์ƒ์„ฑ
String accessToken = tokenProvider.createAccessToken(user.getId().getValue());
String refreshToken = tokenProvider.createRefreshToken(user.getId().getValue());

//refreshToken์€ 7์ผ
// RefreshToken ์ €์žฅ (7์ผ ์œ ํšจ)
authRepository.save(RefreshToken.create(refreshToken, LocalDateTime.now().plusDays(7)));

return new LoginServiceResponse(accessToken, refreshToken);
}


//๋กœ๊ทธ์•„์›ƒ
// ๋กœ๊ทธ์•„์›ƒ
public AuthServiceResponse logout(String refreshToken) {
authRepository.deleteByToken(refreshToken);
return new AuthServiceResponse("๋กœ๊ทธ์•„์›ƒ์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค");
}



//ํ† ํฐ ์žฌ๋ฐœ๊ธ‰
public LoginServiceResponse reissueToken(String refreshToken){
//token ์žˆ๋Š”์ง€ ํ™•์ธ
// ํ† ํฐ ์žฌ๋ฐœ๊ธ‰
public LoginServiceResponse reissueToken(String refreshToken) {
// RefreshToken ํ™•์ธ
RefreshToken rToken = authRepository.findByToken(refreshToken)
.orElseThrow(() -> new InvalidRefreshTokenException("์œ ํšจํ•˜์ง€ ์•Š์€ ๋ฆฌํ”„๋ ˆ์‹œ ํ† ํฐ์ž…๋‹ˆ๋‹ค."));
.orElseThrow(() -> InvalidRefreshTokenException.EXCEPTION);

//์œ ํšจ๊ธฐ๊ฐ„ ์ฒดํฌ
rToken.validateExpiry();
// ์œ ํšจ๊ธฐ๊ฐ„ ์ฒดํฌ
try {
rToken.validateExpiry();
} catch (Exception e) {
throw RefreshTokenExpiredException.EXCEPTION;
}

String newAccessToken = tokenProvider.createAccessToken(rToken.getToken());
String newRefreshToken = tokenProvider.createRefreshToken(rToken.getToken());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.wsws.moduleapplication.dto.follow;
package com.wsws.moduleapplication.follow.dto;


public record FollowServiceRequestDto(String followerId, String followeeId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.wsws.moduleapplication.dto.follow;
package com.wsws.moduleapplication.follow.dto;

public record FollowServiceResponseDto(String userId, String nickname, String profileImage) {
}
Loading