Skip to content

Commit

Permalink
[BE] refactor: request DTO Validation ์ถ”๊ฐ€(#462) (#463)
Browse files Browse the repository at this point in the history
* feat: StudentSendMailRequest validation ์ถ”๊ฐ€

* feat: StudentVerificateRequest validation ์ถ”๊ฐ€

* refactor: FestivalCreateRequest NotNull/NotBlank ์ œ์•ฝ์กฐ๊ฑด ์ถ”๊ฐ€

* refactor: TicketValidationRequest NotNull -> NotBlank ๋กœ ๋ณ€๊ฒฝ

* refactor: LoginRequest NotNull -> NotBlank ๋กœ ๋ณ€๊ฒฝ

* feat: Request DTO Validation ์ถ”๊ฐ€

* refactor: ์˜ˆ์™ธ ๋ฉ”์‹œ์ง€ ์ผ๊ด„์ ์ด๊ฒŒ ๋ณ€๊ฒฝ

* style: ๊ฐœํ–‰ ์ผ๊ด„ ์ ์šฉ

* refactor: Controller์˜ Request Body @Valid ์ ์šฉ

* refactor: ์ƒ์ˆ˜ ์˜คํƒ€ ์ˆ˜์ •

* refactor: Validation Error ๋ฉ”์„ธ์ง€ ์ž์„ธํ•˜๊ฒŒ ์ˆ˜์ •

---------

Co-authored-by: seokjin8678 <[email protected]>
  • Loading branch information
2 people authored and BGuga committed Oct 17, 2023
1 parent 797d83f commit 5a4b092
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 28 deletions.
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
) {

}

0 comments on commit 5a4b092

Please sign in to comment.