-
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.
- Loading branch information
1 parent
a6ad7a5
commit 0ec3713
Showing
12 changed files
with
187 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.teamB.pcs.common.dto; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
import java.io.Serializable; | ||
|
||
public record ResponseDto<T>(T data) implements Serializable { | ||
public static <T> ResponseEntity<T> ok(T data) { | ||
return ResponseEntity.ok(data); | ||
} | ||
|
||
public static <T> ResponseEntity<T> created(T data) { | ||
return ResponseEntity | ||
.status(HttpStatus.CREATED) | ||
.body(data); | ||
} | ||
|
||
public static ResponseEntity<Void> noContent() { | ||
return ResponseEntity | ||
.status(HttpStatus.NO_CONTENT) | ||
.build(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.teamB.pcs.domain; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.teamB.pcs.error.ErrorCode; | ||
import com.teamB.pcs.error.exception.BusinessException; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
@RequiredArgsConstructor | ||
@Getter | ||
public enum Tag { | ||
STUDY, HEALTH, DRINK, OTHER; | ||
|
||
@JsonCreator | ||
public static List<Tag> create(List<String> requestValue) { | ||
List<Tag> tags = new ArrayList<>(); | ||
for (String r : requestValue) { | ||
Tag tag = Stream.of(values()) | ||
.filter(v -> v.toString().equalsIgnoreCase(r)) | ||
.findFirst() | ||
.orElseThrow(() -> new BusinessException(ErrorCode.INVALID_TAG)); | ||
tags.add(tag); | ||
} | ||
return tags; | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/teamB/pcs/dto/request/ArticleSaveRequestDto.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,18 @@ | ||
package com.teamB.pcs.dto.request; | ||
|
||
import com.teamB.pcs.domain.Article; | ||
import com.teamB.pcs.domain.Tag; | ||
|
||
import java.util.List; | ||
|
||
public record ArticleSaveRequestDto(String title, String description, String body, List<String> tags) { | ||
|
||
public Article toEntity(String title, String description, String body, List<Tag> tags) { | ||
return Article.builder() | ||
.title(title) | ||
.description(description) | ||
.body(body) | ||
.tags(tags) | ||
.build(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.teamB.pcs.error; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
|
||
import static org.springframework.http.HttpStatus.*; | ||
import static org.springframework.http.HttpStatus.NOT_FOUND; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum ErrorCode { | ||
|
||
INVALID_TAG(BAD_REQUEST, "태그를 찾을 수 없습니다"); | ||
|
||
|
||
private final int code; | ||
private final String message; | ||
|
||
ErrorCode(HttpStatus code, String message) { | ||
this.code = code.value(); | ||
this.message = message; | ||
} | ||
} |
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,8 @@ | ||
package com.teamB.pcs.error.dto; | ||
|
||
public record ErrorResponse(int code, String message) { | ||
public static ErrorResponse of(int code, String message){ | ||
return new ErrorResponse(code, message); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/teamB/pcs/error/exception/BusinessException.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,15 @@ | ||
package com.teamB.pcs.error.exception; | ||
|
||
import com.teamB.pcs.error.ErrorCode; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class BusinessException extends RuntimeException{ | ||
|
||
private final int code; | ||
|
||
public BusinessException(ErrorCode errorCode) { | ||
super(errorCode.getMessage()); | ||
this.code = errorCode.getCode(); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/com/teamB/pcs/error/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,41 @@ | ||
package com.teamB.pcs.error.exception; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.BindingResult; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import com.teamB.pcs.error.dto.ErrorResponse; | ||
import static org.springframework.http.HttpStatus.BAD_REQUEST; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler | ||
public ResponseEntity<ErrorResponse> handle(final Exception e) { | ||
log.error("Internal Error occurred", e); | ||
return ResponseEntity.internalServerError().body(ErrorResponse.of(500, e.getMessage())); | ||
} | ||
|
||
@ExceptionHandler | ||
public ResponseEntity<ErrorResponse> handle(final BusinessException e) { | ||
log.info("businessException: {}", e); | ||
return ResponseEntity.status(e.getCode()).body(ErrorResponse.of(e.getCode(), e.getMessage())); | ||
} | ||
|
||
@ExceptionHandler | ||
public ResponseEntity<ErrorResponse> handle(MethodArgumentNotValidException e) { | ||
BindingResult bindingResult = e.getBindingResult(); | ||
String firstErrorMessage = bindingResult.getFieldErrors().get(0).getDefaultMessage(); | ||
|
||
List<String> errorList = bindingResult.getFieldErrors().stream().map(err -> err.getDefaultMessage()).collect(Collectors.toList()); | ||
log.warn("MethodArgumentNotValidExceptionException = {}", errorList); | ||
|
||
return ResponseEntity.status(BAD_REQUEST).body(ErrorResponse.of(BAD_REQUEST.value(), firstErrorMessage)); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
spring: | ||
profiles: | ||
active: | ||
local | ||
group: | ||
local-env: | ||
- local | ||
|