Skip to content

Commit

Permalink
[#3] feat : Validation 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
sangminee committed May 10, 2023
1 parent 6dd26db commit d51080c
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
Expand All @@ -30,4 +31,11 @@ protected CommonResult courseException(CourseException e){
ExceptionList exceptionList = e.getExceptionList();
return responseService.getFailResult(exceptionList.getCode(),exceptionList.getMessage());
}

@ExceptionHandler(MethodArgumentNotValidException.class)
protected CommonResult validException(MethodArgumentNotValidException e){
log.error("MethodArgumentNotValidException : " + e.getMessage());
return responseService.getFailResult(HttpStatus.BAD_REQUEST.value(), e.getFieldError().getDefaultMessage());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

Expand All @@ -37,7 +39,7 @@ public class CourseManageController {
@Parameter(name = "titlePhoto", description = "코스 기본 사진", required = true)
})
@PostMapping("/new-course")
public CommonResult createCourse(@RequestPart("courseCreateDto") CourseCreateDto courseCreateDto,
public CommonResult createCourse(@RequestPart("courseCreateDto") @Validated CourseCreateDto courseCreateDto,
@RequestPart(value="titlePhoto") MultipartFile titlePhoto) throws IOException {
return courseManageService.createCourse(courseCreateDto, titlePhoto);
}
Expand Down Expand Up @@ -106,7 +108,7 @@ public CommonResult requestCourse(@PathVariable Long course_index){
})
@PostMapping("/edit/{course_index}/add-curriculum")
public CommonResult addCurriculum(@PathVariable Long course_index,
@RequestBody CurriculumCreateDto curriculumCreateDto){
@RequestBody @Validated CurriculumCreateDto curriculumCreateDto){
return courseManageService.addCurriculum(course_index,curriculumCreateDto);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

Expand Down Expand Up @@ -46,7 +47,7 @@ public class LectureManageController {
public CommonResult createLecture(@PathVariable Long course_index,
@PathVariable int chapter,
@PathVariable int lecture_sequence,
@RequestPart("postLectureDto") LectureCreateDto lectureCreateDto,
@RequestPart("postLectureDto") @Validated LectureCreateDto lectureCreateDto,
@RequestPart("video") MultipartFile multipartFile) throws IOException {
return lectureManageService.createLecture(course_index,chapter,lecture_sequence, lectureCreateDto,multipartFile);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.mju.course.presentation.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.*;

import java.util.ArrayList;
Expand All @@ -12,16 +14,26 @@ public class CourseCreateDto {

// 코스
@Schema(description = "카테고리", defaultValue = "프로그래밍")
@NotNull @NotBlank
private String category;

@Schema(description = "코스 이름", defaultValue = "자바 기초")
@NotNull @NotBlank
private String courseName;

@Schema(description = "가격", defaultValue = "100000")
private String price;

@Schema(description = "코스 설명", defaultValue = "자바 기초 강의입니다.")
@NotNull @NotBlank
private String courseDescription;

@Schema(description = "난이도", defaultValue = "2")
@NotNull
private int difficulty;

@Schema(description = "스킬", defaultValue = "Java")
@NotNull @NotBlank
private String skill;

@Schema(description = "커리 큘럼")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.mju.course.presentation.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -12,9 +14,14 @@
@Builder
public class CurriculumCreateDto {
@Schema(description = "챕터", defaultValue = "1")
@NotNull
private int chapter;

@Schema(description = "챕터 제목", defaultValue = "자바란 무엇인가")
@NotNull @NotBlank
private String curriculumTitle;

@Schema(description = "강의 수", defaultValue = "3")
@NotNull
private int lectureSum;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.mju.course.presentation.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -8,6 +11,9 @@
@AllArgsConstructor
@NoArgsConstructor
public class LectureCreateDto {
@Schema(description = "강의 이름", defaultValue = "프로젝트 생성")
@NotNull @NotBlank
private String lectureTitle;

private String lectureDescription;
}

0 comments on commit d51080c

Please sign in to comment.