diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..8af972c --- /dev/null +++ b/.gitattributes @@ -0,0 +1,3 @@ +/gradlew text eol=lf +*.bat text eol=crlf +*.jar binary diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 0000000..949493a --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,20 @@ +--- +name: Feature request +about: Suggest an idea for this project +title: '' +labels: '' +assignees: '' + +--- + +## ๐Ÿš€์ด์Šˆ ์ œ๋ชฉ + +## ๐Ÿ“์ด์Šˆ ์„ค๋ช… + +## โœจ์ž‘์—… ๋‚ด์šฉ +- [ ] ์ƒˆ๋กœ์šด ์ž‘์—… ๋‚ด์šฉ +- [ ] ๊ธฐ์กด ์ฝ”๋“œ ๋ณ€๊ฒฝ + +## โ˜‘๏ธTo-Do + +- [ ] diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..7224acf --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,18 @@ +## ๐Ÿš€ ๊ด€๋ จ ์ด์Šˆ +- + +## ๐Ÿ”‘ ์ฃผ์š” ๋ณ€๊ฒฝ์‚ฌํ•ญ + +- + +## โœ”๏ธ ์ฒดํฌ ๋ฆฌ์ŠคํŠธ +- [ ] Merge ํ•˜๋ ค๋Š” ๋ธŒ๋žœ์น˜๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ๊ฐ€? (`main` branch์— ์‹ค์ˆ˜๋กœ PR ์ƒ์„ฑ ๊ธˆ์ง€) +- [ ] ์ž‘์—…ํ•œ API์— ๋Œ€ํ•ด ์ ์ ˆํ•œ **์˜ˆ์™ธ์ฒ˜๋ฆฌ**๊ฐ€ ์ด๋ฃจ์–ด์กŒ๋Š”๊ฐ€? +- [ ] ์ž‘์—…ํ•œ API์— ๋Œ€ํ•ด ์ ์ ˆํ•œ **๋กœ๊ทธ ๋ฉ”์‹œ์ง€**๊ฐ€ ์ž‘์„ฑ๋˜์—ˆ๋Š”๊ฐ€? (`Controller`๋‚˜ `Service`์—์„œ `log.error` ํ™œ์šฉ) +- [ ] Merge ํ•˜๋ ค๋Š” PR ๋ฐ Commit๋“ค์„ **๋กœ์ปฌ**์—์„œ ์‹คํ–‰ํ–ˆ์„ ๋•Œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์•˜๋Š”๊ฐ€? + +## โ†—๏ธ ๊ฐœ์„  ์‚ฌํ•ญ +- + +## ๐Ÿ“” ์ฐธ๊ณ  ์ž๋ฃŒ +- ์„ ํƒ ์‚ฌํ•ญ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..18f2ce8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +HELP.md +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ +*.yml + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..67887f0 --- /dev/null +++ b/build.gradle @@ -0,0 +1,42 @@ +plugins { + id 'java' + id 'org.springframework.boot' version '3.4.2' + id 'io.spring.dependency-management' version '1.1.7' +} + +group = 'com.urung' +version = '0.0.1-SNAPSHOT' + +java { + toolchain { + languageVersion = JavaLanguageVersion.of(17) + } +} + +configurations { + compileOnly { + extendsFrom annotationProcessor + } +} + +repositories { + mavenCentral() +} + +dependencies { + implementation 'org.springframework.boot:spring-boot-starter-web' + //jpa + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + // lombok + compileOnly 'org.projectlombok:lombok' + annotationProcessor 'org.projectlombok:lombok' + // database + runtimeOnly 'mysql:mysql-connector-java:8.0.32' + // junit + testImplementation 'org.springframework.boot:spring-boot-starter-test' + testRuntimeOnly 'org.junit.platform:junit-platform-launcher' +} + +tasks.named('test') { + useJUnitPlatform() +} diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..a4b76b9 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..e18bc25 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.12.1-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..f5feea6 --- /dev/null +++ b/gradlew @@ -0,0 +1,252 @@ +#!/bin/sh + +# +# Copyright ยฉ 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions ยซ$varยป, ยซ${var}ยป, ยซ${var:-default}ยป, ยซ${var+SET}ยป, +# ยซ${var#prefix}ยป, ยซ${var%suffix}ยป, and ยซ$( cmd )ยป; +# * compound commands having a testable exit status, especially ยซcaseยป; +# * various built-in commands including ยซcommandยป, ยซsetยป, and ยซulimitยป. +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s +' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..9d21a21 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..f8a8f87 --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'ktb-be-dev-assignment' diff --git a/src/main/java/com/urung/ktbbedevassignment/KtbBeDevAssignmentApplication.java b/src/main/java/com/urung/ktbbedevassignment/KtbBeDevAssignmentApplication.java new file mode 100644 index 0000000..f5f75ca --- /dev/null +++ b/src/main/java/com/urung/ktbbedevassignment/KtbBeDevAssignmentApplication.java @@ -0,0 +1,13 @@ +package com.urung.ktbbedevassignment; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class KtbBeDevAssignmentApplication { + + public static void main(String[] args) { + SpringApplication.run(KtbBeDevAssignmentApplication.class, args); + } + +} diff --git a/src/main/java/com/urung/ktbbedevassignment/api/v1/domain/controller/StockController.java b/src/main/java/com/urung/ktbbedevassignment/api/v1/domain/controller/StockController.java new file mode 100644 index 0000000..109b2cb --- /dev/null +++ b/src/main/java/com/urung/ktbbedevassignment/api/v1/domain/controller/StockController.java @@ -0,0 +1,30 @@ +package com.urung.ktbbedevassignment.api.v1.domain.controller; + +import com.urung.ktbbedevassignment.api.v1.domain.dto.StockPriceDto; +import com.urung.ktbbedevassignment.api.v1.domain.service.StockService; +import com.urung.ktbbedevassignment.global.response.JsonResponse; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestPart; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +public class StockController { + private final StockService stockService; + + public StockController(StockService stockService) { + this.stockService = stockService; + } + + @GetMapping("/api/v1/stockPrice") + public ResponseEntity getStockPrice(@RequestPart("req") StockPriceDto.StockPriceRequest request) { + // API ํ‚ค ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ + + List response = stockService.getStockPrices(request); + + return ResponseEntity.ok(new JsonResponse(true, 200, "get Stock Price", response)); + } + +} diff --git a/src/main/java/com/urung/ktbbedevassignment/api/v1/domain/dto/StockPriceDto.java b/src/main/java/com/urung/ktbbedevassignment/api/v1/domain/dto/StockPriceDto.java new file mode 100644 index 0000000..52dd535 --- /dev/null +++ b/src/main/java/com/urung/ktbbedevassignment/api/v1/domain/dto/StockPriceDto.java @@ -0,0 +1,38 @@ +package com.urung.ktbbedevassignment.api.v1.domain.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.springframework.format.annotation.DateTimeFormat; + +import java.time.LocalDate; + +public class StockPriceDto { + + @Data + @NoArgsConstructor + public static class StockPriceRequest{ + private String companyCode; + @DateTimeFormat(pattern = "yyyy-MM-dd") + private LocalDate startDate; + @DateTimeFormat(pattern = "yyyy-MM-dd") + private LocalDate endDate; + + @Builder + public StockPriceRequest(String companyCode, LocalDate startDate, LocalDate endDate) { + this.companyCode = companyCode; + this.startDate = startDate; + this.endDate = endDate; + } + } + + @Data + @NoArgsConstructor + @AllArgsConstructor + public static class StockPriceResponse{ + private String companyName; + private String tradeDate; + private float closingPrice; + } +} diff --git a/src/main/java/com/urung/ktbbedevassignment/api/v1/domain/service/StockService.java b/src/main/java/com/urung/ktbbedevassignment/api/v1/domain/service/StockService.java new file mode 100644 index 0000000..569fb5a --- /dev/null +++ b/src/main/java/com/urung/ktbbedevassignment/api/v1/domain/service/StockService.java @@ -0,0 +1,51 @@ +package com.urung.ktbbedevassignment.api.v1.domain.service; + +import com.urung.ktbbedevassignment.api.v1.domain.dto.StockPriceDto; +import com.urung.ktbbedevassignment.domain.company.repository.CompanyRepository; +import com.urung.ktbbedevassignment.domain.stocksHistory.entity.StocksHistory; +import com.urung.ktbbedevassignment.domain.stocksHistory.repository.StocksHistoryRepository; +import com.urung.ktbbedevassignment.global.exception.BadRequestException; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + + +import java.time.format.DateTimeFormatter; +import java.util.List; +import java.util.stream.Collectors; + +import static com.urung.ktbbedevassignment.global.exception.ErrorResponseStatus.*; + + +@Slf4j +@Service +public class StockService { + private final CompanyRepository companyRepository; + private final StocksHistoryRepository stocksHistoryRepository; + + public StockService(CompanyRepository companyRepository, StocksHistoryRepository stocksHistoryRepository) { + this.companyRepository = companyRepository; + this.stocksHistoryRepository = stocksHistoryRepository; + } + + public List getStockPrices(StockPriceDto.StockPriceRequest request){ + String companyName = companyRepository.findCompanyNameByCode(request.getCompanyCode()) + .orElseThrow(() -> new BadRequestException(COMPANY_NOT_FOUND)); + + List stockHistories = stocksHistoryRepository + .findByCompanyCodeAndTradeDateBetween( + request.getCompanyCode(), + request.getStartDate(), + request.getEndDate() + ); + + // DTO ๋ณ€ํ™˜ + return stockHistories.stream() + .map(history -> { + StockPriceDto.StockPriceResponse res = new StockPriceDto.StockPriceResponse(); + res.setCompanyName(companyName); + res.setTradeDate(history.getTradeDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); + res.setClosingPrice(history.getClosePrice()); + return res; + }).collect(Collectors.toList()); + } +} \ No newline at end of file diff --git a/src/main/java/com/urung/ktbbedevassignment/domain/company/entity/Company.java b/src/main/java/com/urung/ktbbedevassignment/domain/company/entity/Company.java new file mode 100644 index 0000000..3f53b62 --- /dev/null +++ b/src/main/java/com/urung/ktbbedevassignment/domain/company/entity/Company.java @@ -0,0 +1,20 @@ +package com.urung.ktbbedevassignment.domain.company.entity; + +import com.urung.ktbbedevassignment.global.config.BaseTime; +import jakarta.persistence.*; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@Table(name = "company") +@NoArgsConstructor +@Entity +public class Company extends BaseTime { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "company_code") + private String companyCode; + + @Column(name = "company_name") + private String companyName; +} diff --git a/src/main/java/com/urung/ktbbedevassignment/domain/company/repository/CompanyRepository.java b/src/main/java/com/urung/ktbbedevassignment/domain/company/repository/CompanyRepository.java new file mode 100644 index 0000000..21d5f58 --- /dev/null +++ b/src/main/java/com/urung/ktbbedevassignment/domain/company/repository/CompanyRepository.java @@ -0,0 +1,13 @@ +package com.urung.ktbbedevassignment.domain.company.repository; + +import com.urung.ktbbedevassignment.domain.company.entity.Company; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +import java.util.Optional; + +public interface CompanyRepository extends JpaRepository{ + @Query("SELECT c.companyName FROM Company c WHERE c.companyCode = :companyCode") + Optional findCompanyNameByCode(@Param("companyCode") String companyCode); +} diff --git a/src/main/java/com/urung/ktbbedevassignment/domain/stocksHistory/entity/StocksHistory.java b/src/main/java/com/urung/ktbbedevassignment/domain/stocksHistory/entity/StocksHistory.java new file mode 100644 index 0000000..9066847 --- /dev/null +++ b/src/main/java/com/urung/ktbbedevassignment/domain/stocksHistory/entity/StocksHistory.java @@ -0,0 +1,36 @@ +package com.urung.ktbbedevassignment.domain.stocksHistory.entity; + +import jakarta.persistence.*; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.time.LocalDate; + +@Getter +@Table(name = "stocks_history") +@NoArgsConstructor +@Entity +public class StocksHistory { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "company_code") + private String companyCode; + + @Column(name = "trade_date") + private LocalDate tradeDate; + + @Column(name = "open_price") + private float openPrice; + + @Column(name = "high_price") + private float highPrice; + + @Column(name = "low_price") + private float lowPrice; + + @Column(name = "close_price") + private float closePrice; + + @Column(name = "volume") + private float volume; +} diff --git a/src/main/java/com/urung/ktbbedevassignment/domain/stocksHistory/repository/StocksHistoryRepository.java b/src/main/java/com/urung/ktbbedevassignment/domain/stocksHistory/repository/StocksHistoryRepository.java new file mode 100644 index 0000000..b3b72af --- /dev/null +++ b/src/main/java/com/urung/ktbbedevassignment/domain/stocksHistory/repository/StocksHistoryRepository.java @@ -0,0 +1,15 @@ +package com.urung.ktbbedevassignment.domain.stocksHistory.repository; + +import com.urung.ktbbedevassignment.domain.stocksHistory.entity.StocksHistory; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.time.LocalDate; +import java.util.List; + +public interface StocksHistoryRepository extends JpaRepository { + List findByCompanyCodeAndTradeDateBetween( + String companyCode, + LocalDate startDate, + LocalDate endDate + ); +} diff --git a/src/main/java/com/urung/ktbbedevassignment/global/config/BaseTime.java b/src/main/java/com/urung/ktbbedevassignment/global/config/BaseTime.java new file mode 100644 index 0000000..be2d2a5 --- /dev/null +++ b/src/main/java/com/urung/ktbbedevassignment/global/config/BaseTime.java @@ -0,0 +1,24 @@ +package com.urung.ktbbedevassignment.global.config; + +import jakarta.persistence.Column; +import jakarta.persistence.EntityListeners; +import jakarta.persistence.MappedSuperclass; +import lombok.Getter; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +import java.time.LocalDateTime; + +@Getter +@MappedSuperclass +@EntityListeners(AuditingEntityListener.class) +public class BaseTime { + @CreatedDate + @Column(updatable = false) + private LocalDateTime createdAt; + + @LastModifiedDate + @Column(name = "modified_at", nullable = false) + private LocalDateTime updatedAt; +} diff --git a/src/main/java/com/urung/ktbbedevassignment/global/exception/BadRequestException.java b/src/main/java/com/urung/ktbbedevassignment/global/exception/BadRequestException.java new file mode 100644 index 0000000..cf5e5a5 --- /dev/null +++ b/src/main/java/com/urung/ktbbedevassignment/global/exception/BadRequestException.java @@ -0,0 +1,19 @@ +package com.urung.ktbbedevassignment.global.exception; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +public class BadRequestException extends RuntimeException { + private ErrorResponseStatus status; + + public BadRequestException(ErrorResponseStatus status){ + this.status = status; + } + public ErrorResponseStatus getStatus(){ + return this.status; + } + +} diff --git a/src/main/java/com/urung/ktbbedevassignment/global/exception/ErrorResponse.java b/src/main/java/com/urung/ktbbedevassignment/global/exception/ErrorResponse.java new file mode 100644 index 0000000..8ff3385 --- /dev/null +++ b/src/main/java/com/urung/ktbbedevassignment/global/exception/ErrorResponse.java @@ -0,0 +1,22 @@ +package com.urung.ktbbedevassignment.global.exception; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +@Getter +@AllArgsConstructor +public class ErrorResponse { + private final String timeStamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); + private final boolean isSuccess; + private final int errorCode; + private final String message; + + public ErrorResponse(ErrorResponseStatus status){ + this.isSuccess = status.isSuccess(); + this.errorCode = status.getErrorCode(); + this.message = status.getMessage(); + } +} diff --git a/src/main/java/com/urung/ktbbedevassignment/global/exception/ErrorResponseStatus.java b/src/main/java/com/urung/ktbbedevassignment/global/exception/ErrorResponseStatus.java new file mode 100644 index 0000000..4c1ed76 --- /dev/null +++ b/src/main/java/com/urung/ktbbedevassignment/global/exception/ErrorResponseStatus.java @@ -0,0 +1,46 @@ +package com.urung.ktbbedevassignment.global.exception; + +public enum ErrorResponseStatus { + // 400 Bad Request + MISSING_API_KEY(false, 4001, "APIํ‚ค๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”."), + INVALID_DATE_FORMAT(false, 4002, "๋‚ ์งœ ํ˜•์‹์ด ๋งž์ง€ ์•Š์Šต๋‹ˆ๋‹ค."), + INVALID_DATE_RANGE(false, 4003, "์กฐํšŒ ์‹œ์ž‘์ผ์ด ์ข…๋ฃŒ์ผ๋ณด๋‹ค ๋Šฆ์Šต๋‹ˆ๋‹ค."), + MISSING_REQUIRED_PARAMETER(false, 4004, "์ž…๋ ฅ๊ฐ’์„ ํ™•์ธํ•ด์ฃผ์„ธ์š”."), + // 403 Forbidden + INVALID_API_KEY(false, 4031, "APIํ‚ค๊ฐ€ ์œ ํšจํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."), + + // 404 Not Found + COMPANY_NOT_FOUND(false, 4041, "์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ข…๋ชฉ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค."), + STOCK_DATA_NOT_FOUND(false, 4042, "๋“ฑ๋ก๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค."), + API_NOT_FOUND(false, 4043, "์กด์žฌํ•˜์ง€ ์•Š๋Š” API์ž…๋‹ˆ๋‹ค."), + + // 429 Too Many Request + + + // 500 Internal Server Error + INTERNAL_SERVER_ERROR(false, 5001, "์„œ๋ฒ„ ๋‚ด๋ถ€์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค.") + ; + private final boolean isSuccess; + private final int errorCode; + private final String message; + + private ErrorResponseStatus(boolean isSuccess, int errorCode, String message){ + this.isSuccess = isSuccess; + this.errorCode = errorCode; + this.message = message; + } + + public boolean isSuccess(){ + return this.isSuccess; + } + + public int getErrorCode(){ + return this.errorCode; + } + + public String getMessage(){ + return this.message; + } + + +} diff --git a/src/main/java/com/urung/ktbbedevassignment/global/exception/ForbiddenException.java b/src/main/java/com/urung/ktbbedevassignment/global/exception/ForbiddenException.java new file mode 100644 index 0000000..39adea0 --- /dev/null +++ b/src/main/java/com/urung/ktbbedevassignment/global/exception/ForbiddenException.java @@ -0,0 +1,13 @@ +package com.urung.ktbbedevassignment.global.exception; + +public class ForbiddenException extends RuntimeException{ + private ErrorResponseStatus status; + + public ForbiddenException(ErrorResponseStatus status){ + this.status = status; + } + + public ErrorResponseStatus getStatus(){ + return this.status; + } +} diff --git a/src/main/java/com/urung/ktbbedevassignment/global/exception/GlobalException.java b/src/main/java/com/urung/ktbbedevassignment/global/exception/GlobalException.java new file mode 100644 index 0000000..52d6699 --- /dev/null +++ b/src/main/java/com/urung/ktbbedevassignment/global/exception/GlobalException.java @@ -0,0 +1,13 @@ +package com.urung.ktbbedevassignment.global.exception; + +public class GlobalException extends RuntimeException { + private ErrorResponseStatus status; + + public GlobalException(ErrorResponseStatus status) { + this.status = status; + } + + public ErrorResponseStatus getStatus() { + return this.status; + } +} diff --git a/src/main/java/com/urung/ktbbedevassignment/global/exception/NotFoundException.java b/src/main/java/com/urung/ktbbedevassignment/global/exception/NotFoundException.java new file mode 100644 index 0000000..716ebda --- /dev/null +++ b/src/main/java/com/urung/ktbbedevassignment/global/exception/NotFoundException.java @@ -0,0 +1,12 @@ +package com.urung.ktbbedevassignment.global.exception; + +public class NotFoundException extends RuntimeException { + private ErrorResponseStatus status; + + public NotFoundException(ErrorResponseStatus status) { + this.status = status; + } + public ErrorResponseStatus getStatus() { + return this.status; + } +} diff --git a/src/main/java/com/urung/ktbbedevassignment/global/handler/GlobalExceptionHandler.java b/src/main/java/com/urung/ktbbedevassignment/global/handler/GlobalExceptionHandler.java new file mode 100644 index 0000000..390e7c0 --- /dev/null +++ b/src/main/java/com/urung/ktbbedevassignment/global/handler/GlobalExceptionHandler.java @@ -0,0 +1,37 @@ +package com.urung.ktbbedevassignment.global.handler; + +import com.urung.ktbbedevassignment.global.exception.*; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +// Exception +@RestControllerAdvice +@Slf4j +public class GlobalExceptionHandler { + // 400 + @ExceptionHandler(BadRequestException.class) + public ResponseEntity BadRequestException(BadRequestException e){ + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ErrorResponse(e.getStatus())); + } + + //403 + @ExceptionHandler(ForbiddenException.class) + public ResponseEntity ForbiddenException(ForbiddenException e){ + return ResponseEntity.status(HttpStatus.FORBIDDEN).body(new ErrorResponse(e.getStatus())); + } + + //404 + @ExceptionHandler(NotFoundException.class) + public ResponseEntity NotFoundException(NotFoundException e){ + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse(e.getStatus())); + } + + //500 + @ExceptionHandler(GlobalException.class) + public ResponseEntity GlobalException(GlobalException e){ + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ErrorResponse(e.getStatus())); + } +} diff --git a/src/main/java/com/urung/ktbbedevassignment/global/response/JsonResponse.java b/src/main/java/com/urung/ktbbedevassignment/global/response/JsonResponse.java new file mode 100644 index 0000000..7f2e33f --- /dev/null +++ b/src/main/java/com/urung/ktbbedevassignment/global/response/JsonResponse.java @@ -0,0 +1,19 @@ +package com.urung.ktbbedevassignment.global.response; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class JsonResponse { + private final String timeStamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); + private boolean isSuccess; + private int status; + private String message; + private Object result; +} diff --git a/src/test/java/com/urung/ktbbedevassignment/KtbBeDevAssignmentApplicationTests.java b/src/test/java/com/urung/ktbbedevassignment/KtbBeDevAssignmentApplicationTests.java new file mode 100644 index 0000000..b68eaeb --- /dev/null +++ b/src/test/java/com/urung/ktbbedevassignment/KtbBeDevAssignmentApplicationTests.java @@ -0,0 +1,13 @@ +package com.urung.ktbbedevassignment; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class KtbBeDevAssignmentApplicationTests { + + @Test + void contextLoads() { + } + +}