diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2065bc --- /dev/null +++ b/.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/build.gradle b/build.gradle new file mode 100644 index 0000000..976a03e --- /dev/null +++ b/build.gradle @@ -0,0 +1,46 @@ +plugins { + id 'java' + id 'org.springframework.boot' version '3.0.11' + id 'io.spring.dependency-management' version '1.1.3' //의존성관리 +} + +group = 'com.server.sopt' +version = '0.0.1-SNAPSHOT' + +java { + sourceCompatibility = '17' +} + +configurations { + compileOnly { + extendsFrom annotationProcessor + } +} + +repositories { + mavenCentral() +} + +dependencies { + implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.boot:spring-boot-starter-security' + compileOnly 'org.projectlombok:lombok' + annotationProcessor 'org.projectlombok:lombok' + testImplementation 'org.springframework.boot:spring-boot-starter-test' + + // H2 Database 추가 + runtimeOnly 'com.h2database:h2' + // JPA 추가 + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + + // security + implementation 'org.springframework.boot:spring-boot-starter-security' + testImplementation 'org.springframework.security:spring-security-test' + + // mysql + runtimeOnly 'com.mysql:mysql-connector-j' +} + +tasks.named('test') { + useJUnitPlatform() +} diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..033e24c Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..9f4197d --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100644 index 0000000..fcb6fca --- /dev/null +++ b/gradlew @@ -0,0 +1,248 @@ +#!/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/HEAD/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 + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, 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/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..93e3f59 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,92 @@ +@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=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. +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/settings.gradle b/settings.gradle new file mode 100644 index 0000000..6b30e33 --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'seminar' diff --git a/src/main/java/com/server/sopt/seminar/ServerSeminarApplication.java b/src/main/java/com/server/sopt/seminar/ServerSeminarApplication.java new file mode 100644 index 0000000..026af16 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/ServerSeminarApplication.java @@ -0,0 +1,13 @@ +package com.server.sopt.seminar; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ServerSeminarApplication { + + public static void main(String[] args) { + SpringApplication.run(ServerSeminarApplication.class, args); + } + +} diff --git a/src/main/java/com/server/sopt/seminar/authentification/CustomAccessDeniedHandler.java b/src/main/java/com/server/sopt/seminar/authentification/CustomAccessDeniedHandler.java new file mode 100644 index 0000000..e6ade2a --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/authentification/CustomAccessDeniedHandler.java @@ -0,0 +1,2 @@ +package com.server.sopt.seminar.authentification;public class CustomAccessDeniedHandler { +} diff --git a/src/main/java/com/server/sopt/seminar/authentification/CustomJwtAuthenticationEntryPoint.java b/src/main/java/com/server/sopt/seminar/authentification/CustomJwtAuthenticationEntryPoint.java new file mode 100644 index 0000000..2bc927c --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/authentification/CustomJwtAuthenticationEntryPoint.java @@ -0,0 +1,2 @@ +package com.server.sopt.seminar.authentification;public class CustomJwtAuthenticationEntryPoint { +} diff --git a/src/main/java/com/server/sopt/seminar/authentification/UserAuthentication.java b/src/main/java/com/server/sopt/seminar/authentification/UserAuthentication.java new file mode 100644 index 0000000..0c4f6dc --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/authentification/UserAuthentication.java @@ -0,0 +1,2 @@ +package com.server.sopt.seminar.authentification;public class UserAuthentication { +} diff --git a/src/main/java/com/server/sopt/seminar/common/config/AWSConfig.java b/src/main/java/com/server/sopt/seminar/common/config/AWSConfig.java new file mode 100644 index 0000000..dee6f92 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/common/config/AWSConfig.java @@ -0,0 +1,50 @@ +package com.server.sopt.seminar.common.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import software.amazon.awssdk.auth.credentials.SystemPropertyCredentialsProvider; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.s3.S3Client; + + + +@Configuration +public class AWSConfig { + private static final String AWS_ACCESS_KEY_ID = "aws.accessKeyId"; + private static final String AWS_SECRET_ACCESS_KEY = "aws.secretAccessKey"; + + private final String accessKey; + private final String secretKey; + private final String regionString; + + public AWSConfig(@Value("${aws-property.access-key}") final String accessKey, + @Value("${aws-property.secret-key}") final String secretKey, + @Value("${aws-property.aws-region}") final String regionString) { + this.accessKey = accessKey; + this.secretKey = secretKey; + this.regionString = regionString; + } + + + //자격 증명을 얻는 방법 + @Bean + public SystemPropertyCredentialsProvider systemPropertyCredentialsProvider() { + System.setProperty(AWS_ACCESS_KEY_ID, accessKey); + System.setProperty(AWS_SECRET_ACCESS_KEY, secretKey); + return SystemPropertyCredentialsProvider.create(); + } + + @Bean + public Region getRegion() { + return Region.of(regionString); + } + + @Bean + S3Client getS3Client() { + return S3Client.builder() + .region(getRegion()) + .credentialsProvider(systemPropertyCredentialsProvider()) + .build(); + } +} \ No newline at end of file diff --git a/src/main/java/com/server/sopt/seminar/common/config/BCryptPasswordConfig.java b/src/main/java/com/server/sopt/seminar/common/config/BCryptPasswordConfig.java new file mode 100644 index 0000000..28d60a0 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/common/config/BCryptPasswordConfig.java @@ -0,0 +1,2 @@ +package com.server.sopt.seminar.common.config;public class BCryptPasswordConfig { +} diff --git a/src/main/java/com/server/sopt/seminar/common/config/SecurityConfig.java b/src/main/java/com/server/sopt/seminar/common/config/SecurityConfig.java new file mode 100644 index 0000000..e72415b --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/common/config/SecurityConfig.java @@ -0,0 +1,2 @@ +package com.server.sopt.seminar.common.config;public class SecurityConfig { +} diff --git a/src/main/java/com/server/sopt/seminar/common/exception/BusinessException.java b/src/main/java/com/server/sopt/seminar/common/exception/BusinessException.java new file mode 100644 index 0000000..ed9cff1 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/common/exception/BusinessException.java @@ -0,0 +1,7 @@ +package com.server.sopt.seminar.common.config.exception; + +public class BusinessException extends RuntimeException { + public BusinessException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/src/main/java/com/server/sopt/seminar/controller/CategoryController.java b/src/main/java/com/server/sopt/seminar/controller/CategoryController.java new file mode 100644 index 0000000..9eeb846 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/controller/CategoryController.java @@ -0,0 +1,23 @@ +package com.server.sopt.seminar.controller; + +import com.server.sopt.seminar.dto.response.category.CategoryResponse; +import com.server.sopt.seminar.service.CategoryService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/categories") +public class CategoryController { + + private final CategoryService categoryService; + + @GetMapping("/{categoryId}") + public ResponseEntity getCategoryById(@PathVariable Short categoryId) { + return ResponseEntity.ok(categoryService.getById(categoryId)); + } +} \ No newline at end of file diff --git a/src/main/java/com/server/sopt/seminar/controller/HealthCheckController.java b/src/main/java/com/server/sopt/seminar/controller/HealthCheckController.java new file mode 100644 index 0000000..5157fb0 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/controller/HealthCheckController.java @@ -0,0 +1,47 @@ +package com.server.sopt.seminar.controller; + +import com.server.sopt.seminar.dto.HealthCheckResponse; +import com.server.sopt.seminar.sample.Person; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.HashMap; +import java.util.Map; + +@RestController +@RequestMapping("/health") +public class HealthCheckController { + @GetMapping("/v1") + public Map healthCheck(){ + Map response = new HashMap<>(); + response.put("status", "OK"); + return response; + } + + @GetMapping("/v2") + public ResponseEntity healthCheck2(){ + return ResponseEntity.ok("OK"); + } + + @GetMapping("/v3") + public String healthcheck3(){ + //Person person = new Person(); + //Person person = Person.builder().build(); + return "ok"; + } + + @GetMapping("/v4") + public ResponseEntity> healthcheck4(){ + Map response = new HashMap<>(); + response.put("status", "OK"); + return ResponseEntity.ok(response); + } + + @GetMapping("/v5") + public ResponseEntity healthcheck5(){ + //return ResponseEntity.ok(new HealthCheckResponse("OK")); + return ResponseEntity.ok((new HealthCheckResponse())); + } +} diff --git a/src/main/java/com/server/sopt/seminar/controller/PostController.java b/src/main/java/com/server/sopt/seminar/controller/PostController.java new file mode 100644 index 0000000..5d3d3ce --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/controller/PostController.java @@ -0,0 +1,49 @@ +package com.server.sopt.seminar.controller; + +import com.server.sopt.seminar.dto.request.post.PostCreateRequest; +import com.server.sopt.seminar.dto.request.post.PostUpdateRequest; +import com.server.sopt.seminar.dto.response.post.PostGetResponse; +import com.server.sopt.seminar.service.PostService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.net.URI; +import java.util.List; + +@RestController +@RequestMapping("/api/posts") +@RequiredArgsConstructor +public class PostController { + private static final String CUSTOM_AUTH_ID = "X-Auth-Id"; + + private final PostService postService; + + @GetMapping("{postId}") + public ResponseEntity getPostById(@PathVariable Long postId) { + return ResponseEntity.ok(postService.getById(postId)); + } + + @GetMapping + public ResponseEntity> getPosts(@RequestHeader(CUSTOM_AUTH_ID) Long memberId) { + return ResponseEntity.ok(postService.getPosts(memberId)); + } + + @PostMapping + public ResponseEntity createPost(@RequestHeader(CUSTOM_AUTH_ID) Long memberId, @RequestBody PostCreateRequest request) { + URI location = URI.create("/api/post/" + postService.create(request, memberId)); + return ResponseEntity.created(location).build(); + } + + @PatchMapping("{postId}") + public ResponseEntity updatePost(@PathVariable Long postId, @RequestBody PostUpdateRequest request) { + postService.editContent(postId, request); + return ResponseEntity.noContent().build(); + } + + @DeleteMapping("{postId}") + public ResponseEntity deletePost(@PathVariable Long postId) { + postService.deleteById(postId); + return ResponseEntity.noContent().build(); + } +} diff --git a/src/main/java/com/server/sopt/seminar/controller/PostControllerV2.java b/src/main/java/com/server/sopt/seminar/controller/PostControllerV2.java new file mode 100644 index 0000000..dd54079 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/controller/PostControllerV2.java @@ -0,0 +1,31 @@ +package com.server.sopt.seminar.controller; + +import com.server.sopt.seminar.dto.request.post.PostCreateRequest; +import com.server.sopt.seminar.service.PostServiceV2; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import java.net.URI; + +@RestController +@RequestMapping("/api/v2/posts") +@RequiredArgsConstructor +public class PostControllerV2 { + + private static final String CUSTOM_AUTH_ID = "X-Auth-Id"; + private final PostServiceV2 postService; + + @PostMapping + public ResponseEntity createPostV2(@RequestHeader(CUSTOM_AUTH_ID) Long memberId, @RequestPart MultipartFile image, PostCreateRequest request) { + URI location = URI.create("/api/posts/v2" + postService.createV2(request, image, memberId)); + return ResponseEntity.created(location).build(); + } + + @DeleteMapping("{postId}") + public ResponseEntity deletePost(@PathVariable Long postId) { + postService.deleteByIdV2(postId); + return ResponseEntity.noContent().build(); + } +} \ No newline at end of file diff --git a/src/main/java/com/server/sopt/seminar/controller/ServiceMemberController.java b/src/main/java/com/server/sopt/seminar/controller/ServiceMemberController.java new file mode 100644 index 0000000..d670f79 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/controller/ServiceMemberController.java @@ -0,0 +1,2 @@ +package com.server.sopt.seminar.controller;public class ServiceMemberController { +} diff --git a/src/main/java/com/server/sopt/seminar/domain/BaseTimeEntity.java b/src/main/java/com/server/sopt/seminar/domain/BaseTimeEntity.java new file mode 100644 index 0000000..2f87f3b --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/domain/BaseTimeEntity.java @@ -0,0 +1,20 @@ +package com.server.sopt.seminar.domain; + +import jakarta.persistence.EntityListeners; +import jakarta.persistence.MappedSuperclass; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +import java.time.LocalDateTime; + +@MappedSuperclass +@EntityListeners(AuditingEntityListener.class) +public abstract class BaseTimeEntity { + + @CreatedDate + private LocalDateTime createdAt; + + @LastModifiedDate + private LocalDateTime updatedAt; +} \ No newline at end of file diff --git a/src/main/java/com/server/sopt/seminar/domain/Category.java b/src/main/java/com/server/sopt/seminar/domain/Category.java new file mode 100644 index 0000000..9d3bd10 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/domain/Category.java @@ -0,0 +1,20 @@ +package com.server.sopt.seminar.domain; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import static lombok.AccessLevel.PROTECTED; + +@Entity +@Getter +@NoArgsConstructor(access = PROTECTED) +public class Category { + + @Id @GeneratedValue + private Short id; + + private String content; +} \ No newline at end of file diff --git a/src/main/java/com/server/sopt/seminar/domain/CategoryId.java b/src/main/java/com/server/sopt/seminar/domain/CategoryId.java new file mode 100644 index 0000000..70f8aaf --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/domain/CategoryId.java @@ -0,0 +1,15 @@ +package com.server.sopt.seminar.domain; + +import jakarta.persistence.Embeddable; +import lombok.EqualsAndHashCode; +import lombok.Getter; + +import java.io.Serializable; + +@Embeddable +@Getter +@EqualsAndHashCode +public class CategoryId implements Serializable { + + private String categoryId; +} diff --git a/src/main/java/com/server/sopt/seminar/domain/Member.java b/src/main/java/com/server/sopt/seminar/domain/Member.java new file mode 100644 index 0000000..27b52d3 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/domain/Member.java @@ -0,0 +1,40 @@ +package com.server.sopt.seminar.domain; + +import jakarta.persistence.*; +import lombok.AccessLevel; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.ArrayList; +import java.util.List; + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class Member extends BaseTimeEntity{ + + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + private String name; + private String nickname; + private int age; + + @Embedded + private SOPT sopt; + + @OneToMany(mappedBy = "member", cascade = CascadeType.ALL) + private final List posts = new ArrayList<>(); + + @Builder + public Member(String name, String nickname, int age, SOPT sopt) { + this.name = name; + this.nickname = nickname; + this.age = age; + this.sopt = sopt; + } + + public void updateSOPT(SOPT sopt) { + this.sopt = sopt; + } +} \ No newline at end of file diff --git a/src/main/java/com/server/sopt/seminar/domain/Part.java b/src/main/java/com/server/sopt/seminar/domain/Part.java new file mode 100644 index 0000000..18fe1c0 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/domain/Part.java @@ -0,0 +1,16 @@ +package com.server.sopt.seminar.domain; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public enum Part { + SERVER("서버"), + WEB("웹"), + ANDROID("안드로이드"), + IOS("iOS"), + PLAN("기획"), + DESIGN("디자인"); + + private final String name; +} diff --git a/src/main/java/com/server/sopt/seminar/domain/Post.java b/src/main/java/com/server/sopt/seminar/domain/Post.java new file mode 100644 index 0000000..128d112 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/domain/Post.java @@ -0,0 +1,41 @@ +package com.server.sopt.seminar.domain; +import jakarta.persistence.*; +import lombok.AccessLevel; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Entity +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@Getter +@Table(name = "post") +public class Post extends BaseTimeEntity { + + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String title; + private String content; + + @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) + @JoinColumn(name = "member_id") + private Member member; + + @Column(name = "category_id") + private CategoryId categoryId; + + @Builder + public Post(String title, String content, Member member) { + this.title = title; + this.content = content; + this.member = member; + } + + public void updateContent(String content) { + this.content = content; + } + + public void addCategory(CategoryId categoryId) { + this.categoryId = categoryId; + } +} \ No newline at end of file diff --git a/src/main/java/com/server/sopt/seminar/domain/SOPT.java b/src/main/java/com/server/sopt/seminar/domain/SOPT.java new file mode 100644 index 0000000..3d84ba8 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/domain/SOPT.java @@ -0,0 +1,20 @@ +package com.server.sopt.seminar.domain; +import jakarta.persistence.Embeddable; +import jakarta.persistence.Enumerated; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import static jakarta.persistence.EnumType.*; + +@Embeddable +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class SOPT { + private int generation; + + @Enumerated(STRING) + private Part part; +} diff --git a/src/main/java/com/server/sopt/seminar/domain/ServiceMember.java b/src/main/java/com/server/sopt/seminar/domain/ServiceMember.java new file mode 100644 index 0000000..b72edd3 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/domain/ServiceMember.java @@ -0,0 +1,2 @@ +package com.server.sopt.seminar.domain;public class ServiceMember { +} diff --git a/src/main/java/com/server/sopt/seminar/dto/HealthCheckResponse.java b/src/main/java/com/server/sopt/seminar/dto/HealthCheckResponse.java new file mode 100644 index 0000000..0514ada --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/dto/HealthCheckResponse.java @@ -0,0 +1,13 @@ +package com.server.sopt.seminar.dto; + +public class HealthCheckResponse { + //private String status; + private static final String STATUS = "OK"; + public String status; + + public HealthCheckResponse() { + //this.status = status; + this.status = STATUS; + } + +} diff --git a/src/main/java/com/server/sopt/seminar/dto/request/member/MemberCreateRequest.java b/src/main/java/com/server/sopt/seminar/dto/request/member/MemberCreateRequest.java new file mode 100644 index 0000000..ca9ec0b --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/dto/request/member/MemberCreateRequest.java @@ -0,0 +1,12 @@ +package com.server.sopt.seminar.dto.request.member; + +import com.server.sopt.seminar.domain.SOPT; +import lombok.Data; + +@Data +public class MemberCreateRequest { + private String name; + private String nickname; + private int age; + private SOPT sopt; +} \ No newline at end of file diff --git a/src/main/java/com/server/sopt/seminar/dto/request/member/MemberProfileUpdateRequest.java b/src/main/java/com/server/sopt/seminar/dto/request/member/MemberProfileUpdateRequest.java new file mode 100644 index 0000000..48e80e3 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/dto/request/member/MemberProfileUpdateRequest.java @@ -0,0 +1,9 @@ +package com.server.sopt.seminar.dto.request.member; +import com.server.sopt.seminar.domain.Part; +import lombok.Data; + +@Data +public class MemberProfileUpdateRequest { + private int generation; + private Part part; +} \ No newline at end of file diff --git a/src/main/java/com/server/sopt/seminar/dto/request/post/PostCreateRequest.java b/src/main/java/com/server/sopt/seminar/dto/request/post/PostCreateRequest.java new file mode 100644 index 0000000..b07d2ac --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/dto/request/post/PostCreateRequest.java @@ -0,0 +1,8 @@ +package com.server.sopt.seminar.dto.request.post; + + +public record PostCreateRequest( + String title, + String content +) { +} \ No newline at end of file diff --git a/src/main/java/com/server/sopt/seminar/dto/request/post/PostUpdateRequest.java b/src/main/java/com/server/sopt/seminar/dto/request/post/PostUpdateRequest.java new file mode 100644 index 0000000..d4c1f0e --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/dto/request/post/PostUpdateRequest.java @@ -0,0 +1,6 @@ +package com.server.sopt.seminar.dto.request.post; + +public record PostUpdateRequest( + String content +) { +} \ No newline at end of file diff --git a/src/main/java/com/server/sopt/seminar/dto/request/post/ServiceMemberRequest.java b/src/main/java/com/server/sopt/seminar/dto/request/post/ServiceMemberRequest.java new file mode 100644 index 0000000..3e23426 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/dto/request/post/ServiceMemberRequest.java @@ -0,0 +1,2 @@ +package com.server.sopt.seminar.dto.request.post;public class ServiceMemberRequest { +} diff --git a/src/main/java/com/server/sopt/seminar/dto/request/servicemember/ServiceMemberRequest.java b/src/main/java/com/server/sopt/seminar/dto/request/servicemember/ServiceMemberRequest.java new file mode 100644 index 0000000..419dc50 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/dto/request/servicemember/ServiceMemberRequest.java @@ -0,0 +1,2 @@ +package com.server.sopt.seminar.dto.request.servicemember;public class ServiceMemberRequest { +} diff --git a/src/main/java/com/server/sopt/seminar/dto/response/MemberGetResponse.java b/src/main/java/com/server/sopt/seminar/dto/response/MemberGetResponse.java new file mode 100644 index 0000000..15ab7d8 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/dto/response/MemberGetResponse.java @@ -0,0 +1,20 @@ +package com.server.sopt.seminar.dto.response; + +import com.server.sopt.seminar.domain.Member; +import com.server.sopt.seminar.domain.SOPT; + +public record MemberGetResponse( + String name, + String nickname, + int age, + SOPT soptInfo +) { + public static MemberGetResponse of(Member member) { + return new MemberGetResponse( + member.getName(), + member.getNickname(), + member.getAge(), + member.getSopt() + ); + } +} diff --git a/src/main/java/com/server/sopt/seminar/dto/response/category/CategoryResponse.java b/src/main/java/com/server/sopt/seminar/dto/response/category/CategoryResponse.java new file mode 100644 index 0000000..6004984 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/dto/response/category/CategoryResponse.java @@ -0,0 +1,16 @@ +package com.server.sopt.seminar.dto.response.category; +import com.server.sopt.seminar.domain.Category; +import lombok.Data; + +@Data +public class CategoryResponse { + private String content; + + public CategoryResponse(String content) { + this.content = content; + } + + public static CategoryResponse of(Category category) { + return new CategoryResponse(category.getContent()); + } +} \ No newline at end of file diff --git a/src/main/java/com/server/sopt/seminar/dto/response/post/PostGetResponse.java b/src/main/java/com/server/sopt/seminar/dto/response/post/PostGetResponse.java new file mode 100644 index 0000000..3c467ae --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/dto/response/post/PostGetResponse.java @@ -0,0 +1,20 @@ +package com.server.sopt.seminar.dto.response.post; + +import com.server.sopt.seminar.domain.Category; +import com.server.sopt.seminar.domain.Post; + +public record PostGetResponse( + Long id, + String title, + String content, + String category +) { + public static PostGetResponse of(Post post, Category category) { + return new PostGetResponse( + post.getId(), + post.getTitle(), + post.getContent(), + category.getContent() + ); + } +} diff --git a/src/main/java/com/server/sopt/seminar/external/S3/PreSignedUrlVO.java b/src/main/java/com/server/sopt/seminar/external/S3/PreSignedUrlVO.java new file mode 100644 index 0000000..1f09ed2 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/external/S3/PreSignedUrlVO.java @@ -0,0 +1,2 @@ +package com.server.sopt.seminar.external.S3;public class PreSignedUrlVO { +} diff --git a/src/main/java/com/server/sopt/seminar/external/S3/S3Service.java b/src/main/java/com/server/sopt/seminar/external/S3/S3Service.java new file mode 100644 index 0000000..8788d35 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/external/S3/S3Service.java @@ -0,0 +1,2 @@ +package com.server.sopt.seminar.external.S3;public class S3Service { +} diff --git a/src/main/java/com/server/sopt/seminar/filter/JwtAuthenticationFilter.java b/src/main/java/com/server/sopt/seminar/filter/JwtAuthenticationFilter.java new file mode 100644 index 0000000..992b0c2 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/filter/JwtAuthenticationFilter.java @@ -0,0 +1,2 @@ +package com.server.sopt.seminar.filter;public class JwtAuthenticationFilter { +} diff --git a/src/main/java/com/server/sopt/seminar/filter/JwtTokenProvider.java b/src/main/java/com/server/sopt/seminar/filter/JwtTokenProvider.java new file mode 100644 index 0000000..862873f --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/filter/JwtTokenProvider.java @@ -0,0 +1,2 @@ +package com.server.sopt.seminar.filter;public class JwtTokenProvider { +} diff --git a/src/main/java/com/server/sopt/seminar/filter/JwtValidationType.java b/src/main/java/com/server/sopt/seminar/filter/JwtValidationType.java new file mode 100644 index 0000000..4428324 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/filter/JwtValidationType.java @@ -0,0 +1,2 @@ +package com.server.sopt.seminar.filter;public enum JwtValidationType { +} diff --git a/src/main/java/com/server/sopt/seminar/repository/CategoryJpaRepository.java b/src/main/java/com/server/sopt/seminar/repository/CategoryJpaRepository.java new file mode 100644 index 0000000..0312d78 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/repository/CategoryJpaRepository.java @@ -0,0 +1,7 @@ +package com.server.sopt.seminar.repository; + +import com.server.sopt.seminar.domain.Category; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface CategoryJpaRepository extends JpaRepository { +} \ No newline at end of file diff --git a/src/main/java/com/server/sopt/seminar/repository/MemberJpaRepository.java b/src/main/java/com/server/sopt/seminar/repository/MemberJpaRepository.java new file mode 100644 index 0000000..ac8422b --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/repository/MemberJpaRepository.java @@ -0,0 +1,13 @@ +package com.server.sopt.seminar.repository; + +import com.server.sopt.seminar.domain.Member; +import jakarta.persistence.EntityNotFoundException; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface MemberJpaRepository extends JpaRepository { + + default Member findByIdOrThrow(Long id) { + return findById(id).orElseThrow( + () -> new EntityNotFoundException("존재하지 않는 회원입니다.")); + } +} diff --git a/src/main/java/com/server/sopt/seminar/repository/PostJpaRepository.java b/src/main/java/com/server/sopt/seminar/repository/PostJpaRepository.java new file mode 100644 index 0000000..7c2aeb9 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/repository/PostJpaRepository.java @@ -0,0 +1,14 @@ +package com.server.sopt.seminar.repository; + +import com.server.sopt.seminar.domain.Member; +import com.server.sopt.seminar.domain.Post; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface PostJpaRepository extends JpaRepository { + + List findAllByMemberId(Long memberId); + List findAllByMember(Member member); + +} diff --git a/src/main/java/com/server/sopt/seminar/repository/ServiceMemberJpaRepository.java b/src/main/java/com/server/sopt/seminar/repository/ServiceMemberJpaRepository.java new file mode 100644 index 0000000..aa76695 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/repository/ServiceMemberJpaRepository.java @@ -0,0 +1,2 @@ +package com.server.sopt.seminar.repository;public class ServiceMemberJpaRepository { +} diff --git a/src/main/java/com/server/sopt/seminar/sample/Person.java b/src/main/java/com/server/sopt/seminar/sample/Person.java new file mode 100644 index 0000000..2f482cf --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/sample/Person.java @@ -0,0 +1,20 @@ +package com.server.sopt.seminar.sample; + +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@NoArgsConstructor +public class Person { + private String lastName; + private String firstName; + + @Builder + public Person(String lastName, String firstName) { + this.lastName = lastName; + this.firstName = firstName; + } +} diff --git a/src/main/java/com/server/sopt/seminar/service/CategoryService.java b/src/main/java/com/server/sopt/seminar/service/CategoryService.java new file mode 100644 index 0000000..54bc9b9 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/service/CategoryService.java @@ -0,0 +1,27 @@ +package com.server.sopt.seminar.service; +import com.server.sopt.seminar.domain.Category; +import com.server.sopt.seminar.domain.CategoryId; +import com.server.sopt.seminar.dto.response.category.CategoryResponse; +import com.server.sopt.seminar.repository.CategoryJpaRepository; +import jakarta.persistence.EntityNotFoundException; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +@Transactional(readOnly = true) +public class CategoryService { + + private final CategoryJpaRepository categoryJpaRepository; + + public Category getByCategoryId(CategoryId categoryId) { + return categoryJpaRepository.findById(Short.valueOf(categoryId.getCategoryId())).orElseThrow( + () -> new EntityNotFoundException("해당하는 카테고리가 없습니다.")); + } + + public CategoryResponse getById(Short id) { + return CategoryResponse.of(categoryJpaRepository.findById(id).orElseThrow( + () -> new EntityNotFoundException("해당하는 카테고리가 없습니다."))); + } +} \ No newline at end of file diff --git a/src/main/java/com/server/sopt/seminar/service/MemberService.java b/src/main/java/com/server/sopt/seminar/service/MemberService.java new file mode 100644 index 0000000..160a659 --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/service/MemberService.java @@ -0,0 +1,65 @@ +package com.server.sopt.seminar.service; +import com.server.sopt.seminar.domain.Member; +import com.server.sopt.seminar.domain.SOPT; +import com.server.sopt.seminar.dto.request.member.MemberCreateRequest; +import com.server.sopt.seminar.dto.request.member.MemberProfileUpdateRequest; +import com.server.sopt.seminar.dto.response.MemberGetResponse; +import com.server.sopt.seminar.repository.MemberJpaRepository; +import jakarta.persistence.EntityNotFoundException; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.stream.Collectors; + +@Service +@RequiredArgsConstructor +@Transactional(readOnly = true) +public class MemberService { + + private final MemberJpaRepository memberJpaRepository; + + @Transactional + public String create(MemberCreateRequest request) { + Member member = memberJpaRepository.save(Member.builder() + .name(request.getName()) + .nickname(request.getNickname()) + .age(request.getAge()) + .sopt(request.getSopt()) + .build()); + return member.getId().toString(); + } + + @Transactional + public void updateSOPT(Long memberId, MemberProfileUpdateRequest request) { + Member member = memberJpaRepository.findByIdOrThrow(memberId); + member.updateSOPT(new SOPT(request.getGeneration(), request.getPart())); + } + + public MemberGetResponse getMemberByIdV1(Long id) { + Member member = memberJpaRepository.findById(id).get(); + return MemberGetResponse.of(member); + } + public MemberGetResponse getMemberByIdV2(Long id) { + return MemberGetResponse.of(memberJpaRepository.findById(id).orElseThrow( + () -> new EntityNotFoundException("존재하지 않는 회원입니다."))); + } + + public MemberGetResponse getMemberByIdV3(Long id) { + return MemberGetResponse.of(memberJpaRepository.findByIdOrThrow(id)); + } + + public List getMembers() { + return memberJpaRepository.findAll() + .stream() + .map(MemberGetResponse::of) + .collect(Collectors.toList()); + } + + @Transactional + public void deleteMember(Long memberId) { + Member member = memberJpaRepository.findByIdOrThrow(memberId); + memberJpaRepository.delete(member); + } +} \ No newline at end of file diff --git a/src/main/java/com/server/sopt/seminar/service/PostService.java b/src/main/java/com/server/sopt/seminar/service/PostService.java new file mode 100644 index 0000000..1836dcc --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/service/PostService.java @@ -0,0 +1,66 @@ +package com.server.sopt.seminar.service; + +import com.server.sopt.seminar.domain.Category; +import com.server.sopt.seminar.domain.Member; +import com.server.sopt.seminar.domain.Post; +import com.server.sopt.seminar.dto.request.post.PostCreateRequest; +import com.server.sopt.seminar.dto.request.post.PostUpdateRequest; +import com.server.sopt.seminar.dto.response.post.PostGetResponse; +import com.server.sopt.seminar.repository.MemberJpaRepository; +import com.server.sopt.seminar.repository.PostJpaRepository; +import jakarta.persistence.EntityNotFoundException; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@Service +@RequiredArgsConstructor +@Transactional(readOnly = true) +public class PostService { + + private final PostJpaRepository postJpaRepository; + private final MemberJpaRepository memberJpaRepository; + private final CategoryService categoryService; + + @Transactional + public String create(PostCreateRequest request, Long memberId) { + Member member = memberJpaRepository.findByIdOrThrow(memberId); + Post post = postJpaRepository.save( + Post.builder() + .member(member) + .title(request.title()) + .content(request.content()).build()); + return post.getId().toString(); + } + + @Transactional + public void editContent(Long postId, PostUpdateRequest request) { + Post post = postJpaRepository.findById(postId) + .orElseThrow(() -> new EntityNotFoundException("해당하는 게시글이 없습니다.")); + post.updateContent(request.content()); + } + + public List getPosts(Long memberId) { + return postJpaRepository.findAllByMemberId(memberId) + .stream() + .map(post -> PostGetResponse.of(post, getCategoryByPost(post))) + .toList(); + } + + public PostGetResponse getById(Long postId) { + Post post = postJpaRepository.findById(postId).orElseThrow(() -> new EntityNotFoundException("해당하는 게시글이 없습니다.")); + return PostGetResponse.of(post, getCategoryByPost(post)); + } + + @Transactional + public void deleteById(Long postId) { + Post post = postJpaRepository.findById(postId).orElseThrow(() -> new EntityNotFoundException("해당하는 게시글이 없습니다.")); + postJpaRepository.delete(post); + } + + private Category getCategoryByPost(Post post) { + return categoryService.getByCategoryId(post.getCategoryId()); + } +} diff --git a/src/main/java/com/server/sopt/seminar/service/PostServiceV2.java b/src/main/java/com/server/sopt/seminar/service/PostServiceV2.java new file mode 100644 index 0000000..600acce --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/service/PostServiceV2.java @@ -0,0 +1,2 @@ +package com.server.sopt.seminar.service;public class PostServiceV2 { +} diff --git a/src/main/java/com/server/sopt/seminar/service/ServiceMemberService.java b/src/main/java/com/server/sopt/seminar/service/ServiceMemberService.java new file mode 100644 index 0000000..28bd1ef --- /dev/null +++ b/src/main/java/com/server/sopt/seminar/service/ServiceMemberService.java @@ -0,0 +1,2 @@ +package com.server.sopt.seminar.service;public class ServiceMemberService { +} diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql new file mode 100644 index 0000000..ca23e86 --- /dev/null +++ b/src/main/resources/data.sql @@ -0,0 +1,20 @@ +INSERT INTO MEMBER(ID, NICKNAME, NAME, AGE, PART, GENERATION) +VALUES (1, '유난', '최윤한', 20, 'SERVER', 33), + (2, 'DDD', '정준서', 24, 'SERVER', 33), + (3, '멜로니', '김해린', 26, 'PLAN', 33), + (4, '티벳여우', '이태희', 25, 'ANDROID', 33) +; + +INSERT INTO POST(ID, TITLE, CONTENT, MEMBER_ID, CATEGORY_ID) +VALUES + (1, '안녕하세요 제목1', '안녕하세요 내용1', 1, 1), + (2, '안녕하세요 제목2', '안녕하세요 내용2', 1, 1), + (3, '안녕하세요 제목3', '안녕하세요 내용3', 1, 1), + (4, '반갑습니다 제목4', '반갑습니다', 2, 2), + (5, '안녕하세요 제목5', '안녕하세요', 3, 3); + +INSERT INTO CATEGORY(ID, CONTENT) +VALUES + (1, '일상'), + (2, '여행'), + (3, '공부'); \ No newline at end of file diff --git a/src/test/java/com/server/sopt/seminar/ServerSeminarApplicationTests.java b/src/test/java/com/server/sopt/seminar/ServerSeminarApplicationTests.java new file mode 100644 index 0000000..b97de86 --- /dev/null +++ b/src/test/java/com/server/sopt/seminar/ServerSeminarApplicationTests.java @@ -0,0 +1,13 @@ +package com.server.sopt.seminar; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class ServerSeminarApplicationTests { + + @Test + void contextLoads() { + } + +}