-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Exception 관련 클래스 추가 * feat: Config 관련 클래스 추가 * feat: Entity 관련 클래스 추가
- Loading branch information
Showing
13 changed files
with
270 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
src/main/java/com/moabam/global/common/entity/BaseTimeEntity.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,29 @@ | ||
package com.moabam.global.common.entity; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import org.springframework.data.annotation.CreatedDate; | ||
import org.springframework.data.annotation.LastModifiedDate; | ||
import org.springframework.data.jpa.domain.support.AuditingEntityListener; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.EntityListeners; | ||
import jakarta.persistence.MappedSuperclass; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@MappedSuperclass | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@EntityListeners(AuditingEntityListener.class) | ||
public abstract class BaseTimeEntity { | ||
|
||
@CreatedDate | ||
@Column(name = "created_at", updatable = false, nullable = false) | ||
private LocalDateTime createdAt; | ||
|
||
@LastModifiedDate | ||
@Column(name = "updated_at") | ||
private LocalDateTime updatedAt; | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/moabam/global/common/util/DynamicQuery.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,37 @@ | ||
package com.moabam.global.common.util; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
|
||
import org.springframework.util.CollectionUtils; | ||
|
||
import com.querydsl.core.types.dsl.BooleanExpression; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class DynamicQuery { | ||
|
||
public static <T> BooleanExpression generateEq(T value, Function<T, BooleanExpression> function) { | ||
if (Objects.isNull(value)) { | ||
return null; | ||
} | ||
|
||
return function.apply(value); | ||
} | ||
|
||
public static <T> BooleanExpression filterCondition(T condition, Function<T, BooleanExpression> function) { | ||
T tempCondition = condition; | ||
|
||
if (tempCondition instanceof List<?> c && CollectionUtils.isEmpty(c)) { | ||
tempCondition = null; | ||
} | ||
|
||
return Optional.ofNullable(tempCondition) | ||
.map(function) | ||
.orElse(null); | ||
} | ||
} |
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,23 @@ | ||
package com.moabam.global.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing; | ||
|
||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
|
||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.PersistenceContext; | ||
|
||
@Configuration | ||
@EnableJpaAuditing | ||
public class JpaConfig { | ||
|
||
@PersistenceContext | ||
private EntityManager entityManager; | ||
|
||
@Bean | ||
public JPAQueryFactory jpaQueryFactory() { | ||
return new JPAQueryFactory(entityManager); | ||
} | ||
} |
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,23 @@ | ||
package com.moabam.global.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.CorsRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
public class WebConfig implements WebMvcConfigurer { | ||
|
||
private static final String ALLOWED_METHOD_NAMES = "GET,HEAD,POST,PUT,DELETE,TRACE,OPTIONS,PATCH"; | ||
private static final String ALLOW_ORIGIN_PATTERN = "[a-z]+\\.moabam.com"; | ||
private static final String ALLOW_LOCAL_HOST = "http://localhost:3000"; | ||
|
||
@Override | ||
public void addCorsMappings(final CorsRegistry registry) { | ||
registry.addMapping("/**") | ||
.allowedOriginPatterns(ALLOW_ORIGIN_PATTERN, ALLOW_LOCAL_HOST) | ||
.allowedMethods(ALLOWED_METHOD_NAMES.split(",")) | ||
.allowedHeaders("*") | ||
.allowCredentials(true) | ||
.maxAge(3600); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/moabam/global/error/exception/BadRequestException.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,10 @@ | ||
package com.moabam.global.error.exception; | ||
|
||
import com.moabam.global.error.model.ErrorMessage; | ||
|
||
public class BadRequestException extends MoabamException { | ||
|
||
public BadRequestException(ErrorMessage errorMessage) { | ||
super(errorMessage); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/moabam/global/error/exception/ConflictException.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,10 @@ | ||
package com.moabam.global.error.exception; | ||
|
||
import com.moabam.global.error.model.ErrorMessage; | ||
|
||
public class ConflictException extends MoabamException { | ||
|
||
public ConflictException(ErrorMessage errorMessage) { | ||
super(errorMessage); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/moabam/global/error/exception/ForbiddenException.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,10 @@ | ||
package com.moabam.global.error.exception; | ||
|
||
import com.moabam.global.error.model.ErrorMessage; | ||
|
||
public class ForbiddenException extends MoabamException { | ||
|
||
public ForbiddenException(ErrorMessage errorMessage) { | ||
super(errorMessage); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/moabam/global/error/exception/MoabamException.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,13 @@ | ||
package com.moabam.global.error.exception; | ||
|
||
import com.moabam.global.error.model.ErrorMessage; | ||
|
||
public class MoabamException extends RuntimeException { | ||
|
||
private final ErrorMessage errorMessage; | ||
|
||
public MoabamException(ErrorMessage errorMessage) { | ||
super(errorMessage.getMessage()); | ||
this.errorMessage = errorMessage; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/moabam/global/error/exception/NotFoundException.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,13 @@ | ||
package com.moabam.global.error.exception; | ||
|
||
import com.moabam.global.error.model.ErrorMessage; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class NotFoundException extends MoabamException { | ||
|
||
public NotFoundException(ErrorMessage errorMessage) { | ||
super(errorMessage); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/moabam/global/error/exception/UnauthorizedException.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,10 @@ | ||
package com.moabam.global.error.exception; | ||
|
||
import com.moabam.global.error.model.ErrorMessage; | ||
|
||
public class UnauthorizedException extends MoabamException { | ||
|
||
public UnauthorizedException(ErrorMessage errorMessage) { | ||
super(errorMessage); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/com/moabam/global/error/handler/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,68 @@ | ||
package com.moabam.global.error.handler; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.validation.FieldError; | ||
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; | ||
|
||
import com.moabam.global.error.exception.BadRequestException; | ||
import com.moabam.global.error.exception.ConflictException; | ||
import com.moabam.global.error.exception.ForbiddenException; | ||
import com.moabam.global.error.exception.MoabamException; | ||
import com.moabam.global.error.exception.NotFoundException; | ||
import com.moabam.global.error.exception.UnauthorizedException; | ||
import com.moabam.global.error.model.ErrorMessage; | ||
import com.moabam.global.error.model.ErrorResponse; | ||
|
||
@RestControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
@ResponseStatus(HttpStatus.NOT_FOUND) | ||
@ExceptionHandler(NotFoundException.class) | ||
protected ErrorResponse handleNotFoundException(MoabamException moabamException) { | ||
return new ErrorResponse(moabamException.getMessage(), null); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.UNAUTHORIZED) | ||
@ExceptionHandler(UnauthorizedException.class) | ||
protected ErrorResponse handleUnauthorizedException(MoabamException moabamException) { | ||
return new ErrorResponse(moabamException.getMessage(), null); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.NOT_FOUND) | ||
@ExceptionHandler(ForbiddenException.class) | ||
protected ErrorResponse handleForbiddenException(MoabamException moabamException) { | ||
return new ErrorResponse(moabamException.getMessage(), null); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.CONFLICT) | ||
@ExceptionHandler(ConflictException.class) | ||
protected ErrorResponse handleConflictException(MoabamException moabamException) { | ||
return new ErrorResponse(moabamException.getMessage(), null); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@ExceptionHandler(BadRequestException.class) | ||
protected ErrorResponse handleBadRequestException(MoabamException moabamException) { | ||
return new ErrorResponse(moabamException.getMessage(), null); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
public ErrorResponse handleMethodArgumentNotValidException(MethodArgumentNotValidException moabamException) { | ||
List<FieldError> fieldErrors = moabamException.getBindingResult().getFieldErrors(); | ||
Map<String, String> validation = new HashMap<>(); | ||
|
||
for (FieldError fieldError : fieldErrors) { | ||
validation.put(fieldError.getField(), fieldError.getDefaultMessage()); | ||
} | ||
|
||
return new ErrorResponse(ErrorMessage.INVALID_REQUEST_FIELD.getMessage(), validation); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/moabam/global/error/model/ErrorMessage.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,13 @@ | ||
package com.moabam.global.error.model; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum ErrorMessage { | ||
|
||
INVALID_REQUEST_FIELD("올바른 요청 정보가 아닙니다."); | ||
|
||
private final String message; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/moabam/global/error/model/ErrorResponse.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,11 @@ | ||
package com.moabam.global.error.model; | ||
|
||
import java.util.Map; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
|
||
public record ErrorResponse( | ||
String message, | ||
@JsonInclude(JsonInclude.Include.NON_EMPTY) Map<String, String> validation | ||
) { | ||
} |