-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from Team-J9/feature/validate
DTO 검증 및 예외 처리
- Loading branch information
Showing
11 changed files
with
152 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/com/j9/bestmoments/constants/VideoFileTypes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.j9.bestmoments.constants; | ||
|
||
import java.util.Arrays; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum VideoFileTypes { | ||
MP4("video/mp4"), | ||
AVI("video/x-msvideo"), | ||
MKV("video/x-matroska"), | ||
WEBM("video/webm"), | ||
MOV("video/quicktime"), | ||
MPEG("video/mpeg"), | ||
FLV("video/x-flv"); | ||
|
||
private final String contentType; | ||
|
||
public static boolean contains(String contentType) { | ||
if (contentType == null) { | ||
return false; | ||
} | ||
String findingContentType = contentType.toLowerCase(); | ||
return Arrays.stream(VideoFileTypes.values()) | ||
.map(VideoFileTypes::getContentType) | ||
.anyMatch(type -> type.equals(findingContentType)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/com/j9/bestmoments/dto/request/MemberUpdateDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,21 @@ | ||
package com.j9.bestmoments.dto.request; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Size; | ||
|
||
public record MemberUpdateDto ( | ||
|
||
@NotNull | ||
@NotBlank | ||
@Size(max = 20, message = "이름은 20자 이내로 입력해주세요.") | ||
String name, | ||
|
||
@NotNull | ||
@NotBlank | ||
@Size(max = 1000, message = "자기소개는 1000자 이내로 입력해주세요.") | ||
String description | ||
|
||
) { | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/j9/bestmoments/dto/request/VideoCreateDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,30 @@ | ||
package com.j9.bestmoments.dto.request; | ||
|
||
import com.j9.bestmoments.domain.VideoStatus; | ||
import com.j9.bestmoments.validator.VideoTypeCheck; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Size; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
public record VideoCreateDto( | ||
|
||
@VideoTypeCheck | ||
MultipartFile file, | ||
|
||
@NotNull | ||
@NotBlank | ||
@Size(max = 100, message = "제목은 100자 이내로 입력해주세요.") | ||
String title, | ||
|
||
@NotNull | ||
@NotBlank | ||
@Size(max = 2000, message = "상세설명은 2000자 이내로 입력해주세요.") | ||
String description, | ||
|
||
@NotNull | ||
VideoStatus videoStatus | ||
|
||
) { | ||
|
||
} |
14 changes: 13 additions & 1 deletion
14
src/main/java/com/j9/bestmoments/dto/request/VideoUpdateDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/com/j9/bestmoments/exception/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.j9.bestmoments.exception; | ||
|
||
import jakarta.persistence.EntityNotFoundException; | ||
import java.util.stream.Collectors; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.support.DefaultMessageSourceResolvable; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.http.converter.HttpMessageNotReadableException; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
|
||
@ControllerAdvice | ||
@Slf4j | ||
public class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler(EntityNotFoundException.class) | ||
protected ResponseEntity<String> handleEntityNotFoundException(EntityNotFoundException e) { | ||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage()); | ||
} | ||
|
||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
protected ResponseEntity<String> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { | ||
String msg = e.getBindingResult() | ||
.getAllErrors() | ||
.stream() | ||
.map(DefaultMessageSourceResolvable::getDefaultMessage) | ||
.collect(Collectors.joining(", ")); | ||
log.info("handled MethodArgumentNotValidException : {}", msg); | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(msg); | ||
} | ||
|
||
// 잘못된 형식, 매핑 실패, 필드 누락 | ||
@ExceptionHandler(HttpMessageNotReadableException.class) | ||
protected ResponseEntity<String> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) { | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/j9/bestmoments/validator/VideoTypeCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.j9.bestmoments.validator; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Constraint(validatedBy = VideoTypeValidator.class) | ||
@Target(ElementType.FIELD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface VideoTypeCheck { | ||
String message() default "Invalid Video File Type"; | ||
Class<?>[] groups() default {}; | ||
Class<? extends Payload>[] payload() default {}; | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/j9/bestmoments/validator/VideoTypeValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.j9.bestmoments.validator; | ||
|
||
import com.j9.bestmoments.constants.VideoFileTypes; | ||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import jakarta.validation.constraints.NotNull; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
public class VideoTypeValidator implements ConstraintValidator<VideoTypeCheck, MultipartFile> { | ||
|
||
@Override | ||
public boolean isValid(@NotNull MultipartFile file, ConstraintValidatorContext context) { | ||
String contentType = file.getContentType(); | ||
return VideoFileTypes.contains(contentType); | ||
} | ||
} |