-
Notifications
You must be signed in to change notification settings - Fork 4
[feat] 공통 유틸(common) 구현 #20
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
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c6b1f62
chore : 테스트 contextLoads 제거
Doritosch 7fc7b66
Merge branch 'dev' of https://github.com/ElevenHub/HubEleven into fea…
Doritosch 4f76f82
chore : .properties -> .yml
Doritosch f6f7a32
feat : BaseEntity 구현 및 @SoftDeletable 어노테이션 추가
Doritosch 01dcc65
feat : PagingUtils 및 공통 페이지 요청/응답 DTO 추가
Doritosch a2f6ff1
feat : 공통 응답 및 예외 처리 구현
Doritosch 54c22f5
chore : user-service.yml 환경 변수 설정
Doritosch 658a819
chore : common 모듈 JitPack 배포 설정 추가
Doritosch 11fae48
chore : common 모듈 JitPack 배포 설정 추가
Doritosch 380d6cb
Merge branch 'dev' of https://github.com/ElevenHub/HubEleven into fea…
Doritosch 1ed39f7
refactor : HttpStatus 코드 표기 일관화
Doritosch 3eaa4ef
chore : dependencies 버전 명시
Doritosch 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,50 +1,53 @@ | ||
| plugins { | ||
| id 'java' | ||
| id 'org.springframework.boot' version '3.5.7' | ||
| id 'java-library' | ||
| id 'maven-publish' | ||
| id 'io.spring.dependency-management' version '1.1.7' | ||
| } | ||
|
|
||
| group = 'com.hubEleven' | ||
| version = '0.0.1-SNAPSHOT' | ||
| description = 'Demo project for Spring Boot' | ||
| version = '1.0.0' | ||
| description = 'Common utility module' | ||
|
|
||
| java { | ||
| toolchain { | ||
| languageVersion = JavaLanguageVersion.of(17) | ||
| } | ||
| } | ||
|
|
||
| configurations { | ||
| compileOnly { | ||
| extendsFrom annotationProcessor | ||
| } | ||
| } | ||
|
|
||
| repositories { | ||
| mavenCentral() | ||
| } | ||
|
|
||
| ext { | ||
| set('springCloudVersion', "2025.0.0") | ||
| publishing { | ||
| publications { | ||
| register(MavenPublication) { | ||
| from components.java | ||
| groupId = 'com.github.ElevenHub' | ||
| artifactId = 'common' | ||
| version = '1.0.0' | ||
|
|
||
| pom { | ||
| name.set('HubEleven Common') | ||
| description.set('Common module for shared utilities') | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation 'org.springframework.boot:spring-boot-starter-data-jpa' | ||
| implementation 'org.springframework.boot:spring-boot-starter-web' | ||
| implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' | ||
| implementation 'org.springframework:spring-context:6.1.2' | ||
| implementation 'org.springframework:spring-web:6.1.2' | ||
| implementation 'org.springframework.data:spring-data-commons:3.3.3' | ||
| implementation 'org.springframework.data:spring-data-jpa:3.3.3' | ||
|
|
||
| implementation 'org.hibernate.orm:hibernate-core:6.6.3.Final' | ||
| implementation 'jakarta.persistence:jakarta.persistence-api:3.1.0' | ||
|
|
||
| compileOnly 'org.projectlombok:lombok' | ||
| runtimeOnly 'com.mysql:mysql-connector-j' | ||
| annotationProcessor 'org.projectlombok:lombok' | ||
| testImplementation 'org.springframework.boot:spring-boot-starter-test' | ||
| testRuntimeOnly 'org.junit.platform:junit-platform-launcher' | ||
| } | ||
|
|
||
| dependencyManagement { | ||
| imports { | ||
| mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" | ||
| } | ||
| } | ||
|
|
||
| tasks.named('test') { | ||
| useJUnitPlatform() | ||
| } |
14 changes: 14 additions & 0 deletions
14
common/src/main/java/com/hubEleven/common/annotation/SoftDeletable.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,14 @@ | ||
| package com.hubEleven.common.annotation; | ||
|
|
||
| import org.hibernate.annotations.SQLRestriction; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Target(ElementType.TYPE) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @SQLRestriction("deleted_at IS NULL") | ||
| public @interface SoftDeletable { | ||
| } |
21 changes: 21 additions & 0 deletions
21
common/src/main/java/com/hubEleven/common/code/ErrorCode.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,21 @@ | ||
| package com.hubEleven.common.code; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| import static org.springframework.http.HttpStatus.*; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @Getter | ||
| public enum ErrorCode implements StatusCode { | ||
| SERVER_ERROR(INTERNAL_SERVER_ERROR, "서버 에러가 발생하였습니다."); | ||
|
|
||
| private final HttpStatus httpStatus; | ||
| private final String message; | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return this.name(); | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
common/src/main/java/com/hubEleven/common/code/StatusCode.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,9 @@ | ||
| package com.hubEleven.common.code; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| public interface StatusCode { | ||
| HttpStatus getHttpStatus(); | ||
| String getMessage(); | ||
| String getName(); | ||
| } |
15 changes: 15 additions & 0 deletions
15
common/src/main/java/com/hubEleven/common/code/SuccessCode.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,15 @@ | ||
| package com.hubEleven.common.code; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| public enum SuccessCode { | ||
|
|
||
| SUCCESS(HttpStatus.OK, "성공했습니다."); | ||
|
|
||
| private final HttpStatus status; | ||
| private final String message; | ||
| } |
11 changes: 11 additions & 0 deletions
11
common/src/main/java/com/hubEleven/common/exception/GlobalException.java
hinoyat marked this conversation as resolved.
Show resolved
Hide resolved
|
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,11 @@ | ||
| package com.hubEleven.common.exception; | ||
|
|
||
| import com.hubEleven.common.code.StatusCode; | ||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| public class GlobalException extends RuntimeException { | ||
| private final StatusCode errorCode; | ||
| } |
24 changes: 24 additions & 0 deletions
24
common/src/main/java/com/hubEleven/common/exception/GlobalExceptionHandler.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,24 @@ | ||
| package com.hubEleven.common.exception; | ||
|
|
||
| import com.hubEleven.common.response.ApiResponse; | ||
| import com.hubEleven.common.response.ApiResponseEntity; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
| import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
| import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
|
||
| import static com.hubEleven.common.code.ErrorCode.*; | ||
|
|
||
| @RestControllerAdvice | ||
| public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { | ||
|
|
||
| @ExceptionHandler(GlobalException.class) | ||
| ResponseEntity<?> globalExceptionHandler(GlobalException e) { | ||
| return ApiResponseEntity.onFailure(e.getErrorCode()); | ||
| } | ||
|
|
||
| @ExceptionHandler(Exception.class) | ||
| public ResponseEntity<ApiResponse<Object>> handleException(final Exception e) { | ||
| return ApiResponseEntity.onFailure(SERVER_ERROR); | ||
| } | ||
| } |
48 changes: 48 additions & 0 deletions
48
common/src/main/java/com/hubEleven/common/model/BaseEntity.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,48 @@ | ||
| package com.hubEleven.common.model; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.EntityListeners; | ||
| import jakarta.persistence.MappedSuperclass; | ||
| import lombok.Getter; | ||
| import org.hibernate.annotations.Comment; | ||
| import org.hibernate.annotations.SQLRestriction; | ||
| import org.springframework.data.annotation.CreatedBy; | ||
| import org.springframework.data.annotation.CreatedDate; | ||
| import org.springframework.data.annotation.LastModifiedBy; | ||
| import org.springframework.data.annotation.LastModifiedDate; | ||
| import org.springframework.data.jpa.domain.support.AuditingEntityListener; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @Getter | ||
| @MappedSuperclass | ||
| @EntityListeners(AuditingEntityListener.class) | ||
| public abstract class BaseEntity { | ||
|
|
||
| @CreatedDate | ||
| @Column(updatable = false, nullable = false) | ||
| private LocalDateTime createdAt; | ||
|
|
||
| @CreatedBy | ||
| @Column(updatable = false) | ||
| private Long createdBy; | ||
|
|
||
| @LastModifiedDate | ||
| private LocalDateTime updatedAt; | ||
|
|
||
| @LastModifiedBy | ||
| private Long updatedBy; | ||
|
|
||
| private LocalDateTime deletedAt; | ||
|
|
||
| private Long deletedBy; | ||
|
|
||
| public void delete(Long deletedBy) { | ||
| this.deletedAt = LocalDateTime.now(); | ||
| this.deletedBy = deletedBy; | ||
| } | ||
|
|
||
| public boolean isDeleted() { | ||
| return this.deletedAt != null; | ||
| } | ||
| } |
50 changes: 50 additions & 0 deletions
50
common/src/main/java/com/hubEleven/common/request/CommonPageRequest.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.hubEleven.common.request; | ||
|
|
||
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.domain.Sort; | ||
|
|
||
| public record CommonPageRequest( | ||
| int page, | ||
| int size, | ||
| SortType sortType, | ||
| Sort.Direction direction, | ||
| String keyword | ||
| ) { | ||
| public CommonPageRequest { | ||
| if (page < 0) { | ||
| page = 0; | ||
| } | ||
|
|
||
| if (size != 10 && size != 30 && size != 50) { | ||
| size = 10; | ||
| } | ||
|
|
||
| if (sortType == null) { | ||
| sortType = SortType.CREATED_AT; | ||
| } | ||
|
|
||
| if (direction == null) { | ||
| direction = Sort.Direction.DESC; | ||
| } | ||
| } | ||
|
|
||
| public Pageable toPageable() { | ||
| return PageRequest.of(page, size, Sort.by(direction, sortType.getFieldName())); | ||
| } | ||
|
|
||
| public enum SortType { | ||
| CREATED_AT("createdAt"), | ||
| UPDATED_AT("updatedAt"); | ||
|
|
||
| private final String fieldName; | ||
|
|
||
| SortType(String fieldName) { | ||
| this.fieldName = fieldName; | ||
| } | ||
|
|
||
| public String getFieldName() { | ||
| return fieldName; | ||
| } | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
common/src/main/java/com/hubEleven/common/response/ApiResponse.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,12 @@ | ||
| package com.hubEleven.common.response; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonInclude; | ||
| import com.fasterxml.jackson.annotation.JsonPropertyOrder; | ||
|
|
||
| @JsonPropertyOrder({"code", "message", "result"}) | ||
| public record ApiResponse<T> ( | ||
| String code, | ||
| String message, | ||
| @JsonInclude(JsonInclude.Include.NON_NULL) T result | ||
| ) { | ||
| } |
39 changes: 39 additions & 0 deletions
39
common/src/main/java/com/hubEleven/common/response/ApiResponseEntity.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,39 @@ | ||
| package com.hubEleven.common.response; | ||
|
|
||
| import com.hubEleven.common.code.StatusCode; | ||
| import com.hubEleven.common.code.SuccessCode; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
|
|
||
| import java.net.URI; | ||
|
|
||
| public class ApiResponseEntity { | ||
|
|
||
| public static <T> ResponseEntity<ApiResponse<T>> success(T result) { | ||
| return ResponseEntity.ok().body(new ApiResponse<>(SuccessCode.SUCCESS.name(), SuccessCode.SUCCESS.getMessage(), result)); | ||
| } | ||
|
|
||
| public static <T> ResponseEntity<ApiResponse<T>> from(StatusCode code, T result) { | ||
| return ResponseEntity.status(code.getHttpStatus()) | ||
| .body(new ApiResponse<>(code.getName(), code.getMessage(), result)); | ||
| } | ||
|
|
||
| public static <T> ResponseEntity<ApiResponse<T>> create(StatusCode code, String url, T result) { | ||
| return ResponseEntity.created(URI.create(url)) | ||
| .body(new ApiResponse<>(code.getName(), code.getMessage(), result)); | ||
| } | ||
|
|
||
| public static <T> ResponseEntity<ApiResponse<T>> onFailure(StatusCode code) { | ||
| return ResponseEntity.status(code.getHttpStatus()) | ||
| .body(new ApiResponse<>(code.getName(), code.getMessage(), null)); | ||
| } | ||
|
|
||
| public static <T> ResponseEntity<ApiResponse<T>> badRequest(String message) { | ||
| return ResponseEntity.badRequest() | ||
| .body(new ApiResponse<>(HttpStatus.BAD_REQUEST.name(), message, null)); | ||
| } | ||
|
|
||
| public static <T> ResponseEntity<ApiResponse<T>> ok(String message) { | ||
| return ResponseEntity.ok().body(new ApiResponse<>(HttpStatus.OK.name(), message, null)); | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
common/src/main/java/com/hubEleven/common/response/CommonPageResponse.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,27 @@ | ||
| package com.hubEleven.common.response; | ||
|
|
||
| import org.springframework.data.domain.Page; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public record CommonPageResponse<T> ( | ||
| List<T> content, | ||
| int page, | ||
| int size, | ||
| long totalElements, | ||
| int totalPages, | ||
| boolean first, | ||
| boolean last | ||
| ) { | ||
| public static <T> CommonPageResponse<T> of(Page<T> page) { | ||
| return new CommonPageResponse<>( | ||
| page.getContent(), | ||
| page.getNumber(), | ||
| page.getSize(), | ||
| page.getTotalElements(), | ||
| page.getTotalPages(), | ||
| page.isFirst(), | ||
| page.isLast() | ||
| ); | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
common/src/main/java/com/hubEleven/common/utils/PagingUtils.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,13 @@ | ||
| package com.hubEleven.common.utils; | ||
|
|
||
| import com.hubEleven.common.response.CommonPageResponse; | ||
| import org.springframework.data.domain.Page; | ||
|
|
||
| import java.util.function.Function; | ||
|
|
||
| public class PagingUtils { | ||
|
|
||
| public static <E, D> CommonPageResponse<D> convert(Page<E> page, Function<E, D> mapper) { | ||
| return CommonPageResponse.of(page.map(mapper)); | ||
| } | ||
| } |
This file was deleted.
Oops, something went wrong.
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,3 @@ | ||
| spring: | ||
| application: | ||
| name: common |
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 |
|---|---|---|
|
|
@@ -6,6 +6,4 @@ | |
| @SpringBootTest | ||
| class CommonApplicationTests { | ||
|
|
||
| @Test | ||
| void contextLoads() {} | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.