Skip to content

Commit

Permalink
feat: 공통된 클래스 추가 작업 (#4)
Browse files Browse the repository at this point in the history
* feat: Exception 관련 클래스 추가

* feat: Config 관련 클래스 추가

* feat: Entity 관련 클래스 추가
  • Loading branch information
ymkim97 authored Oct 27, 2023
1 parent 7306cca commit 0aae2cc
Show file tree
Hide file tree
Showing 13 changed files with 270 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/main/java/com/moabam/global/common/entity/BaseTimeEntity.java
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 src/main/java/com/moabam/global/common/util/DynamicQuery.java
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);
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/moabam/global/config/JpaConfig.java
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);
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/moabam/global/config/WebConfig.java
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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;
}
}
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);
}
}
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);
}
}
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 src/main/java/com/moabam/global/error/model/ErrorMessage.java
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 src/main/java/com/moabam/global/error/model/ErrorResponse.java
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
) {
}

0 comments on commit 0aae2cc

Please sign in to comment.