diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..d5b20d8 Binary files /dev/null and b/.DS_Store differ diff --git a/ThirdSeminar/.gitignore b/ThirdSeminar/.gitignore new file mode 100644 index 0000000..c2065bc --- /dev/null +++ b/ThirdSeminar/.gitignore @@ -0,0 +1,37 @@ +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/ + +### 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/ThirdSeminar/build.gradle b/ThirdSeminar/build.gradle new file mode 100644 index 0000000..4ff82f2 --- /dev/null +++ b/ThirdSeminar/build.gradle @@ -0,0 +1,33 @@ +plugins { + id 'java' + id 'org.springframework.boot' version '2.7.11' + id 'io.spring.dependency-management' version '1.0.15.RELEASE' +} + +group = 'sopt.org' +version = '0.0.1-SNAPSHOT' +sourceCompatibility = '11' + +configurations { + compileOnly { + extendsFrom annotationProcessor + } +} + +repositories { + mavenCentral() +} + +dependencies { + implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + implementation 'mysql:mysql-connector-java:8.0.32' + implementation 'org.springframework.boot:spring-boot-starter-validation' + compileOnly 'org.projectlombok:lombok' + annotationProcessor 'org.projectlombok:lombok' + testImplementation 'org.springframework.boot:spring-boot-starter-test' +} + +tasks.named('test') { + useJUnitPlatform() +} diff --git a/ThirdSeminar/gradle/wrapper/gradle-wrapper.properties b/ThirdSeminar/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..774fae8 --- /dev/null +++ b/ThirdSeminar/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/ThirdSeminar/gradlew b/ThirdSeminar/gradlew new file mode 100755 index 0000000..a69d9cb --- /dev/null +++ b/ThirdSeminar/gradlew @@ -0,0 +1,240 @@ +#!/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 \ + "$@" + +# 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/ThirdSeminar/gradlew.bat b/ThirdSeminar/gradlew.bat new file mode 100644 index 0000000..53a6b23 --- /dev/null +++ b/ThirdSeminar/gradlew.bat @@ -0,0 +1,91 @@ +@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% equ 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% 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/ThirdSeminar/settings.gradle b/ThirdSeminar/settings.gradle new file mode 100644 index 0000000..397b799 --- /dev/null +++ b/ThirdSeminar/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'ThirdSeminar' diff --git a/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/ThirdSeminarApplication.java b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/ThirdSeminarApplication.java new file mode 100644 index 0000000..e152738 --- /dev/null +++ b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/ThirdSeminarApplication.java @@ -0,0 +1,13 @@ +package sopt.org.ThirdSeminar; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ThirdSeminarApplication { + + public static void main(String[] args) { + SpringApplication.run(ThirdSeminarApplication.class, args); + } + +} diff --git a/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/common/advice/ControllerExceptionAdvice.java b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/common/advice/ControllerExceptionAdvice.java new file mode 100644 index 0000000..440ca10 --- /dev/null +++ b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/common/advice/ControllerExceptionAdvice.java @@ -0,0 +1,27 @@ +package sopt.org.ThirdSeminar.common.advice; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import sopt.org.ThirdSeminar.exception.ErrorStatus; +import sopt.org.thirdSeminar.common.dto.ApiResponseDto; + +import javax.persistence.EntityNotFoundException; + +@RestControllerAdvice +public class ControllerExceptionAdvice { + + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ExceptionHandler(MethodArgumentNotValidException.class) + protected ApiResponseDto handleMethodArgumentNotValidException(final MethodArgumentNotValidException e) { + return ApiResponseDto.error(ErrorStatus.VALIDATION_REQUEST_MISSING_EXCEPTION); + } + + @ResponseStatus(HttpStatus.NOT_FOUND) + @ExceptionHandler(EntityNotFoundException.class) + protected ApiResponseDto handleEntityNotFoundException(final EntityNotFoundException e) { + return ApiResponseDto.error(ErrorStatus.NOT_FOUND_USER); + } +} diff --git a/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/common/dto/ApiResponseDto.java b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/common/dto/ApiResponseDto.java new file mode 100644 index 0000000..c3b45c1 --- /dev/null +++ b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/common/dto/ApiResponseDto.java @@ -0,0 +1,29 @@ +package sopt.org.thirdSeminar.common.dto; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import sopt.org.ThirdSeminar.exception.ErrorStatus; +import sopt.org.ThirdSeminar.exception.SuccessStatus; + +@Getter +@RequiredArgsConstructor(access = AccessLevel.PRIVATE) +@AllArgsConstructor(access = AccessLevel.PRIVATE) +public class ApiResponseDto { + + private final int code; + private final String message; + private T data; + + public static ApiResponseDto success(SuccessStatus successStatus){ + return new ApiResponseDto<>(successStatus.getHttpStatus().value(),successStatus.getMessage()); + } + public static ApiResponseDto success(SuccessStatus successStatus,T data){ + return new ApiResponseDto<>(successStatus.getHttpStatus().value(),successStatus.getMessage(),data); + } + public static ApiResponseDto error(ErrorStatus errorStatus) { + return new ApiResponseDto<>(errorStatus.getHttpStatus().value(), errorStatus.getMessage()); + } +} \ No newline at end of file diff --git a/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/controller/PostController.java b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/controller/PostController.java new file mode 100644 index 0000000..143c9b0 --- /dev/null +++ b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/controller/PostController.java @@ -0,0 +1,33 @@ +package sopt.org.ThirdSeminar.controller; + +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; +import sopt.org.ThirdSeminar.controller.dto.request.PostRequestDto; +import sopt.org.ThirdSeminar.controller.dto.response.PostResponseDto; +import sopt.org.ThirdSeminar.exception.SuccessStatus; +import sopt.org.ThirdSeminar.service.PostService; +import sopt.org.thirdSeminar.common.dto.ApiResponseDto; + +import javax.validation.Valid; +import java.util.List; + +@RestController +@RequiredArgsConstructor +public class PostController { + + private final PostService postService; + + @PostMapping("/post/create") + public ApiResponseDto crate(@RequestBody @Valid final PostRequestDto postRequestDto){ + return ApiResponseDto.success(SuccessStatus.CREATE_SUCCESS, postService.create(postRequestDto)); + } + @GetMapping("/post/{id}") + public ApiResponseDto search(@PathVariable final Long id){ + return ApiResponseDto.success(SuccessStatus.SEARCH_SUCCESS,postService.search(id)); + } + @GetMapping("/post/search") + public ApiResponseDto> searchByTitle(@RequestParam final String title){ + return ApiResponseDto.success(SuccessStatus.SEARCH_SUCCESS,postService.searchByTitle(title)); + } + +} diff --git a/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/controller/UserController.java b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/controller/UserController.java new file mode 100644 index 0000000..0760451 --- /dev/null +++ b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/controller/UserController.java @@ -0,0 +1,30 @@ +package sopt.org.ThirdSeminar.controller; + +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; +import sopt.org.ThirdSeminar.controller.dto.request.UserRequestDto; +import sopt.org.ThirdSeminar.controller.dto.response.UserResponseDto; +import sopt.org.ThirdSeminar.exception.SuccessStatus; +import sopt.org.ThirdSeminar.service.UserService; +import sopt.org.thirdSeminar.common.dto.ApiResponseDto; +import javax.validation.Valid; + +@RestController +@RequiredArgsConstructor +public class UserController { + + private final UserService userService; + + @PostMapping("/user/signup") + @ResponseStatus(HttpStatus.CREATED) + public ApiResponseDto create(@RequestBody @Valid final UserRequestDto request){ + return ApiResponseDto.success(SuccessStatus.SIGNUP_SUCCESS,userService.create(request)); + } + + @GetMapping("/user/{id}") + @ResponseStatus(HttpStatus.OK) + public ApiResponseDto search(@PathVariable @Valid final Long id){ + return ApiResponseDto.success(SuccessStatus.SEARCH_SUCCESS,userService.search(id)); + } +} diff --git a/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/controller/dto/request/PostRequestDto.java b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/controller/dto/request/PostRequestDto.java new file mode 100644 index 0000000..093879c --- /dev/null +++ b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/controller/dto/request/PostRequestDto.java @@ -0,0 +1,19 @@ +package sopt.org.ThirdSeminar.controller.dto.request; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; + +import javax.validation.constraints.NotNull; + +@Getter +@AllArgsConstructor(access = AccessLevel.PRIVATE) +public class PostRequestDto { + + @NotNull + private String title; + @NotNull + private String content; + @NotNull + private Long created_userId; +} diff --git a/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/controller/dto/request/UserRequestDto.java b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/controller/dto/request/UserRequestDto.java new file mode 100644 index 0000000..75f5a8c --- /dev/null +++ b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/controller/dto/request/UserRequestDto.java @@ -0,0 +1,30 @@ +package sopt.org.ThirdSeminar.controller.dto.request; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; + +import javax.validation.constraints.Email; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; + +@Getter +@AllArgsConstructor(access = AccessLevel.PRIVATE) +public class UserRequestDto { + @Email(message = "이메일 형식이 맞지 않습니다.") + @NotNull + private String email; + + @NotBlank + @Pattern(regexp = "^[가-힣a-zA-Z]{2,10}$", message = "닉네임 형식에 맞지 않습니다.") + private String nickname; + + @NotBlank + @Pattern(regexp="(?=.*[0-9])(?=.*[a-zA-Z])(?=.*\\W)(?=\\S+$).{8,20}", + message = "비밀번호는 영문 대,소문자와 숫자, 특수기호가 적어도 1개 이상씩 포함된 8자 ~ 20자의 비밀번호여야 합니다." + ) + private String password; + + +} diff --git a/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/controller/dto/response/PostResponseDto.java b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/controller/dto/response/PostResponseDto.java new file mode 100644 index 0000000..3bce587 --- /dev/null +++ b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/controller/dto/response/PostResponseDto.java @@ -0,0 +1,19 @@ +package sopt.org.ThirdSeminar.controller.dto.response; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@AllArgsConstructor(access = AccessLevel.PRIVATE) +public class PostResponseDto { + private Long id; + private String title; + private String content; + private String created_user; + + public static PostResponseDto of(Long id,String title,String content,String created_user){ + return new PostResponseDto(id,title,content,created_user); + } +} diff --git a/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/controller/dto/response/UserResponseDto.java b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/controller/dto/response/UserResponseDto.java new file mode 100644 index 0000000..591b7d6 --- /dev/null +++ b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/controller/dto/response/UserResponseDto.java @@ -0,0 +1,17 @@ +package sopt.org.ThirdSeminar.controller.dto.response; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor(access = AccessLevel.PRIVATE) +public class UserResponseDto { + + private Long userId; + private String nickname; + + public static UserResponseDto of(Long userId, String nickname) { + return new UserResponseDto(userId, nickname); + } +} diff --git a/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/domain/Post.java b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/domain/Post.java new file mode 100644 index 0000000..c752f36 --- /dev/null +++ b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/domain/Post.java @@ -0,0 +1,32 @@ +package sopt.org.ThirdSeminar.domain; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import javax.persistence.*; + +@Entity +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class Post { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(nullable = false) + private String title; + @Column(nullable = false) + private String content; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name="created_userId") + private User created_user; + + + +} diff --git a/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/domain/User.java b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/domain/User.java new file mode 100644 index 0000000..36be36a --- /dev/null +++ b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/domain/User.java @@ -0,0 +1,41 @@ +package sopt.org.ThirdSeminar.domain; + +import lombok.AccessLevel; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import javax.persistence.*; +import java.util.ArrayList; +import java.util.List; + +@Entity +@Getter +@NoArgsConstructor +public class User { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + public Long id; + + @Column(nullable = false) + private String nickname; + + @Column(nullable = false) + private String email; + + + @Column(nullable = false) + private String password; + + @OneToMany(mappedBy = "created_user") + private List posts=new ArrayList(); + @Builder + public User(String nickname,String email,String password){ + this.nickname=nickname; + this.email=email; + this.password=password; + this.posts=new ArrayList(); + } + + +} diff --git a/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/exception/ErrorStatus.java b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/exception/ErrorStatus.java new file mode 100644 index 0000000..0cdf79b --- /dev/null +++ b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/exception/ErrorStatus.java @@ -0,0 +1,35 @@ +package sopt.org.ThirdSeminar.exception; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; + +@Getter +@RequiredArgsConstructor(access = AccessLevel.PRIVATE) +public enum ErrorStatus { + + /* +BAD_REQUEST + */ + VALIDATION_EXCEPTION(HttpStatus.BAD_REQUEST, "잘못된 요청입니다."), + VALIDATION_REQUEST_MISSING_EXCEPTION(HttpStatus.BAD_REQUEST, "요청값이 입력되지 않았습니다."), + + NOT_FOUND_USER(HttpStatus.BAD_REQUEST, "검색 결과가 없습니다."), + /* + CONFLICT + */ + CONFLICT_EMAIL_EXCEPTION(HttpStatus.CONFLICT, "이미 등록된 이메일입니다."), + CONFLICT_NICKNAME_EXCEPTION(HttpStatus.CONFLICT, "이미 등록된 닉네임입니다."), + + /* + SERVER_ERROR + */ + INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "예상치 못한 서버 에러가 발생했습니다."), + BAD_GATEWAY_EXCEPTION(HttpStatus.BAD_GATEWAY, "일시적인 에러가 발생하였습니다.\n잠시 후 다시 시도해주세요!"), + SERVICE_UNAVAILABLE_EXCEPTION(HttpStatus.SERVICE_UNAVAILABLE, "현재 점검 중입니다.\n잠시 후 다시 시도해주세요!"), + ; + + private final HttpStatus httpStatus; + private final String message; +} diff --git a/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/exception/SuccessStatus.java b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/exception/SuccessStatus.java new file mode 100644 index 0000000..73e02fd --- /dev/null +++ b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/exception/SuccessStatus.java @@ -0,0 +1,19 @@ +package sopt.org.ThirdSeminar.exception; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; + +@Getter +@RequiredArgsConstructor(access = AccessLevel.PRIVATE) +public enum SuccessStatus { + + + SIGNUP_SUCCESS(HttpStatus.CREATED,"회원가입이 완료되었습니다"), + SEARCH_SUCCESS(HttpStatus.OK,"유저 검색에 성공했습니다."), + + CREATE_SUCCESS(HttpStatus.CREATED,"글 작성에 성공했습니다"); + private final HttpStatus httpStatus; + private final String message; +} diff --git a/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/infrastructure/PostRepository.java b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/infrastructure/PostRepository.java new file mode 100644 index 0000000..57cad92 --- /dev/null +++ b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/infrastructure/PostRepository.java @@ -0,0 +1,17 @@ +package sopt.org.ThirdSeminar.infrastructure; + + +import org.springframework.data.repository.Repository; +import sopt.org.ThirdSeminar.domain.Post; + +import java.util.List; + +public interface PostRepository extends Repository { + + void save(Post post); + + List findAllByTitle(String title); + + Post getOne(Long id); + +} diff --git a/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/infrastructure/UserRepository.java b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/infrastructure/UserRepository.java new file mode 100644 index 0000000..4e0f14e --- /dev/null +++ b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/infrastructure/UserRepository.java @@ -0,0 +1,11 @@ +package sopt.org.ThirdSeminar.infrastructure; + +import org.springframework.data.repository.Repository; +import sopt.org.ThirdSeminar.domain.User; + +public interface UserRepository extends Repository { + void save(User user) ; + + User getOne(Long id); + +} diff --git a/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/service/PostService.java b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/service/PostService.java new file mode 100644 index 0000000..0cbc885 --- /dev/null +++ b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/service/PostService.java @@ -0,0 +1,61 @@ +package sopt.org.ThirdSeminar.service; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import sopt.org.ThirdSeminar.controller.dto.request.PostRequestDto; +import sopt.org.ThirdSeminar.controller.dto.response.PostResponseDto; +import sopt.org.ThirdSeminar.domain.Post; +import sopt.org.ThirdSeminar.domain.User; +import sopt.org.ThirdSeminar.infrastructure.PostRepository; +import sopt.org.ThirdSeminar.infrastructure.UserRepository; + +import java.util.ArrayList; +import java.util.List; + +@Service +@RequiredArgsConstructor +public class PostService { + + private final PostRepository postRepository; + private final UserRepository userRepository; + @Transactional + public PostResponseDto create(PostRequestDto postRequestDto){ + + User user=userRepository.getOne(postRequestDto.getCreated_userId()); + Post post= Post.builder() + .title(postRequestDto.getTitle()) + .content(postRequestDto.getContent()) + .created_user(user) + .build(); + postRepository.save(post); + return PostResponseDto.of(post.getId(),post.getTitle(),post.getContent(),user.getNickname()); + } + + @Transactional + public PostResponseDto search(Long postId){ + + Post post= postRepository.getOne(postId); + User user=post.getCreated_user(); + System.out.println(user); + PostResponseDto postResponseDto=PostResponseDto.of(post.getId(),post.getTitle(),post.getContent(),user.getNickname()); + + return postResponseDto; + } + + @Transactional + public List searchByTitle(String title){ + List postList=postRepository.findAllByTitle(title); + + List responseList=new ArrayList(); + + for (Post post:postList + ) { + User user=post.getCreated_user(); + responseList.add(PostResponseDto.of(post.getId(),post.getTitle(),post.getContent(),user.getNickname())); + + } + + return responseList; + } +} diff --git a/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/service/UserService.java b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/service/UserService.java new file mode 100644 index 0000000..74aa319 --- /dev/null +++ b/ThirdSeminar/src/main/java/sopt/org/ThirdSeminar/service/UserService.java @@ -0,0 +1,35 @@ +package sopt.org.ThirdSeminar.service; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import sopt.org.ThirdSeminar.controller.dto.request.UserRequestDto; +import sopt.org.ThirdSeminar.controller.dto.response.UserResponseDto; +import sopt.org.ThirdSeminar.domain.User; +import sopt.org.ThirdSeminar.infrastructure.UserRepository; + +@Service +@RequiredArgsConstructor +public class UserService { + + private final UserRepository userRepository; + + @Transactional + public UserResponseDto create(UserRequestDto request){ + User user= User.builder() + .email(request.getEmail()) + .nickname(request.getNickname()) + .password(request.getPassword()) + .build(); + + userRepository.save(user); + + return UserResponseDto.of(user.getId(), user.getNickname()); + } + + public UserResponseDto search(Long userId){ + User user=userRepository.getOne(userId); + + return UserResponseDto.of(user.getId(),user.getNickname()); + } +} diff --git a/ThirdSeminar/src/main/resources/application.yaml b/ThirdSeminar/src/main/resources/application.yaml new file mode 100644 index 0000000..c56a508 --- /dev/null +++ b/ThirdSeminar/src/main/resources/application.yaml @@ -0,0 +1,21 @@ +spring: + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:3306/sopt_32?useSSL=true&useUnicode=true&serverTimezone=Asia/Seoul + username: sopt_server + password: 1124 + + jpa: + show-sql: true + hibernate: + ddl-auto: create + properties: + hibernate: + format_sql: true + logging: + level: + org: + hibernate: + type: + descriptor: + sql: trace \ No newline at end of file diff --git a/ThirdSeminar/src/test/java/sopt/org/ThirdSeminar/ThirdSeminarApplicationTests.java b/ThirdSeminar/src/test/java/sopt/org/ThirdSeminar/ThirdSeminarApplicationTests.java new file mode 100644 index 0000000..c6d451e --- /dev/null +++ b/ThirdSeminar/src/test/java/sopt/org/ThirdSeminar/ThirdSeminarApplicationTests.java @@ -0,0 +1,13 @@ +package sopt.org.ThirdSeminar; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class ThirdSeminarApplicationTests { + + @Test + void contextLoads() { + } + +}