Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

Commit

Permalink
#2 코드리뷰반영
Browse files Browse the repository at this point in the history
  • Loading branch information
jsy3831 committed Sep 27, 2021
1 parent dc6280c commit d4b7e9d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 92 deletions.
12 changes: 6 additions & 6 deletions src/main/java/com/photobook/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Response<UserDto> login(@RequestParam @NotBlank String id, @RequestParam

loginService.setLoginUserInfo(userInfo);

return Response.toResponse(200, "로그인 하였습니다.", userInfo);
return new Response("로그인 하였습니다.", userInfo);
}

@PostMapping("/logout")
Expand All @@ -58,7 +58,7 @@ public Response logout() {

loginService.removeLoginUserInfo();

return Response.toResponse(200, "로그아웃 하였습니다.");
return new Response("로그아웃 하였습니다.");
}

@GetMapping("/my-info")
Expand All @@ -67,7 +67,7 @@ public Response<UserDto> getUserInfoById(@LoginUser UserDto loginUser) {

UserDto userInfo = userService.getUserInfoById(loginUser.getId());

return Response.toResponse(200, "회원정보 조회", userInfo);
return new Response("회원정보 조회", userInfo);
}

@PostMapping("/signup")
Expand All @@ -76,15 +76,15 @@ public Response signup(@RequestBody @Valid UserDto userInfo) {

userService.createUser(userInfo);

return Response.toResponse(201, "회원가입 하였습니다.");
return new Response("회원가입 하였습니다.");
}

@GetMapping("/{id}")
public Response checkUserId(@PathVariable @NotBlank String id) {

userService.validateUserId(id);

return Response.toResponse(200, "사용가능한 아이디입니다.");
return new Response("사용가능한 아이디입니다.");
}

@DeleteMapping("/my-info")
Expand All @@ -95,7 +95,7 @@ public Response deleteUser(@LoginUser UserDto loginUser) {

loginService.removeLoginUserInfo();

return Response.toResponse(200, "회원탈퇴 하였습니다.");
return new Response("회원탈퇴 하였습니다.");
}

}
25 changes: 6 additions & 19 deletions src/main/java/com/photobook/controller/response/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,23 @@

import java.time.LocalDateTime;

import lombok.Builder;
import lombok.Getter;

@Getter
public class Response<T> {

private final LocalDateTime timestamp = LocalDateTime.now();

private final int code;
private String message;

private final String message;
private T body;

private final T body;

@Builder
Response(final int code, final String message, final T body) {
this.code = code;
public Response(String message) {
this.message = message;
this.body = body;
}

public static <T> Response<T> toResponse(int code, String message) {
return toResponse(code, message, null);
}

public static <T> Response<T> toResponse(int code, String message, T body) {
return Response.<T>builder()
.code(code)
.message(message)
.body(body)
.build();
public Response(String message, T body) {
this.message = message;
this.body = body;
}
}
14 changes: 7 additions & 7 deletions src/main/java/com/photobook/dto/UserDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ public class UserDto implements Serializable {

private int userId;

@NotBlank
@NotBlank(message = "아이디를 입력하세요.")
private String id;

@NotBlank
@NotBlank(message = "비밀번호를 입력하세요.")
private String password;

@NotBlank
@NotBlank(message = "이름을 입력하세요.")
private String name;

@NotBlank
@Email
@NotBlank(message = "이메일을 입력하세요.")
@Email(message = "이메일 형식이 올바르지 않습니다.")
private String email;

@NotNull
@Past
@NotNull(message = "생년월일을 입력하세요.")
@Past(message = "날짜가 올바르지 않습니다.")
private LocalDate birth;

private String profileImageName;
Expand Down
43 changes: 16 additions & 27 deletions src/main/java/com/photobook/exception/ExceptionController.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,67 +24,56 @@
@RestControllerAdvice
public class ExceptionController {

private static ExceptionType exceptionType(Exception e) {
return new ExceptionType(e.getClass().getSimpleName());
}

@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Response<ExceptionType> handleException(Exception e) {
public Response handleException(Exception e) {
log.error("handleException throw Exception : {}", e.getMessage());
return Response.toResponse(400, e.getMessage(), exceptionType(e));
return new Response(e.getMessage());
}

@ExceptionHandler(ConstraintViolationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Response<ExceptionType> handleConstraintViolationException(ConstraintViolationException e) {
public Response handleConstraintViolationException(ConstraintViolationException e) {
log.error("handleConstraintViolationException throw ConstraintViolationException : {}", e.getMessage());
return Response.toResponse(400, e.getMessage(), exceptionType(e));
return new Response(e.getMessage());
}

@ExceptionHandler(IllegalArgumentException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Response<ExceptionType> handleIllegalArgumentException(IllegalArgumentException e) {
public Response handleIllegalArgumentException(IllegalArgumentException e) {
log.error("handleIllegalArgumentException throw IllegalArgumentException : {}", e.getMessage());
return Response.toResponse(400, e.getMessage(), exceptionType(e));
return new Response(e.getMessage());
}

@ExceptionHandler(UnauthorizedException.class)
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public Response<ExceptionType> handleUnauthorizedException(UnauthorizedException e) {
return Response.toResponse(401, e.getMessage(), exceptionType(e));
public Response handleUnauthorizedException(UnauthorizedException e) {
return new Response(e.getMessage());
}

@ExceptionHandler(DuplicatedException.class)
@ResponseStatus(HttpStatus.CONFLICT)
public Response<ExceptionType> handleDuplicatedException(DuplicatedException e) {
return Response.toResponse(409, e.getMessage(), exceptionType(e));
public Response handleDuplicatedException(DuplicatedException e) {
return new Response(e.getMessage());
}

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Response<ExceptionType> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
public Response handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {

BindingResult bindingResult = e.getBindingResult();
List<StringBuilder> message = new ArrayList<>();
List<String> message = new ArrayList<>();

for (FieldError fieldError : bindingResult.getFieldErrors()) {
StringBuilder builder = new StringBuilder();

builder.append(fieldError.getField());
builder.append(" : ");
builder.append(fieldError.getDefaultMessage());

message.add(builder);
message.add(fieldError.getDefaultMessage());
}

return Response.toResponse(400, message.toString(), exceptionType(e));
return new Response(message.toString());
}

@ExceptionHandler(AuthenticationException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
public Response<ExceptionType> handleAuthenticationException(AuthenticationException e) {
return Response.toResponse(403, e.getMessage(), exceptionType(e));
public Response handleAuthenticationException(AuthenticationException e) {
return new Response(e.getMessage());
}

}
33 changes: 0 additions & 33 deletions src/test/java/com/photobook/utils/PasswordEncoderTest.java

This file was deleted.

0 comments on commit d4b7e9d

Please sign in to comment.