diff --git a/IdeaProjects/Practice/2023-server-study b/IdeaProjects/Practice/2023-server-study deleted file mode 160000 index 38ba776..0000000 --- a/IdeaProjects/Practice/2023-server-study +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 38ba7761d4c9015ba4c05ee5e94ab004c37c6692 diff --git a/IdeaProjects/youjin/2023-server-study b/IdeaProjects/youjin/2023-server-study new file mode 160000 index 0000000..8fc2202 --- /dev/null +++ b/IdeaProjects/youjin/2023-server-study @@ -0,0 +1 @@ +Subproject commit 8fc2202c4502093a775030a97e991e061cef6b4b diff --git a/IdeaProjects/youjin/SpringBoot/Practice/.gitignore b/IdeaProjects/youjin/SpringBoot/Practice/.gitignore new file mode 100644 index 0000000..1c494ff --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Practice/.gitignore @@ -0,0 +1,44 @@ +.gradle +.idea + +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/IdeaProjects/youjin/SpringBoot/Practice/build.gradle b/IdeaProjects/youjin/SpringBoot/Practice/build.gradle new file mode 100644 index 0000000..2af64d0 --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Practice/build.gradle @@ -0,0 +1,33 @@ +buildscript { + ext { + springBootVersion = '2.1.7.RELEASE' + } + repositories { + mavenCentral() + jcenter() + } + dependencies { + classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") + } +} + +apply plugin: 'java' +apply plugin: 'eclipse' +apply plugin: 'org.springframework.boot' +apply plugin: 'io.spring.dependency-management' + +group = 'com.practice' +version = '1.0-SNAPSHOT' +sourceCompatibility = 1.8 + +repositories { + mavenCentral() +} + +dependencies { + compile('org.springframework.boot:spring-boot-starter-web') + compile('org.projectlombok:lombok') + compile('org.springframework.boot:spring-boot-starter-data-jpa') + compile('com.h2database:h2') + testCompile('org.springframework.boot:spring-boot-starter-test') +} diff --git a/IdeaProjects/youjin/SpringBoot/Practice/gradle/wrapper/gradle-wrapper.jar b/IdeaProjects/youjin/SpringBoot/Practice/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..249e583 Binary files /dev/null and b/IdeaProjects/youjin/SpringBoot/Practice/gradle/wrapper/gradle-wrapper.jar differ diff --git a/IdeaProjects/youjin/SpringBoot/Practice/gradle/wrapper/gradle-wrapper.properties b/IdeaProjects/youjin/SpringBoot/Practice/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..4ec0056 --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Practice/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Sun Sep 24 11:18:24 KST 2023 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/IdeaProjects/youjin/SpringBoot/Practice/gradlew b/IdeaProjects/youjin/SpringBoot/Practice/gradlew new file mode 100644 index 0000000..1b6c787 --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Practice/gradlew @@ -0,0 +1,234 @@ +#!/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. +# + +############################################################################## +# +# 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/master/subprojects/plugins/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 + +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +APP_NAME="Gradle" +APP_BASE_NAME=${0##*/} + +# 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"' + +# 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 + which java >/dev/null 2>&1 || 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 + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + 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 + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# 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/IdeaProjects/youjin/SpringBoot/Practice/gradlew.bat b/IdeaProjects/youjin/SpringBoot/Practice/gradlew.bat new file mode 100644 index 0000000..107acd3 --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Practice/gradlew.bat @@ -0,0 +1,89 @@ +@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 + +@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=. +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%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +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%"=="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! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/IdeaProjects/youjin/SpringBoot/Practice/settings.gradle b/IdeaProjects/youjin/SpringBoot/Practice/settings.gradle new file mode 100644 index 0000000..944bdc6 --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Practice/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'Practice' + diff --git a/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/Application.java b/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/Application.java new file mode 100644 index 0000000..14311a4 --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/Application.java @@ -0,0 +1,13 @@ +package com.practice; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; + +@EnableJpaAuditing +@SpringBootApplication +public class Application { + public static void main (String[] args){ + SpringApplication.run(Application.class, args); + } +} \ No newline at end of file diff --git a/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/domain/posts/BaseTimeEntitiy.java b/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/domain/posts/BaseTimeEntitiy.java new file mode 100644 index 0000000..44b929c --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/domain/posts/BaseTimeEntitiy.java @@ -0,0 +1,22 @@ +package com.practice.domain.posts; + +import lombok.Getter; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +import javax.persistence.EntityListeners; +import javax.persistence.MappedSuperclass; +import java.time.LocalDateTime; + +@Getter +@MappedSuperclass +@EntityListeners(AuditingEntityListener.class) +public abstract class BaseTimeEntitiy { + + @CreatedDate + private LocalDateTime createdDate; + + @LastModifiedDate + private LocalDateTime modifiedDate; +} diff --git a/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/domain/posts/Posts.java b/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/domain/posts/Posts.java new file mode 100644 index 0000000..9bc4abd --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/domain/posts/Posts.java @@ -0,0 +1,40 @@ +package com.practice.domain.posts; + +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Getter +@NoArgsConstructor +@Entity +public class Posts extends BaseTimeEntitiy{ + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(length = 500, nullable = false) + private String title; + + @Column(columnDefinition = "TEXT", nullable = false) + private String content; + + private String author; + + @Builder + public Posts(String title, String content, String author){ + this.title = title; + this.content = content; + this.author = author; + } + + public void update(String title, String content){ + this.title = title; + this.content = content; + } +} \ No newline at end of file diff --git a/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/domain/posts/PostsRepository.java b/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/domain/posts/PostsRepository.java new file mode 100644 index 0000000..1afcb18 --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/domain/posts/PostsRepository.java @@ -0,0 +1,6 @@ +package com.practice.domain.posts; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface PostsRepository extends JpaRepository { +} \ No newline at end of file diff --git a/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/service/posts/PostsService.java b/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/service/posts/PostsService.java new file mode 100644 index 0000000..1465bc4 --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/service/posts/PostsService.java @@ -0,0 +1,35 @@ +package com.practice.service.posts; + +import com.practice.domain.posts.Posts; +import com.practice.domain.posts.PostsRepository; +import com.practice.web.dto.PostsResponseDto; +import com.practice.web.dto.PostsSaveRequestDto; +import com.practice.web.PostsUpdateRequestDto; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@RequiredArgsConstructor +@Service +public class PostsService { + private final PostsRepository postsRepository; + + @Transactional + public Long save(PostsSaveRequestDto requestDto){ + return postsRepository.save(requestDto.toEntity()).getId(); + } + + @Transactional + public Long update(Long id, PostsUpdateRequestDto requestDto){ + Posts posts = postsRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id = "+id)); + + posts.update(requestDto.getTitle(),requestDto.getContent()); + return id; + } + + public PostsResponseDto findById(Long id){ + Posts entity = postsRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id = " + id)); + + return new PostsResponseDto(entity); + } +} diff --git a/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/web/HelloController.java b/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/web/HelloController.java new file mode 100644 index 0000000..0b6ad6f --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/web/HelloController.java @@ -0,0 +1,19 @@ +package com.practice.web; + +import com.practice.web.dto.HelloResponseDto; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class HelloController { + @GetMapping("/hello") + public String hello(){ + return "hello"; + } + + @GetMapping("/hello/dto") + public HelloResponseDto helloDto(@RequestParam("name") String name, @RequestParam("amount") int amount){ + return new HelloResponseDto(name, amount); + } +} \ No newline at end of file diff --git a/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/web/PostsApiController.java b/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/web/PostsApiController.java new file mode 100644 index 0000000..8be2068 --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/web/PostsApiController.java @@ -0,0 +1,29 @@ +package com.practice.web; + +import com.practice.service.posts.PostsService; +import com.practice.web.dto.PostsResponseDto; +import com.practice.web.dto.PostsSaveRequestDto; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +@RequiredArgsConstructor +@RestController +public class PostsApiController { + private final PostsService postsService; + + @PostMapping("/api/v1/posts") + public Long save(@RequestBody PostsSaveRequestDto requestDto){ + return postsService.save(requestDto); + } + + @PutMapping("/api/v1/posts/{id}") + public Long update(@PathVariable Long id, @RequestBody PostsUpdateRequestDto requestDto) { + + return postsService.update(id, requestDto); + } + + @GetMapping("/api/v1/posts/{id}") + public PostsResponseDto findById(@PathVariable Long id){ + return postsService.findById(id); + } +} diff --git a/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/web/PostsUpdateRequestDto.java b/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/web/PostsUpdateRequestDto.java new file mode 100644 index 0000000..ba99a39 --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/web/PostsUpdateRequestDto.java @@ -0,0 +1,18 @@ +package com.practice.web; + +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +public class PostsUpdateRequestDto { + private String title; + private String content; + + @Builder + public PostsUpdateRequestDto(String title, String content){ + this.title = title; + this.content = content; + } +} \ No newline at end of file diff --git a/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/web/dto/HelloResponseDto.java b/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/web/dto/HelloResponseDto.java new file mode 100644 index 0000000..e4aa230 --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/web/dto/HelloResponseDto.java @@ -0,0 +1,11 @@ +package com.practice.web.dto; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor +public class HelloResponseDto { + private final String name; + private final int amount; +} \ No newline at end of file diff --git a/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/web/dto/PostsResponseDto.java b/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/web/dto/PostsResponseDto.java new file mode 100644 index 0000000..016f013 --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/web/dto/PostsResponseDto.java @@ -0,0 +1,19 @@ +package com.practice.web.dto; + +import com.practice.domain.posts.Posts; +import lombok.Getter; + +@Getter +public class PostsResponseDto { + private Long id; + private String title; + private String content; + private String author; + + public PostsResponseDto(Posts entity){ + this.id = entity.getId(); + this.title = entity.getTitle(); + this.content = entity.getContent(); + this.author = entity.getAuthor(); + } +} \ No newline at end of file diff --git a/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/web/dto/PostsSaveRequestDto.java b/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/web/dto/PostsSaveRequestDto.java new file mode 100644 index 0000000..59b25e2 --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Practice/src/main/java/com/practice/web/dto/PostsSaveRequestDto.java @@ -0,0 +1,29 @@ +package com.practice.web.dto; + +import com.practice.domain.posts.Posts; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +public class PostsSaveRequestDto { + private String title; + private String content; + private String author; + @Builder + public PostsSaveRequestDto(String title, String content, String author){ + this.title = title; + this.content = content; + this.author = author; + } + + + public Posts toEntity() { + return Posts.builder() + .title(title) + .content(content) + .author(author) + .build(); + } +} \ No newline at end of file diff --git a/IdeaProjects/youjin/SpringBoot/Practice/src/test/java/com/practice/HelloControllerTest.java b/IdeaProjects/youjin/SpringBoot/Practice/src/test/java/com/practice/HelloControllerTest.java new file mode 100644 index 0000000..7e65411 --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Practice/src/test/java/com/practice/HelloControllerTest.java @@ -0,0 +1,45 @@ +package com.practice; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; + +import static org.hamcrest.Matchers.is; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@RunWith(SpringRunner.class) +@WebMvcTest +public class HelloControllerTest { + + @Autowired + private MockMvc mvc; + + @Test + public void hello가_리턴된다() throws Exception { + String hello = "hello"; + + mvc.perform(get("/hello")) + .andExpect(status().isOk()) + .andExpect(content().string(hello)); + } + + @Test + public void helloDto가_리턴된다() throws Exception { + String name = "hello"; + int amount = 1000; + + mvc.perform( + get("/hello/dto") + .param("name", name) + .param("amount", String.valueOf(amount))) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.name", is(name))) + .andExpect(jsonPath("$.amount", is(amount))); + } +} \ No newline at end of file diff --git a/IdeaProjects/youjin/SpringBoot/Practice/src/test/java/com/practice/domain/posts/PostsRepositoryTest.java b/IdeaProjects/youjin/SpringBoot/Practice/src/test/java/com/practice/domain/posts/PostsRepositoryTest.java new file mode 100644 index 0000000..6b9a5ee --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Practice/src/test/java/com/practice/domain/posts/PostsRepositoryTest.java @@ -0,0 +1,71 @@ +package com.practice.domain.posts; + +import org.junit.After; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + + +import java.time.LocalDateTime; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class PostsRepositoryTest { + + @Autowired + PostsRepository postsRepository; + + @After + public void cleanup(){ + postsRepository.deleteAll(); + } + + @Test + public void 게시글저장_불러오기(){ + //given + String title = "테스트 게시글"; + String content = "테스트 본문"; + + postsRepository.save(Posts.builder() + .title(title) + .content(content) + .author("youjin09222@gmail.com") + .build()); + + //when + List postsList = postsRepository.findAll(); + + //then + Posts posts = postsList.get(0); + assertThat(posts.getTitle()).isEqualTo(title); + assertThat(posts.getContent()).isEqualTo(content); + } + + @Test + public void BaseTimeEntity_등록(){ + //given + LocalDateTime now = LocalDateTime.of(2019,6,4,0,0,0); + postsRepository.save(Posts.builder() + .title("title") + .content("content") + .author("author") + .build()); + + //when + List postsList = postsRepository.findAll(); + + //then + Posts posts = postsList.get(0); + + System.out.println(">>>>>>>> createDate =" + posts.getCreatedDate() + ", modifiedDate =" + posts.getModifiedDate()); + + assertThat(posts.getCreatedDate()).isAfter(now); + assertThat(posts.getModifiedDate()).isAfter(now); + } +} + diff --git a/IdeaProjects/youjin/SpringBoot/Practice/src/test/java/com/practice/dto/HelloResponseDtoTest.java b/IdeaProjects/youjin/SpringBoot/Practice/src/test/java/com/practice/dto/HelloResponseDtoTest.java new file mode 100644 index 0000000..cc9dbc8 --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Practice/src/test/java/com/practice/dto/HelloResponseDtoTest.java @@ -0,0 +1,22 @@ +package com.practice.dto; + +import com.practice.web.dto.HelloResponseDto; +import org.junit.Test; +import static org.assertj.core.api.Assertions.assertThat; + +public class HelloResponseDtoTest { + + @Test + public void 롬복_기능_테스트() { + // given + String name = "test"; + int amount = 1000; + + // when + HelloResponseDto dto = new HelloResponseDto(name, amount); + + // then + assertThat(dto.getName()).isEqualTo(name); + assertThat(dto.getAmount()).isEqualTo(amount); + } +} \ No newline at end of file diff --git a/IdeaProjects/youjin/SpringBoot/Practice/src/test/java/com/practice/web/PostsApiControllerTest.java b/IdeaProjects/youjin/SpringBoot/Practice/src/test/java/com/practice/web/PostsApiControllerTest.java new file mode 100644 index 0000000..e0dab8a --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Practice/src/test/java/com/practice/web/PostsApiControllerTest.java @@ -0,0 +1,101 @@ +package com.practice.web; + +import com.practice.domain.posts.Posts; +import com.practice.domain.posts.PostsRepository; +import com.practice.web.dto.PostsSaveRequestDto; +import org.junit.After; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.List; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class PostsApiControllerTest { + @LocalServerPort + private int port; + + @Autowired + private TestRestTemplate restTemplate; + + @Autowired + private PostsRepository postsRepository; + + @After + public void tearDown() throws Exception{ + postsRepository.deleteAll(); + } + + @Test + public void Posts_등록된다() throws Exception{ + //given + String title = "title"; + String content = "content"; + PostsSaveRequestDto requestDto = PostsSaveRequestDto.builder() + .title(title) + .content(content) + .author("author") + .build(); + + String url = "http://localhost:" + port + "/api/v1/posts"; + + //when + ResponseEntity responseEntity = restTemplate.postForEntity(url, requestDto, Long.class); + //then + assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(responseEntity.getBody()).isGreaterThan(0L); + + List all = postsRepository.findAll(); + assertThat(all.get(0).getTitle()).isEqualTo(title); + assertThat(all.get(0).getContent()).isEqualTo(content); + } + + @Test + public void Posts_수정된다() throws Exception{ + //given + Posts savedPosts = postsRepository.save(Posts.builder() + .title("title") + .content("content") + .author("author") + .build()); + + Long updateId = savedPosts.getId(); + String expectedTitle = "title2"; + String expectedContent = "content2"; + + PostsUpdateRequestDto requestDto = + PostsUpdateRequestDto.builder() + .title(expectedTitle) + .content(expectedContent) + .build(); + + + String url = "http://localhost:"+port+"/api/v1/posts/"+updateId; + + + HttpEntity requestEntity = new HttpEntity<>(requestDto); + + // when + ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.PUT, + requestEntity, Long.class); + + // then + assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(responseEntity.getBody()).isGreaterThan(0L); + + List all = postsRepository.findAll(); + assertThat(all.get(0).getTitle()).isEqualTo(expectedTitle); + assertThat(all.get(0).getContent()).isEqualTo(expectedContent); + } +} \ No newline at end of file diff --git a/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/.gitignore b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/.gitignore new file mode 100644 index 0000000..b63da45 --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/.gitignore @@ -0,0 +1,42 @@ +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/.idea/.gitignore b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/.idea/gradle.xml b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/.idea/gradle.xml new file mode 100644 index 0000000..14746e7 --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/.idea/gradle.xml @@ -0,0 +1,16 @@ + + + + + + \ No newline at end of file diff --git a/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/.idea/misc.xml b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/.idea/misc.xml new file mode 100644 index 0000000..7eec1db --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/.idea/uiDesigner.xml b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/build.gradle b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/build.gradle new file mode 100644 index 0000000..2af64d0 --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/build.gradle @@ -0,0 +1,33 @@ +buildscript { + ext { + springBootVersion = '2.1.7.RELEASE' + } + repositories { + mavenCentral() + jcenter() + } + dependencies { + classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") + } +} + +apply plugin: 'java' +apply plugin: 'eclipse' +apply plugin: 'org.springframework.boot' +apply plugin: 'io.spring.dependency-management' + +group = 'com.practice' +version = '1.0-SNAPSHOT' +sourceCompatibility = 1.8 + +repositories { + mavenCentral() +} + +dependencies { + compile('org.springframework.boot:spring-boot-starter-web') + compile('org.projectlombok:lombok') + compile('org.springframework.boot:spring-boot-starter-data-jpa') + compile('com.h2database:h2') + testCompile('org.springframework.boot:spring-boot-starter-test') +} diff --git a/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/gradle/wrapper/gradle-wrapper.jar b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..249e583 Binary files /dev/null and b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/gradle/wrapper/gradle-wrapper.jar differ diff --git a/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/gradle/wrapper/gradle-wrapper.properties b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..4b40535 --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +#Sun Oct 01 19:59:41 KST 2023 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists + diff --git a/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/gradlew b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/gradlew new file mode 100644 index 0000000..1b6c787 --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/gradlew @@ -0,0 +1,234 @@ +#!/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. +# + +############################################################################## +# +# 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/master/subprojects/plugins/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 + +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +APP_NAME="Gradle" +APP_BASE_NAME=${0##*/} + +# 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"' + +# 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 + which java >/dev/null 2>&1 || 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 + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + 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 + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# 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/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/gradlew.bat b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/gradlew.bat new file mode 100644 index 0000000..107acd3 --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/gradlew.bat @@ -0,0 +1,89 @@ +@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 + +@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=. +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%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +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%"=="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! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/settings.gradle b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/settings.gradle new file mode 100644 index 0000000..9e0f3dc --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'Shopping' + diff --git a/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/main/java/com/shopping/Application.java b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/main/java/com/shopping/Application.java new file mode 100644 index 0000000..60619a3 --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/main/java/com/shopping/Application.java @@ -0,0 +1,13 @@ +package com.shopping; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; + +@EnableJpaAuditing +@SpringBootApplication +public class Application { + public static void main (String[] args){ + SpringApplication.run(Application.class, args); + } +} \ No newline at end of file diff --git a/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/main/java/com/shopping/domain/posts/Posts.java b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/main/java/com/shopping/domain/posts/Posts.java new file mode 100644 index 0000000..622fafd --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/main/java/com/shopping/domain/posts/Posts.java @@ -0,0 +1,47 @@ +package com.shopping.domain.posts; + +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import javax.persistence.*; + +@Getter +@NoArgsConstructor +@Entity +public class Posts{ + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(nullable = false) + private String product_name; + + @Column(nullable = false) + private String product_number; + + @Column + private String product_category; + + @Column(nullable = false) + private int product_price; + + @Column + private int product_stock; + + @Builder + public Posts(String product_name, int product_price, String product_number, int product_stock, String product_category){ + this.product_name = product_name; + this.product_price = product_price; + this.product_number = product_number; + this.product_stock = product_stock; + this.product_category = product_category; + } + + public void update(String product_name, String product_number, int product_price, int product_stock){ + this.product_name = product_name; + this.product_number = product_number; + this.product_price = product_price; + this.product_stock = product_stock; + } +} \ No newline at end of file diff --git a/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/main/java/com/shopping/domain/posts/PostsRepository.java b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/main/java/com/shopping/domain/posts/PostsRepository.java new file mode 100644 index 0000000..75a8bc4 --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/main/java/com/shopping/domain/posts/PostsRepository.java @@ -0,0 +1,6 @@ +package com.shopping.domain.posts; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface PostsRepository extends JpaRepository { +} \ No newline at end of file diff --git a/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/main/java/com/shopping/service/posts/PostsService.java b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/main/java/com/shopping/service/posts/PostsService.java new file mode 100644 index 0000000..d08854c --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/main/java/com/shopping/service/posts/PostsService.java @@ -0,0 +1,35 @@ +package com.shopping.service.posts; + +import com.shopping.domain.posts.Posts; +import com.shopping.domain.posts.PostsRepository; +import com.shopping.web.dto.PostsUpdateRequestDto; +import com.shopping.web.dto.PostsReadResponseDto; +import com.shopping.web.dto.PostsCreateRequestDto; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@RequiredArgsConstructor +@Service +public class PostsService { + private final PostsRepository postsRepository; + + @Transactional + public Long save(PostsCreateRequestDto requestDto){ + return postsRepository.save(requestDto.toEntity()).getId(); + } + + @Transactional + public Long update(Long id, PostsUpdateRequestDto requestDto){ + Posts posts = postsRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("해당 상품이 없습니다. id = "+id)); + + posts.update(requestDto.getProduct_name(),requestDto.getProduct_number(), requestDto.getProduct_price(), requestDto.getProduct_stock()); + return id; + } + + public PostsReadResponseDto findById(Long id){ + Posts entity = postsRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("해당 상품이 없습니다. id = "+id)); + + return new PostsReadResponseDto(entity); + } +} diff --git a/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/main/java/com/shopping/web/PostsApiController.java b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/main/java/com/shopping/web/PostsApiController.java new file mode 100644 index 0000000..a4f710a --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/main/java/com/shopping/web/PostsApiController.java @@ -0,0 +1,35 @@ +package com.shopping.web; + +import com.shopping.service.posts.PostsService; +import com.shopping.web.dto.PostsReadResponseDto; +import com.shopping.web.dto.PostsCreateRequestDto; +import com.shopping.web.dto.PostsUpdateRequestDto; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +@RequiredArgsConstructor +@RestController +public class PostsApiController { + private final PostsService postsService; + + @PostMapping("/api/shopping/posts") //Create + public Long save(@RequestBody PostsCreateRequestDto requestDto){ + return postsService.save(requestDto); + } + + @PutMapping("/api/shopping/posts/{id}") //Update + public Long update(@PathVariable Long id, @RequestBody PostsUpdateRequestDto requestDto) { + + return postsService.update(id, requestDto); + } + + @GetMapping("/api/shopping/posts/{id}") //Read + public PostsReadResponseDto findById(@PathVariable Long id){ + return postsService.findById(id); + } + +// @DeleteMapping("/api/shopping/posts/delete/{id}") //Delete +// public Long delete(@PathVariable Long id) { +// System.out.println(id); +// } +} diff --git a/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/main/java/com/shopping/web/dto/PostsCreateRequestDto.java b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/main/java/com/shopping/web/dto/PostsCreateRequestDto.java new file mode 100644 index 0000000..fc3d0ba --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/main/java/com/shopping/web/dto/PostsCreateRequestDto.java @@ -0,0 +1,36 @@ +package com.shopping.web.dto; + +import com.shopping.domain.posts.Posts; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +public class PostsCreateRequestDto { + + private String product_name; + private String product_number; + private String product_category; + private int product_price; + private int product_stock; + + @Builder + public PostsCreateRequestDto(String product_name, String product_number, String product_category, int product_price, int product_stock){ + this.product_number = product_number; + this.product_name = product_name; + this.product_category = product_category; + this.product_price = product_price; + this.product_stock = product_stock; + } + + public Posts toEntity() { + return Posts.builder() + .product_name(product_name) + .product_number(product_number) + .product_category(product_category) + .product_price(product_price) + .product_stock(product_stock) + .build(); + } +} \ No newline at end of file diff --git a/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/main/java/com/shopping/web/dto/PostsReadResponseDto.java b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/main/java/com/shopping/web/dto/PostsReadResponseDto.java new file mode 100644 index 0000000..e74e68e --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/main/java/com/shopping/web/dto/PostsReadResponseDto.java @@ -0,0 +1,24 @@ +package com.shopping.web.dto; + +import com.shopping.domain.posts.Posts; +import lombok.Getter; + +@Getter +public class PostsReadResponseDto { + + private Long id; + private String product_name; + private String product_number; + private String product_category; + private int product_price; + private int product_stock; + + public PostsReadResponseDto(Posts entity){ + this.id = entity.getId(); + this.product_name = entity.getProduct_name(); + this.product_number = entity.getProduct_number(); + this.product_category = entity.getProduct_category(); + this.product_price = entity.getProduct_price(); + this.product_stock = entity.getProduct_stock(); + } +} \ No newline at end of file diff --git a/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/main/java/com/shopping/web/dto/PostsUpdateRequestDto.java b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/main/java/com/shopping/web/dto/PostsUpdateRequestDto.java new file mode 100644 index 0000000..0e7ee1d --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/main/java/com/shopping/web/dto/PostsUpdateRequestDto.java @@ -0,0 +1,22 @@ +package com.shopping.web.dto; + +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +public class PostsUpdateRequestDto { + private String product_name; + private String product_number; + private int product_price; + private int product_stock; + + @Builder + public PostsUpdateRequestDto(String product_name, String product_number, int product_price, int product_stock){ + this.product_name = product_name; + this.product_number = product_number; + this.product_price = product_price; + this.product_stock = product_stock; + } +} \ No newline at end of file diff --git a/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/main/resources/application.properties b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/main/resources/application.properties new file mode 100644 index 0000000..706c288 --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/main/resources/application.properties @@ -0,0 +1,3 @@ +spring.jpa.show_sql=true +spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect +spring.h2.console.enabled=true \ No newline at end of file diff --git a/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/test/java/com/shopping/domain/posts/PostsRepositoryTest.java b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/test/java/com/shopping/domain/posts/PostsRepositoryTest.java new file mode 100644 index 0000000..01a08de --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/test/java/com/shopping/domain/posts/PostsRepositoryTest.java @@ -0,0 +1,52 @@ +package com.shopping.domain.posts; + +import org.junit.After; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import java.util.List; +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class PostsRepositoryTest { + + @Autowired + PostsRepository postsRepository; + + @After + public void cleanup(){ + postsRepository.deleteAll(); + } + + @Test + public void 상품_저장_불러오기(){ + //given + String product_name = "테스트 상품"; + String product_number = "F09E9"; + String product_category = "category"; + int product_price = 10000; + int product_stock = 15; + + postsRepository.save(Posts.builder() + .product_name(product_name) + .product_number(product_number) + .product_category(product_category) + .product_price(product_price) + .product_stock(product_stock) + .build()); + + //when + List postsList = postsRepository.findAll(); + + //then + Posts posts = postsList.get(0); + assertThat(posts.getProduct_name()).isEqualTo(product_name); + assertThat(posts.getProduct_number()).isEqualTo(product_number); + assertThat(posts.getProduct_category()).isEqualTo(product_category); + assertThat(posts.getProduct_price()).isEqualTo(product_price); + assertThat(posts.getProduct_stock()).isEqualTo(product_stock); + } +} diff --git a/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/test/java/com/shopping/web/PostsApiControllerTest.java b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/test/java/com/shopping/web/PostsApiControllerTest.java new file mode 100644 index 0000000..52ca989 --- /dev/null +++ b/IdeaProjects/youjin/SpringBoot/Shopping/Shopping/src/test/java/com/shopping/web/PostsApiControllerTest.java @@ -0,0 +1,144 @@ +package com.shopping.web; + +import com.shopping.domain.posts.Posts; +import com.shopping.domain.posts.PostsRepository; +import com.shopping.web.dto.PostsCreateRequestDto; +import com.shopping.web.dto.PostsUpdateRequestDto; +import org.junit.After; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.List; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class PostsApiControllerTest { + @LocalServerPort + private int port; + + @Autowired + private TestRestTemplate restTemplate; + + @Autowired + private PostsRepository postsRepository; + + @After + public void tearDown() throws Exception{ + postsRepository.deleteAll(); + } + + @Test + public void Posts_등록() throws Exception{ + //given + String product_name = "product_name"; + String product_number = "product_number"; + String product_category = "product_category"; + int product_price = 10000; + int product_stock = 15; + + PostsCreateRequestDto requestDto = PostsCreateRequestDto.builder() + .product_name(product_name) + .product_number(product_number) + .product_category(product_category) + .product_price(product_price) + .product_stock(product_stock) + .build(); + + String url = "http://localhost:" + port + "/api/shopping/posts"; + + //when + ResponseEntity responseEntity = restTemplate.postForEntity(url, requestDto, Long.class); + + //then + assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(responseEntity.getBody()).isGreaterThan(0L); + + List all = postsRepository.findAll(); + assertThat(all.get(0).getProduct_name()).isEqualTo(product_name); + assertThat(all.get(0).getProduct_number()).isEqualTo(product_number); + assertThat(all.get(0).getProduct_category()).isEqualTo(product_category); + assertThat(all.get(0).getProduct_price()).isEqualTo(product_price); + assertThat(all.get(0).getProduct_stock()).isEqualTo(product_stock); + } + + @Test + public void Posts_수정() throws Exception{ + //given + Posts savedPosts = postsRepository.save(Posts.builder() + .product_name("product_name") + .product_number("EDD578") + .product_price(12000) + .product_stock(10) + .build()); + + Long updateNum = savedPosts.getId(); + String expectedName="product_name2"; + String expectedNumber = "product_number2"; + int expectedStock=15; + int expectedPrice=32000; + + PostsUpdateRequestDto requestDto = + PostsUpdateRequestDto.builder() + .product_name(expectedName) + .product_number(expectedNumber) + .product_stock(expectedStock) + .product_price(expectedPrice) + .build(); + + + String url = "http://localhost:" + port + "/api/shopping/posts/" + updateNum; + + + HttpEntity requestEntity = new HttpEntity<>(requestDto); + + // when + ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.PUT, + requestEntity, Long.class); + + // then + assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(responseEntity.getBody()).isGreaterThan(0L); + + List all = postsRepository.findAll(); + assertThat(all.get(0).getProduct_name()).isEqualTo(expectedName); + assertThat(all.get(0).getProduct_number()).isEqualTo(expectedNumber); + assertThat(all.get(0).getProduct_price()).isEqualTo(expectedPrice); + assertThat(all.get(0).getProduct_stock()).isEqualTo(expectedStock); + } + +// @Test +// public void Posts_삭제(){ +// //given +// Posts savedPosts = postsRepository.save(Posts.builder() +// .product_name("product_name") +// .product_price(12000) +// .product_stock(10) +// .build()); +// +// Long id = savedPosts.getId(); +// +// String url = "http://localhost:" + port + "/api/shopping/posts/" + id; +// +// HttpEntity requestEntity = new HttpEntity<>(savedPosts); +// +// // when +// ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.DELETE, requestEntity, Long.class); +// +// // then +// assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK); +// assertThat(responseEntity.getBody()).isGreaterThan(0L); +// +// postsRepository.deleteById(id); +// } +} \ No newline at end of file