Skip to content

Commit

Permalink
feat: ��implement core module (#9)
Browse files Browse the repository at this point in the history
* style: simplify file path

* feat: add custom exception

* feat: add exception handler, response

* feat: add logback.yml

* feat: depend on core module

* feat: implement process of bootJar task

* chore: update by code review

* refactor: unify domain package path
  • Loading branch information
songyi00 authored Jan 25, 2024
1 parent 6c1fc88 commit ec79365
Show file tree
Hide file tree
Showing 33 changed files with 240 additions and 57 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ out/
### VS Code ###
.vscode/
.DS_Store

**/logs
29 changes: 29 additions & 0 deletions api-server/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.1'
id 'io.spring.dependency-management' version '1.1.4'
}

dependencies {
implementation(project(":core"))

implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
useJUnitPlatform()
}

String jarName = "api-server.jar";

tasks.named("bootJar") {
bootJar.getArchiveFileName().set(jarName);

bootJar.doLast(task -> {
copy(copySpec -> {
copySpec.from("build/libs/" + jarName);
copySpec.into("../build/libs");
});
});
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nexters.dividendapiserver;
package nexters.dividend.apiserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand Down
1 change: 1 addition & 0 deletions api-server/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.profiles.include=console-logging, file-logging
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nexters.dividendapiserver;
package nexters.dividend.apiserver;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
Expand Down
18 changes: 15 additions & 3 deletions dividend-batch/build.gradle → batch/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,26 @@ plugins {
id 'io.spring.dependency-management' version '1.1.4'
}

group = 'nexters'
version = '0.0.1-SNAPSHOT'

dependencies {
implementation(project(":core"))

implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
useJUnitPlatform()
}

String jarName = "batch.jar";

tasks.named("bootJar") {
bootJar.getArchiveFileName().set(jarName);

bootJar.doLast(task -> {
copy(copySpec -> {
copySpec.from("build/libs/" + jarName);
copySpec.into("../build/libs");
});
});
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nexters.dividendbatch;
package nexters.dividend.batch;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nexters.dividendbatch;
package nexters.dividend.batch;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
Expand Down
16 changes: 10 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
allprojects {
apply plugin: 'java'
group = 'nexters'
apply plugin: 'java'
group = 'nexters'

sourceCompatibility = '17'
sourceCompatibility = '17'

repositories {
mavenCentral()
}
repositories {
mavenCentral()
}
}

jar {
enabled = false
}
5 changes: 1 addition & 4 deletions dividend-core/build.gradle → core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ plugins {
id 'io.spring.dependency-management' version '1.1.4'
}

group = 'nexters'
version = '0.0.1-SNAPSHOT'

bootJar.enabled = false
jar.enabled = true

dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nexters.dividendcore;
package nexters.dividend.core;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package nexters.dividend.core.exception;

public record ErrorResponse(
int code,
String message
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package nexters.dividend.core.exception;

import nexters.dividend.core.exception.error.AlreadyExistsException;
import nexters.dividend.core.exception.error.BadRequestException;
import nexters.dividend.core.exception.error.NotFoundException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import java.util.NoSuchElementException;

@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
final MethodArgumentNotValidException ex,
final HttpHeaders headers,
final HttpStatusCode status,
final WebRequest request) {
logger.error("message", ex);

return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(new ErrorResponse(HttpStatus.BAD_REQUEST.value(), ex.getMessage()));
}

@Override
protected ResponseEntity<Object> handleHttpMessageNotReadable(
final HttpMessageNotReadableException ex,
final HttpHeaders headers,
final HttpStatusCode status,
final WebRequest request) {
logger.error("message", ex);

return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(new ErrorResponse(HttpStatus.BAD_REQUEST.value(), ex.getMessage()));
}

@Override
protected ResponseEntity<Object> handleMissingServletRequestParameter(
final MissingServletRequestParameterException ex,
final HttpHeaders headers,
final HttpStatusCode status,
final WebRequest request) {
logger.error("message", ex);

return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(new ErrorResponse(HttpStatus.BAD_REQUEST.value(), ex.getMessage()));
}

@ExceptionHandler({IllegalArgumentException.class, BadRequestException.class})
public ResponseEntity<ErrorResponse> handleBadRequestException(final RuntimeException exception) {
logger.error("message", exception);

return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(new ErrorResponse(HttpStatus.BAD_REQUEST.value(), exception.getMessage()));
}

@ExceptionHandler({NoSuchElementException.class, NotFoundException.class})
public ResponseEntity<ErrorResponse> handleNotFoundException(final RuntimeException exception) {
logger.error("message", exception);

return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(new ErrorResponse(HttpStatus.NOT_FOUND.value(), exception.getMessage()));
}

@ExceptionHandler(AlreadyExistsException.class)
public ResponseEntity<ErrorResponse> handleDuplicateException(final AlreadyExistsException exception) {
logger.error("message", exception);

return ResponseEntity.status(HttpStatus.CONFLICT)
.body(new ErrorResponse(HttpStatus.CONFLICT.value(), exception.getMessage()));
}

@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleException(final Exception exception) {
logger.error("message", exception);

return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), exception.getMessage()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package nexters.dividend.core.exception.error;

public class AlreadyExistsException extends BaseException {
public AlreadyExistsException(final String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package nexters.dividend.core.exception.error;

public class BadRequestException extends BaseException {
public BadRequestException(final String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package nexters.dividend.core.exception.error;

public class BaseException extends RuntimeException {
private final String message;

public BaseException(final String message) {
this.message = message;
}

@Override
public String getMessage() {
return message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package nexters.dividend.core.exception.error;

public class NotFoundException extends BaseException {
public NotFoundException(final String message) {
super(message);
}
}
29 changes: 29 additions & 0 deletions core/src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<configuration>
<springProfile name="console-logging">
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
</springProfile>

<springProfile name="file-logging">
<property name="LOG_PATH" value="./logs"/>
<property name="LOG_FILE_NAME" value="dividend"/>

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/${LOG_FILE_NAME}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/%d{yyyy-MM-dd}-info-%i.log</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%level] [%thread] [%logger{36}] - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="FILE"/>
</root>
</springProfile>
</configuration>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nexters.dividendcore;
package nexters.dividend.core;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
Expand Down
17 changes: 0 additions & 17 deletions dividend-api-server/build.gradle

This file was deleted.

1 change: 0 additions & 1 deletion dividend-batch/src/main/resources/application.properties

This file was deleted.

1 change: 0 additions & 1 deletion dividend-core/src/main/resources/application.properties

This file was deleted.

3 changes: 0 additions & 3 deletions domain/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ plugins {
id 'io.spring.dependency-management' version '1.1.4'
}

group = 'nexters'
version = '0.0.1-SNAPSHOT'

repositories {
mavenCentral()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nexters.domain.base;
package nexters.dividend.domain;

import jakarta.persistence.*;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nexters.domain;
package nexters.dividend.domain;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package nexters.domain.dividend;
package nexters.dividend.domain.dividend;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import nexters.domain.base.BaseEntity;
import nexters.dividend.domain.BaseEntity;

import java.time.Instant;
import java.util.UUID;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package nexters.domain.dividend.repository;
package nexters.dividend.domain.dividend.repository;

import nexters.domain.dividend.Dividend;
import nexters.dividend.domain.dividend.Dividend;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.UUID;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nexters.domain.stock;
package nexters.dividend.domain.stock;

public enum Sector {
TECHNOLOGY("Technology"),
Expand Down
Loading

0 comments on commit ec79365

Please sign in to comment.