Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions common/build.gradle
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()
}
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 common/src/main/java/com/hubEleven/common/code/ErrorCode.java
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();
}
}
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 common/src/main/java/com/hubEleven/common/code/SuccessCode.java
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;
}
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;
}
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 common/src/main/java/com/hubEleven/common/model/BaseEntity.java
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;
}
}
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;
}
}
}
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
) {
}
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));
}
}
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 common/src/main/java/com/hubEleven/common/utils/PagingUtils.java
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));
}
}
1 change: 0 additions & 1 deletion common/src/main/resources/application.properties

This file was deleted.

3 changes: 3 additions & 0 deletions common/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spring:
application:
name: common
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@
@SpringBootTest
class CommonApplicationTests {

@Test
void contextLoads() {}
}
4 changes: 2 additions & 2 deletions config/src/main/resources/config-repo/user-service.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
spring:
datasource:
url: jdbc:mysql://localhost:3306/user_db?useSSL=false&serverTimezone=Asia/Seoul
username: root
password: "1234"
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
driver-class-name: com.mysql.cj.jdbc.Driver

jpa:
Expand Down
Loading