Skip to content

Commit

Permalink
Merge pull request #10 from Hous-Release/fix/#9
Browse files Browse the repository at this point in the history
SuccessResponse 변경에 따라 controller단 return value 수정 및 코드 수정
  • Loading branch information
hyejungg authored Aug 28, 2022
2 parents 5b57b32 + 9683412 commit 7c7eead
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 62 deletions.
9 changes: 4 additions & 5 deletions src/main/java/hous/server/common/dto/SuccessResponse.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package hous.server.common.dto;

import feign.Response;
import hous.server.common.success.SuccessCode;
import lombok.*;
import org.springframework.http.ResponseEntity;
Expand All @@ -9,15 +8,15 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class SuccessResponse {
public class SuccessResponse<T> {

private int status;
private boolean success;
private String message;
private Object data;
private T data;

public static ResponseEntity<SuccessResponse> success(SuccessCode successCode, Object data) {
return ResponseEntity.status(successCode.getStatus())
public static <T> ResponseEntity<T> success(SuccessCode successCode, T data) {
return (ResponseEntity<T>) ResponseEntity.status(successCode.getStatus())
.body(new SuccessResponse(successCode.getStatus(), true, successCode.getMessage(), data));
}
}
16 changes: 7 additions & 9 deletions src/main/java/hous/server/common/success/SuccessCode.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
package hous.server.common.success;

import static hous.server.common.success.SuccessStatusCode.CREATED;
import static hous.server.common.success.SuccessStatusCode.NO_CONTENT;
import static hous.server.common.success.SuccessStatusCode.OK;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import static hous.server.common.success.SuccessStatusCode.CREATED;
import static hous.server.common.success.SuccessStatusCode.OK;

@Getter
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public enum SuccessCode {

/**
* 200 OK
*/
SUCCESS(OK, "성공입니다."),

// 인증
LOGIN_SUCCESS(OK, "로그인 성공입니다."),
REISSUE_TOKEN_SUCCESS(OK, "토큰 갱신 성공입니다."),

// 온보딩
CHECK_ONBOARDING_SUCCESS(OK, "온보딩 등록여부 조회 성공입니다."),
SET_ONBOARDING_SUCCESS(OK, "온보딩 정보 등록 성공입니다."),


// 방
GET_ROOM_SUCCESS(OK, "참가중인 방 조회 성공입니다."),
JOIN_ROOM_SUCCESS(OK, "방 참여 성공입니다."),

/**
* 201 CREATED
*/

// 방
CREATE_ROOM_SUCCESS(CREATED, "방 생성 성공입니다."),

Expand All @@ -42,7 +40,7 @@ public enum SuccessCode {
/**
* 204 NO_CONTENT
*/
;
NO_CONTENT_SUCCESS(NO_CONTENT, "");

private final SuccessStatusCode statusCode;
private final String message;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/hous/server/common/util/JwtUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package hous.server.common.util;

import hous.server.config.security.JwtConstants;
import hous.server.service.auth.dto.response.TokenResponseDto;
import hous.server.service.auth.dto.response.TokenResponse;
import io.jsonwebtoken.*;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;
Expand Down Expand Up @@ -35,7 +35,7 @@ public JwtUtils(@Value("${jwt.secret}") String secretKey, RedisTemplate redisTem
this.secretKey = Keys.hmacShaKeyFor(keyBytes);
}

public TokenResponseDto createTokenInfo(Long userId) {
public TokenResponse createTokenInfo(Long userId) {

long now = (new Date()).getTime();
Date accessTokenExpiresIn = new Date(now + ACCESS_TOKEN_EXPIRE_TIME);
Expand All @@ -57,7 +57,7 @@ public TokenResponseDto createTokenInfo(Long userId) {
redisTemplate.opsForValue()
.set("RT:" + userId, refreshToken, REFRESH_TOKEN_EXPIRE_TIME, TimeUnit.MILLISECONDS);

return TokenResponseDto.of(accessToken, refreshToken);
return TokenResponse.of(accessToken, refreshToken);
}

public boolean validateToken(String token) {
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/hous/server/controller/auth/AuthController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import hous.server.service.auth.AuthServiceProvider;
import hous.server.service.auth.CreateTokenService;
import hous.server.service.auth.dto.request.TokenRequestDto;
import hous.server.service.auth.dto.response.TokenResponseDto;
import hous.server.service.auth.dto.response.TokenResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
Expand All @@ -18,12 +18,14 @@
import org.springframework.http.ResponseEntity;
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 javax.validation.Valid;

@Api(tags = "Auth")
@RestController
@RequestMapping("/v1/auth")
@RequiredArgsConstructor
public class AuthController {

Expand All @@ -48,12 +50,12 @@ public class AuthController {
@ApiResponse(code = 409, message = "이미 해당 계정으로 회원가입하셨습니다.\n로그인 해주세요.", response = ErrorResponse.class),
@ApiResponse(code = 500, message = "예상치 못한 서버 에러가 발생하였습니다.", response = ErrorResponse.class)
})
@PostMapping("/v1/auth/login")
public ResponseEntity<SuccessResponse> login(@Valid @RequestBody LoginRequestDto request) {
@PostMapping("/login")
public ResponseEntity<LoginResponse> login(@Valid @RequestBody LoginRequestDto request) {
AuthService authService = authServiceProvider.getAuthService(request.getSocialType());
Long userId = authService.login(request.toServiceDto());

TokenResponseDto tokenInfo = createTokenService.createTokenInfo(userId);
TokenResponse tokenInfo = createTokenService.createTokenInfo(userId);
return SuccessResponse.success(SuccessCode.LOGIN_SUCCESS, LoginResponse.of(userId, tokenInfo));
}

Expand All @@ -72,8 +74,8 @@ public ResponseEntity<SuccessResponse> login(@Valid @RequestBody LoginRequestDto
@ApiResponse(code = 401, message = "토큰이 만료되었습니다. 다시 로그인 해주세요.", response = ErrorResponse.class),
@ApiResponse(code = 500, message = "예상치 못한 서버 에러가 발생하였습니다.", response = ErrorResponse.class)
})
@PostMapping("/v1/auth/refresh")
public ResponseEntity<SuccessResponse> reissue(@Valid @RequestBody TokenRequestDto request) {
@PostMapping("/refresh")
public ResponseEntity<TokenResponse> reissue(@Valid @RequestBody TokenRequestDto request) {
return SuccessResponse.success(SuccessCode.REISSUE_TOKEN_SUCCESS, createTokenService.reissueToken(request));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package hous.server.controller.auth.dto.response;

import hous.server.service.auth.dto.response.TokenResponseDto;
import hous.server.service.auth.dto.response.TokenResponse;
import lombok.*;

@ToString
Expand All @@ -11,9 +11,9 @@
public class LoginResponse {

private Long userId;
private TokenResponseDto token;
private TokenResponse token;

public static LoginResponse of(Long userId, TokenResponseDto token) {
public static LoginResponse of(Long userId, TokenResponse token) {
return LoginResponse.builder()
.userId(userId)
.token(token)
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/hous/server/controller/room/RoomController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import hous.server.config.resolver.UserId;
import hous.server.service.room.RoomService;
import hous.server.service.room.dto.request.CreateRoomRequestDto;
import hous.server.service.room.dto.response.RoomInfoResponse;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
Expand All @@ -18,6 +19,7 @@

@Api(tags = "Room")
@RestController
@RequestMapping("/v1/room")
@RequiredArgsConstructor
public class RoomController {

Expand All @@ -42,8 +44,8 @@ public class RoomController {
})
@Auth
@ResponseStatus(HttpStatus.CREATED)
@PostMapping("/v1/room")
public ResponseEntity<SuccessResponse> createRoom(@Valid @RequestBody CreateRoomRequestDto request, @ApiIgnore @UserId Long userId) {
@PostMapping("")
public ResponseEntity<RoomInfoResponse> createRoom(@Valid @RequestBody CreateRoomRequestDto request, @ApiIgnore @UserId Long userId) {
return SuccessResponse.success(SuccessCode.CREATE_ROOM_SUCCESS, roomService.createRoom(request, userId));
}

Expand All @@ -64,10 +66,10 @@ public ResponseEntity<SuccessResponse> createRoom(@Valid @RequestBody CreateRoom
@ApiResponse(code = 500, message = "예상치 못한 서버 에러가 발생하였습니다.", response = ErrorResponse.class)
})
@Auth
@PostMapping("/v1/room/{roomId}/join")
public ResponseEntity<SuccessResponse> joinRoom(@ApiParam(name = "roomId", value = "참가할 room 의 id", required = true, example = "1")
@PathVariable Long roomId,
@ApiIgnore @UserId Long userId) {
@PostMapping("/{roomId}/join")
public ResponseEntity<RoomInfoResponse> joinRoom(@ApiParam(name = "roomId", value = "참가할 room 의 id", required = true, example = "1")
@PathVariable Long roomId,
@ApiIgnore @UserId Long userId) {
return SuccessResponse.success(SuccessCode.JOIN_ROOM_SUCCESS, roomService.joinRoom(roomId, userId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import hous.server.config.interceptor.Auth;
import hous.server.config.resolver.UserId;
import hous.server.service.room.RoomRetrieveService;
import hous.server.service.room.dto.response.GetRoomResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
Expand Down Expand Up @@ -36,7 +37,7 @@ public class RoomRetrieveController {
})
@Auth
@GetMapping("/v1/room")
public ResponseEntity<SuccessResponse> getRoom(@ApiIgnore @UserId Long userId) {
public ResponseEntity<GetRoomResponse> getRoom(@ApiIgnore @UserId Long userId) {
return SuccessResponse.success(SuccessCode.GET_ROOM_SUCCESS, roomRetrieveService.getRoom(userId));
}
}
35 changes: 19 additions & 16 deletions src/main/java/hous/server/controller/user/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

@Api(tags = "User")
@RequiredArgsConstructor
@RestController
Expand All @@ -30,26 +31,28 @@ public class UserController {
private final UserService userService;

@ApiOperation(
value = "[인증] 온보딩 페이지 - 나의 온보딩 정보를 설정합니다.",
notes = "닉네임, 생년월일을 설정합니다. 성공시 data로 null을 보냅니다."
value = "[인증] 온보딩 페이지 - 나의 온보딩 정보를 설정합니다.",
notes = "닉네임, 생년월일을 설정합니다. 성공시 status code = 204, 빈 response body를 보냅니다."
)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "온보딩 정보 등록 성공입니다."),
@ApiResponse(
code = 400,
message = "1. 닉네임을 입력해주세요. (nickname)\n"
+ "2. 닉네임은 최대 5글자까지 가능합니다. (nickname)\n"
+ "3. 생년월일을 입력해주세요. (birthday)",
response = ErrorResponse.class),
@ApiResponse(code = 401, message = "토큰이 만료되었습니다. 다시 로그인 해주세요.", response = ErrorResponse.class),
@ApiResponse(code = 404, message = "탈퇴했거나 존재하지 않는 유저입니다.", response = ErrorResponse.class),
@ApiResponse(code = 500, message = "예상치 못한 서버 에러가 발생하였습니다.", response = ErrorResponse.class)
@ApiResponse(code = 204, message = ""),
@ApiResponse(
code = 400,
message = "1. 닉네임을 입력해주세요. (nickname)\n"
+ "2. 닉네임은 최대 5글자까지 가능합니다. (nickname)\n"
+ "3. 생년월일을 입력해주세요. (birthday)",
response = ErrorResponse.class),
@ApiResponse(code = 401, message = "토큰이 만료되었습니다. 다시 로그인 해주세요.", response = ErrorResponse.class),
@ApiResponse(code = 404, message = "탈퇴했거나 존재하지 않는 유저입니다.", response = ErrorResponse.class),
@ApiResponse(code = 500, message = "예상치 못한 서버 에러가 발생하였습니다.", response = ErrorResponse.class)
})
@ResponseStatus(HttpStatus.NO_CONTENT)
@Auth
@PostMapping("/onboarding")
public ResponseEntity<SuccessResponse> setOnboardingInfo(@Valid @RequestBody SetOnboardingInfoRequestDto request, @ApiIgnore @UserId Long userId) {
public ResponseEntity<SuccessResponse> setOnboardingInfo(
@Valid @RequestBody SetOnboardingInfoRequestDto request, @ApiIgnore @UserId Long userId) {
userService.setOnboardingInfo(request, userId);
return SuccessResponse.success(SuccessCode.SET_ONBOARDING_SUCCESS, null);
return SuccessResponse.success(SuccessCode.NO_CONTENT_SUCCESS, null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import hous.server.config.interceptor.Auth;
import hous.server.config.resolver.UserId;
import hous.server.service.user.UserRetrieveService;
import hous.server.service.user.dto.response.CheckOnboardingInfoResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
Expand Down Expand Up @@ -36,7 +37,7 @@ public class UserRetrieveController {
})
@Auth
@GetMapping("/onboarding/check")
public ResponseEntity<SuccessResponse> checkMyOnboardingInfo(@ApiIgnore @UserId Long userId) {
public ResponseEntity<CheckOnboardingInfoResponse> checkMyOnboardingInfo(@ApiIgnore @UserId Long userId) {
return SuccessResponse.success(SuccessCode.CHECK_ONBOARDING_SUCCESS, userRetrieveService.checkMyOnboardingInfo(userId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import hous.server.common.exception.UnAuthorizedException;
import hous.server.common.util.JwtUtils;
import hous.server.service.auth.dto.request.TokenRequestDto;
import hous.server.service.auth.dto.response.TokenResponseDto;
import hous.server.service.auth.dto.response.TokenResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
Expand All @@ -20,12 +20,12 @@ public class CreateTokenService {
private final RedisTemplate redisTemplate;

@Transactional
public TokenResponseDto createTokenInfo(Long userId) {
public TokenResponse createTokenInfo(Long userId) {
return jwtProvider.createTokenInfo(userId);
}

@Transactional
public TokenResponseDto reissueToken(TokenRequestDto request) {
public TokenResponse reissueToken(TokenRequestDto request) {
if (!jwtProvider.validateToken(request.getRefreshToken())) {
throw new UnAuthorizedException(String.format("주어진 리프레시 토큰 (%s) 이 유효하지 않습니다.", request.getRefreshToken()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder(access = AccessLevel.PRIVATE)
public class TokenResponseDto {
public class TokenResponse {

private String accessToken;
private String refreshToken;

public static TokenResponseDto of(String accessToken, String refreshToken) {
return TokenResponseDto.builder()
public static TokenResponse of(String accessToken, String refreshToken) {
return TokenResponse.builder()
.accessToken(accessToken)
.refreshToken(refreshToken)
.build();
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/hous/server/service/room/RoomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@

@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
@Transactional
public class RoomService {

private final UserRepository userRepository;
private final RoomRepository roomRepository;
private final ParticipateRepository participateRepository;

@Transactional
public RoomInfoResponse createRoom(CreateRoomRequestDto request, Long userId) {
User user = UserServiceUtils.findUserById(userRepository, userId);
Onboarding onboarding = user.getOnboarding();
Expand All @@ -35,7 +34,6 @@ public RoomInfoResponse createRoom(CreateRoomRequestDto request, Long userId) {
return RoomInfoResponse.of(room);
}

@Transactional
public RoomInfoResponse joinRoom(Long roomId, Long userId) {
User user = UserServiceUtils.findUserById(userRepository, userId);
Onboarding onboarding = user.getOnboarding();
Expand Down
Loading

0 comments on commit 7c7eead

Please sign in to comment.