-
Notifications
You must be signed in to change notification settings - Fork 0
[Feat] : Chapter 7 API 응답 통일 & 예외 처리 #8
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
29 changes: 29 additions & 0 deletions
29
src/main/java/com/example/umc_9th/domain/review/converter/ReviewConverter.java
This file contains hidden or 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,29 @@ | ||
| package com.example.umc_9th.domain.review.converter; | ||
|
|
||
| import com.example.umc_9th.domain.review.dto.res.ReviewResponseDTO; | ||
| import com.example.umc_9th.domain.review.entity.Review; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class ReviewConverter { | ||
|
|
||
| public static ReviewResponseDTO toReviewDTO(Review review) { | ||
|
|
||
|
|
||
| return ReviewResponseDTO.builder() | ||
| .reviewId(review.getId()) | ||
| .memberName(review.getMember().getName()) | ||
| .storeName(review.getStore().getName()) | ||
| .rating(review.getRating()) | ||
| .content(review.getContent()) | ||
| .build(); | ||
| } | ||
|
|
||
| // Review 엔티티 리스트르르 DTO리스트로 변환 | ||
| public static List<ReviewResponseDTO> toReviewDTOList(List<Review> reviewList) { | ||
| return reviewList.stream() | ||
| .map(ReviewConverter::toReviewDTO) | ||
| .collect(Collectors.toList()); | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/example/umc_9th/domain/review/dto/res/ReviewResponseDTO.java
This file contains hidden or 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,32 @@ | ||
| package com.example.umc_9th.domain.review.dto.res; | ||
|
|
||
| import com.example.umc_9th.grobal.BaseEntity; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
|
|
||
|
|
||
| @Getter | ||
| @Builder | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class ReviewResponseDTO extends BaseEntity { | ||
|
|
||
| private Long reviewId; | ||
|
|
||
| private String memberName; | ||
|
|
||
| private String storeName; | ||
|
|
||
| private Integer rating; | ||
|
|
||
|
|
||
| private String content; | ||
|
|
||
|
|
||
|
|
||
| } |
This file contains hidden or 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
10 changes: 10 additions & 0 deletions
10
src/main/java/com/example/umc_9th/domain/review/exception/ReviewException.java
This file contains hidden or 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,10 @@ | ||
| package com.example.umc_9th.domain.review.exception; | ||
|
|
||
| import com.example.umc_9th.grobal.apiPayload.code.BaseErrorCode; | ||
| import com.example.umc_9th.grobal.apiPayload.exception.GeneralException; | ||
|
|
||
| public class ReviewException extends GeneralException { | ||
| public ReviewException(BaseErrorCode code) { | ||
| super(code); | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/example/umc_9th/domain/review/exception/code/ReviewErrorCode.java
This file contains hidden or 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,38 @@ | ||
| package com.example.umc_9th.domain.review.exception.code; | ||
|
|
||
| import com.example.umc_9th.grobal.apiPayload.code.BaseErrorCode; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor | ||
| public enum ReviewErrorCode implements BaseErrorCode { | ||
|
|
||
| // 리뷰 검색 관련 에러 | ||
| // 회원 에러 | ||
| MEMBER_NOT_FOUND(HttpStatus.NOT_FOUND, "REVIEW404_1", "해당 ID의 회원을 찾을 수 없습니다."), | ||
| // 평점 유효성 | ||
| INVALID_RATING_VALUE(HttpStatus.BAD_REQUEST, "REVIEW400_1", "평점 값은 1에서 5 사이여야 합니다."); | ||
|
|
||
| private final HttpStatus status; | ||
| private final String code; | ||
| private final String message; | ||
|
|
||
|
|
||
| @Override | ||
| public HttpStatus getStatus() { | ||
| return this.status; | ||
| } | ||
|
|
||
| @Override | ||
| public String getCode() { | ||
| return this.code; | ||
| } | ||
|
|
||
| @Override | ||
| public String getMessage() { | ||
| return this.message; | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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
50 changes: 50 additions & 0 deletions
50
src/main/java/com/example/umc_9th/domain/test/controller/TestController.java
This file contains hidden or 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,50 @@ | ||
| package com.example.umc_9th.domain.test.controller; | ||
|
|
||
|
|
||
| import com.example.umc_9th.domain.test.converter.TestConverter; | ||
| import com.example.umc_9th.domain.test.dto.res.TestResDTO; | ||
| import com.example.umc_9th.domain.test.exception.TestException; | ||
| import com.example.umc_9th.domain.test.service.query.TestQueryService; | ||
| import com.example.umc_9th.grobal.apiPayload.ApiResponse; | ||
| import com.example.umc_9th.grobal.apiPayload.code.GeneralErrorCode; | ||
| import com.example.umc_9th.grobal.apiPayload.code.GeneralSuccessCode; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequiredArgsConstructor | ||
| @RequestMapping("/temp") | ||
| public class TestController { | ||
|
|
||
|
|
||
| private final TestQueryService testQueryService; | ||
|
|
||
|
|
||
| @GetMapping("/test") | ||
| public ApiResponse<TestResDTO.Testing> test() throws Exception { | ||
| // 응답 코드 정의 | ||
| GeneralSuccessCode code = GeneralSuccessCode.OK; | ||
|
|
||
| throw new TestException(GeneralErrorCode.BAD_REQUEST); | ||
|
|
||
| // return ApiResponse.success( | ||
| // code, | ||
| // TestConverter.toTestingDTO("This is Test!")); | ||
| // | ||
| } | ||
|
|
||
| // 예외 상황 | ||
| @GetMapping("/exception") | ||
| public ApiResponse<TestResDTO.Exception> exception( | ||
| @RequestParam Long flag | ||
| ) { | ||
| testQueryService.checkFlag(flag); | ||
|
|
||
| // 응답 코드 정의 | ||
| GeneralSuccessCode code = GeneralSuccessCode.OK; | ||
| return ApiResponse.success(code, TestConverter.toExceptionDTO("This is Test!")); | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/example/umc_9th/domain/test/converter/TestConverter.java
This file contains hidden or 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,26 @@ | ||
| package com.example.umc_9th.domain.test.converter; | ||
|
|
||
| import com.example.umc_9th.domain.test.dto.res.TestResDTO; | ||
|
|
||
| public class TestConverter { | ||
|
|
||
| // 객체 -> DTO | ||
| public static TestResDTO.Testing toTestingDTO( | ||
| String testing | ||
| ) { | ||
| return TestResDTO.Testing.builder() | ||
| .testing(testing) | ||
| .build(); | ||
| } | ||
|
|
||
|
|
||
| // 예외객체 -> DTO | ||
| public static TestResDTO.Exception toExceptionDTO( | ||
| String testing | ||
| ){ | ||
| return TestResDTO.Exception.builder() | ||
| .testString(testing) | ||
| .build(); | ||
| } | ||
|
|
||
| } |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/example/umc_9th/domain/test/dto/req/TestReqDTO.java
This file contains hidden or 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,4 @@ | ||
| package com.example.umc_9th.domain.test.dto.req; | ||
|
|
||
| public class TestReqDTO { | ||
| } |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/example/umc_9th/domain/test/dto/res/TestResDTO.java
This file contains hidden or 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,22 @@ | ||
| package com.example.umc_9th.domain.test.dto.res; | ||
|
|
||
|
|
||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
|
|
||
| //DTO 하나만 만드록 안에다 스태틱으로 여러개 | ||
| //RequestDTO는 보통 프론트 그대로 받기 떄문에 빌더 패턴 적용할 필요 없음 | ||
| public class TestResDTO { | ||
| //DTO 자체는 수많은 곳에서 사용이 될 수 있기에 static class로 만들면 매번 class 파일을 만들 필요 없음 | ||
| @Builder | ||
| @Getter | ||
| public static class Testing { | ||
| private String testing; | ||
| } | ||
|
|
||
| @Builder | ||
| @Getter | ||
| public static class Exception { | ||
| private String testString; | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/example/umc_9th/domain/test/exception/TestException.java
This file contains hidden or 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,10 @@ | ||
| package com.example.umc_9th.domain.test.exception; | ||
|
|
||
| import com.example.umc_9th.grobal.apiPayload.code.BaseErrorCode; | ||
| import com.example.umc_9th.grobal.apiPayload.exception.GeneralException; | ||
|
|
||
| public class TestException extends GeneralException { | ||
| public TestException(BaseErrorCode code) { | ||
| super(code); | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/example/umc_9th/domain/test/exception/code/TestErrorCode.java
This file contains hidden or 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,20 @@ | ||
| package com.example.umc_9th.domain.test.exception.code; | ||
|
|
||
| import com.example.umc_9th.grobal.apiPayload.code.BaseErrorCode; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor | ||
| public enum TestErrorCode implements BaseErrorCode { | ||
|
|
||
| // For test | ||
| TEST_EXCEPTION(HttpStatus.BAD_REQUEST, "TEST400_1", "이거는 테스트"), | ||
| ; | ||
|
|
||
| private final HttpStatus status; | ||
| private final String code; | ||
| private final String message; | ||
|
|
||
| } |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/example/umc_9th/domain/test/service/query/TestQueryService.java
This file contains hidden or 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,5 @@ | ||
| package com.example.umc_9th.domain.test.service.query; | ||
|
|
||
| public interface TestQueryService { | ||
| void checkFlag(Long flag); | ||
| } |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/example/umc_9th/domain/test/service/query/TestQueryServiceImpl.java
This file contains hidden or 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,20 @@ | ||
| package com.example.umc_9th.domain.test.service.query; | ||
|
|
||
| import com.example.umc_9th.domain.test.exception.TestException; | ||
| import com.example.umc_9th.domain.test.exception.code.TestErrorCode; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class TestQueryServiceImpl implements TestQueryService { | ||
|
|
||
|
|
||
| @Override | ||
| public void checkFlag(Long flag){ | ||
| if (flag == 1){ | ||
| throw new TestException(TestErrorCode.TEST_EXCEPTION); | ||
| } | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
null인 것과 페이지 유효성 검사는 나누는게 좋을듯 합니다.
이렇게 두면 null이어서 안되는건지, 값이 잘못된건지 구분하기 힘들어요