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

[BE] refactor: request DTO Validation 추가(#462) #463

Merged
merged 13 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.festago.auth.dto;

import jakarta.validation.constraints.NotBlank;

public record AdminLoginRequest(
@NotBlank(message = "username은 공백일 수 없습니다.")
String username,
@NotBlank(message = "password는 공백일 수 없습니다.")
String password
) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.festago.auth.dto;

import jakarta.validation.constraints.NotBlank;

public record AdminSignupRequest(
@NotBlank(message = "username은 공백일 수 없습니다.")
String username,
@NotBlank(message = "password는 공백일 수 없습니다.")
String password
) {

Expand Down
10 changes: 7 additions & 3 deletions backend/src/main/java/com/festago/auth/dto/LoginRequest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.festago.auth.dto;

import com.festago.auth.domain.SocialType;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;

public record LoginRequest(
@NotNull(message = "socialType 은 null 일 수 없습니다.") SocialType socialType,
@NotNull(message = "acessToken 은 null 일 수 없습니다.") String accessToken,
@NotNull(message = "fcmToken 은 null 일 수 없습니다.") String fcmToken) {
@NotNull(message = "socialType은 null 일 수 없습니다.")
SocialType socialType,
@NotBlank(message = "acessToken은 공백일 수 없습니다.")
String accessToken,
@NotBlank(message = "fcmToken은 공백일 수 없습니다.")
String fcmToken) {

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.festago.auth.dto;

import jakarta.validation.constraints.NotBlank;

public record RootAdminInitializeRequest(
@NotBlank(message = "password는 공백일 수 없습니다.")
String password
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import com.festago.common.exception.ErrorCode;
import com.festago.common.exception.FestaGoException;
import java.util.List;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;

public record ErrorResponse(
ErrorCode errorCode,
String message) {
String message
) {

private static final String NOT_CUSTOM_EXCPETION = "Validation failed";
private static final String NOT_CUSTOM_EXCEPTION = "Validation failed";

public static ErrorResponse from(FestaGoException festaGoException) {
return ErrorResponse.from(festaGoException.getErrorCode());
Expand All @@ -19,9 +22,13 @@ public static ErrorResponse from(ErrorCode errorCode) {
}

public static ErrorResponse from(ErrorCode errorCode, MethodArgumentNotValidException e) {
if (e.getMessage().startsWith(NOT_CUSTOM_EXCPETION)) {
List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
if (fieldErrors.isEmpty()) {
return new ErrorResponse(errorCode, errorCode.getMessage());
}
if (e.getMessage().startsWith(NOT_CUSTOM_EXCEPTION)) {
return new ErrorResponse(errorCode, fieldErrors.get(0).getDefaultMessage());
}
return new ErrorResponse(errorCode, e.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.festago.entry.dto;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.NotBlank;

public record TicketValidationRequest(
@NotNull(message = "code 는 null 일 수 없습니다.") String code) {
@NotBlank(message = "code는 공백일 수 없습니다.")
String code
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@

import com.festago.festival.domain.Festival;
import com.festago.school.domain.School;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.time.LocalDate;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;

public record FestivalCreateRequest(
@NotNull(message = "name 은 null 일 수 없습니다.") String name,
@DateTimeFormat(iso = ISO.DATE) LocalDate startDate,
@DateTimeFormat(iso = ISO.DATE) LocalDate endDate,
@NotBlank(message = "name은 공백일 수 없습니다.")
String name,
@NotNull(message = "startDate는 null 일 수 없습니다.")
@DateTimeFormat(iso = ISO.DATE)
LocalDate startDate,
@NotNull(message = "endDate는 null 일 수 없습니다.")
@DateTimeFormat(iso = ISO.DATE)
LocalDate endDate,
String thumbnail,
@NotNull(message = "schoolId는 null 일 수 없습니다.") Long schoolId
) {
@NotNull(message = "schoolId는 null 일 수 없습니다.")
Long schoolId) {

public Festival toEntity(School school) {
if (thumbnail == null || thumbnail.isBlank()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand All @@ -27,7 +28,7 @@ public class AuthController {

@PostMapping("/oauth2")
@Operation(description = "소셜 엑세스 토큰을 기반으로 로그인 요청을 보낸다.", summary = "OAuth2 로그인")
public ResponseEntity<LoginResponse> login(@RequestBody LoginRequest request) {
public ResponseEntity<LoginResponse> login(@RequestBody @Valid LoginRequest request) {
LoginResponse response = authFacadeService.login(request.socialType(), request.accessToken());
memberFCMService.saveMemberFCM(response.accessToken(), request.fcmToken());
return ResponseEntity.ok()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotV
HttpHeaders headers,
HttpStatusCode status, WebRequest request) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ErrorResponse.from(ErrorCode.INVALID_REQUEST_ARGUMENT));
.body(ErrorResponse.from(ErrorCode.INVALID_REQUEST_ARGUMENT, e));
}

private void logInfo(FestaGoException e, HttpServletRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class StudentController {
@PostMapping("/send-verification")
@Operation(description = "학교 인증 이메일을 전송한다.", summary = "학생 인증 이메일 전송")
public ResponseEntity<Void> sendEmail(@Member Long memberId,
@RequestBody StudentSendMailRequest request) {
@RequestBody @Valid StudentSendMailRequest request) {
studentService.sendVerificationMail(memberId, request);
return ResponseEntity.ok()
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.festago.school.dto;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.NotBlank;

public record SchoolCreateRequest(
@NotNull(message = "name 은 null 일 수 없습니다.") String name,
@NotNull(message = "domain 은 null 일 수 없습니다.") String domain
@NotBlank(message = "name은 공백일 수 없습니다.")
String name,
@NotBlank(message = "domain은 공백일 수 없습니다.")
String domain
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
import org.springframework.format.annotation.DateTimeFormat.ISO;

public record StageCreateRequest(
@DateTimeFormat(iso = ISO.DATE_TIME) LocalDateTime startTime,
@NotNull(message = "startTime은 null일 수 없습니다.")
@DateTimeFormat(iso = ISO.DATE_TIME)
LocalDateTime startTime,
String lineUp,
@DateTimeFormat(iso = ISO.DATE_TIME) LocalDateTime ticketOpenTime,
@NotNull(message = "festivalId 는 null 일 수 없습니다.") Long festivalId) {
@NotNull(message = "ticketOpenTime은 null일 수 없습니다.")
@DateTimeFormat(iso = ISO.DATE_TIME)
LocalDateTime ticketOpenTime,
@NotNull(message = "festivalId는 null 일 수 없습니다.")
Long festivalId
) {

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.festago.student.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;

public record StudentSendMailRequest(
@NotBlank(message = "username은 공백일 수 없습니다.")
String username,
@NotNull(message = "schoolId는 null 일 수 없습니다.")
Long schoolId
) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.festago.student.dto;

public record StudentVerificateRequest(String code) {
import jakarta.validation.constraints.NotBlank;

public record StudentVerificateRequest(
@NotBlank(message = "code는 공백일 수 없습니다.")
String code
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@
import org.springframework.format.annotation.DateTimeFormat.ISO;

public record TicketCreateRequest(
@NotNull(message = "stageId 는 null 일 수 없습니다.") Long stageId,
@NotNull(message = "ticketType 은 null 일 수 없습니다.") TicketType ticketType,
@NotNull(message = "amount 는 null 일 수 없습니다.") Integer amount,
@DateTimeFormat(iso = ISO.DATE_TIME) LocalDateTime entryTime) {
@NotNull(message = "stageId는 null 일 수 없습니다.")
Long stageId,
@NotNull(message = "ticketType은 null 일 수 없습니다.")
TicketType ticketType,
@NotNull(message = "amount는 null 일 수 없습니다.")
Integer amount,
@NotNull(message = "entryTime은 null 일 수 없습니다.")
@DateTimeFormat(iso = ISO.DATE_TIME)
LocalDateTime entryTime
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import jakarta.validation.constraints.NotNull;

public record TicketingRequest(
@NotNull(message = "ticketId 는 null 일 수 없습니다.") Long ticketId) {
@NotNull(message = "ticketId는 null 일 수 없습니다.")
Long ticketId
) {

}
Loading