From caf15640606ce427aa3179b590798660fabc64b2 Mon Sep 17 00:00:00 2001 From: "[Github" <[Github> Date: Mon, 6 May 2024 12:29:34 +0900 Subject: [PATCH 1/3] deployment_scp.yml --- .github/workflows/deployment_scp.yml | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/.github/workflows/deployment_scp.yml b/.github/workflows/deployment_scp.yml index 8d91ebb..9bd7a1c 100644 --- a/.github/workflows/deployment_scp.yml +++ b/.github/workflows/deployment_scp.yml @@ -46,10 +46,10 @@ jobs: - name: Send jar to remote server uses: appleboy/scp-action@master with: - host: {나의 public ip} - username: {나의 userName} + host: 192.168.219.105 + username: leejieun0000 source: "devlog_springboot-0.0.1-SNAPSHOT.jar" - target: "/home/{나의 userName}" + target: "/home/leejieun0000" key: ${{ secrets.PRIVATE_KEY }} run-app: @@ -61,16 +61,16 @@ jobs: - name: Move deploy.sh uses: appleboy/scp-action@master with: - host: {나의 public ip} - username: {나의 userName} + host: 192.168.219.105 + username: leejieun0000 source: "deploy.sh" - target: "/home/{나의 userName}" + target: "/home/leejieun0000" key: ${{ secrets.PRIVATE_KEY }} - name: Execute script uses: appleboy/ssh-action@master with: - username: {나의 userName} - host: {나의 public ip} + username: leejieun0000 + host: 192.168.219.105 key: ${{ secrets.PRIVATE_KEY }} script_stop: true - script: cd /home/{나의 userName} && chmod +x deploy.sh && ./deploy.sh + script: cd /home/leejieun0000 && chmod +x deploy.sh && ./deploy.sh From d20f09ccfb8f1bf043e5647d764a33304ec080c7 Mon Sep 17 00:00:00 2001 From: "[Github" <[Github> Date: Mon, 6 May 2024 15:54:04 +0900 Subject: [PATCH 2/3] project --- .../controller/PostController.java | 19 +++--- .../controller/ProjectController.java | 54 +++++++++++++++++ .../exception/DevlogException.java | 11 ++++ .../exception/GlobalExceptionHandler.java | 26 ++++++++ .../model/response/ErrorResponse.java | 8 +++ .../repository/PostRepository.java | 4 ++ .../repository/ProjectRepository.java | 9 +++ .../service/PostService.java | 22 ++++--- .../service/ProjectService.java | 60 +++++++++++++++++++ .../cnu/devlog_springboot/type/ErrorCode.java | 30 ++++++++++ 10 files changed, 227 insertions(+), 16 deletions(-) create mode 100644 src/main/java/com/com/cnu/devlog_springboot/controller/ProjectController.java create mode 100644 src/main/java/com/com/cnu/devlog_springboot/exception/DevlogException.java create mode 100644 src/main/java/com/com/cnu/devlog_springboot/exception/GlobalExceptionHandler.java create mode 100644 src/main/java/com/com/cnu/devlog_springboot/model/response/ErrorResponse.java create mode 100644 src/main/java/com/com/cnu/devlog_springboot/repository/ProjectRepository.java create mode 100644 src/main/java/com/com/cnu/devlog_springboot/service/ProjectService.java create mode 100644 src/main/java/com/com/cnu/devlog_springboot/type/ErrorCode.java diff --git a/src/main/java/com/com/cnu/devlog_springboot/controller/PostController.java b/src/main/java/com/com/cnu/devlog_springboot/controller/PostController.java index 7d6847f..b9e7af3 100644 --- a/src/main/java/com/com/cnu/devlog_springboot/controller/PostController.java +++ b/src/main/java/com/com/cnu/devlog_springboot/controller/PostController.java @@ -3,6 +3,8 @@ import com.com.cnu.devlog_springboot.model.Post; import com.com.cnu.devlog_springboot.model.request.PostRequest; import com.com.cnu.devlog_springboot.service.PostService; +import com.com.cnu.devlog_springboot.type.Tag; +import io.micrometer.common.lang.Nullable; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -17,34 +19,33 @@ public class PostController { // GET /posts @GetMapping - public ResponseEntity> getPosts() { - return ResponseEntity.ok(postService.getPosts()); + public ResponseEntity> getPosts(@RequestParam("tag") @Nullable Tag tag) { + return ResponseEntity.ok(postService.getPosts(tag)); } // GET /posts/{postId} - @GetMapping("{postId}") - public ResponseEntity getPost(@PathVariable("postId")Integer postId) { + @GetMapping("/{postId}") + public ResponseEntity getPost(@PathVariable("postId") Integer postId) { return ResponseEntity.ok(postService.getPost(postId)); } // POST /posts @PostMapping public ResponseEntity createPost(@RequestBody PostRequest postRequest) { - return ResponseEntity.ok(postService.creatPost(postRequest)); + return ResponseEntity.ok(postService.createPost(postRequest)); } // PUT /posts/{postId} - // ex. localhost:8080/posts/3 - @PutMapping("{postId}") + @PutMapping("/{postId}") public ResponseEntity updatePost( - @PathVariable("postId")Integer postId, + @PathVariable("postId") Integer postId, @RequestBody PostRequest postRequest ) { return ResponseEntity.ok(postService.updatePost(postId, postRequest)); } // DELETE /posts/{postId} - @DeleteMapping("{postId}") + @DeleteMapping("/{postId}") public ResponseEntity deletePost(@PathVariable("postId") Integer postId) { postService.deletePost(postId); return ResponseEntity.noContent().build(); diff --git a/src/main/java/com/com/cnu/devlog_springboot/controller/ProjectController.java b/src/main/java/com/com/cnu/devlog_springboot/controller/ProjectController.java new file mode 100644 index 0000000..69bb4c4 --- /dev/null +++ b/src/main/java/com/com/cnu/devlog_springboot/controller/ProjectController.java @@ -0,0 +1,54 @@ +package com.com.cnu.devlog_springboot.controller; + +import com.com.cnu.devlog_springboot.model.Project; +import com.com.cnu.devlog_springboot.model.request.ProjectRequest; +import com.com.cnu.devlog_springboot.service.ProjectService; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/project") +@RequiredArgsConstructor +public class ProjectController { + + private final ProjectService projectService; + + @GetMapping + public ResponseEntity> getProjects() { + return ResponseEntity.ok(projectService.getProjects()); + } + + @GetMapping("{projectId}") + public ResponseEntity getProject(@PathVariable("projectId") Integer projectId) { + return ResponseEntity.ok(projectService.getProject(projectId)); + } + + @PostMapping + public ResponseEntity createPost(@RequestBody ProjectRequest projectRequest) { + return ResponseEntity.ok(projectService.createProject(projectRequest)); + } + + + @PutMapping("{projectId}") + public ResponseEntity updateProject( + @PathVariable("projectId") Integer projectId, + @RequestBody ProjectRequest projectRequest + ) { + return ResponseEntity.ok(projectService.updateProject(projectId, projectRequest)); + } + + @DeleteMapping("{projectId}") + public ResponseEntity deleteProject(@PathVariable("projectId") Integer projectId) { + projectService.deleteProject(projectId); + return ResponseEntity.noContent().build(); + } +} \ No newline at end of file diff --git a/src/main/java/com/com/cnu/devlog_springboot/exception/DevlogException.java b/src/main/java/com/com/cnu/devlog_springboot/exception/DevlogException.java new file mode 100644 index 0000000..9da0b4e --- /dev/null +++ b/src/main/java/com/com/cnu/devlog_springboot/exception/DevlogException.java @@ -0,0 +1,11 @@ +package com.com.cnu.devlog_springboot.exception; + +import com.com.cnu.devlog_springboot.type.ErrorCode; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class DevlogException extends RuntimeException { + private final ErrorCode errorCode; +} diff --git a/src/main/java/com/com/cnu/devlog_springboot/exception/GlobalExceptionHandler.java b/src/main/java/com/com/cnu/devlog_springboot/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..69d21e1 --- /dev/null +++ b/src/main/java/com/com/cnu/devlog_springboot/exception/GlobalExceptionHandler.java @@ -0,0 +1,26 @@ +package com.com.cnu.devlog_springboot.exception; + +import com.com.cnu.devlog_springboot.model.response.ErrorResponse; +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; + +@ControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(DevlogException.class) + public ResponseEntity handlerDevlogException( + HttpServletRequest request, DevlogException e + ) { + return ResponseEntity.status(e.getErrorCode().getHttpStatus()) + .body( + new ErrorResponse( + e.getErrorCode().getDescription(), + e.getErrorCode().getHttpStatus().value(), + e.getErrorCode().getErrorCode(), + request.getRequestURI() + ) + ); + } +} \ No newline at end of file diff --git a/src/main/java/com/com/cnu/devlog_springboot/model/response/ErrorResponse.java b/src/main/java/com/com/cnu/devlog_springboot/model/response/ErrorResponse.java new file mode 100644 index 0000000..142b24e --- /dev/null +++ b/src/main/java/com/com/cnu/devlog_springboot/model/response/ErrorResponse.java @@ -0,0 +1,8 @@ +package com.com.cnu.devlog_springboot.model.response; + +public record ErrorResponse(String title, + Integer status, + Integer code, + String instance) { + +} \ No newline at end of file diff --git a/src/main/java/com/com/cnu/devlog_springboot/repository/PostRepository.java b/src/main/java/com/com/cnu/devlog_springboot/repository/PostRepository.java index bb39395..1cc53d6 100644 --- a/src/main/java/com/com/cnu/devlog_springboot/repository/PostRepository.java +++ b/src/main/java/com/com/cnu/devlog_springboot/repository/PostRepository.java @@ -1,13 +1,17 @@ package com.com.cnu.devlog_springboot.repository; import com.com.cnu.devlog_springboot.model.Post; +import com.com.cnu.devlog_springboot.type.Tag; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface PostRepository extends JpaRepository { // Post save(Post post); // List findAll(); // Optional findById(Integer postId); // void delete(Post post); + List findAllByTag(Tag tag); } diff --git a/src/main/java/com/com/cnu/devlog_springboot/repository/ProjectRepository.java b/src/main/java/com/com/cnu/devlog_springboot/repository/ProjectRepository.java new file mode 100644 index 0000000..1cca6fb --- /dev/null +++ b/src/main/java/com/com/cnu/devlog_springboot/repository/ProjectRepository.java @@ -0,0 +1,9 @@ +package com.com.cnu.devlog_springboot.repository; + +import com.com.cnu.devlog_springboot.model.Project; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface ProjectRepository extends JpaRepository { + +} + diff --git a/src/main/java/com/com/cnu/devlog_springboot/service/PostService.java b/src/main/java/com/com/cnu/devlog_springboot/service/PostService.java index 3823d7a..92d7fb6 100644 --- a/src/main/java/com/com/cnu/devlog_springboot/service/PostService.java +++ b/src/main/java/com/com/cnu/devlog_springboot/service/PostService.java @@ -1,8 +1,12 @@ package com.com.cnu.devlog_springboot.service; +import com.com.cnu.devlog_springboot.exception.DevlogException; import com.com.cnu.devlog_springboot.model.Post; import com.com.cnu.devlog_springboot.model.request.PostRequest; import com.com.cnu.devlog_springboot.repository.PostRepository; +import com.com.cnu.devlog_springboot.type.ErrorCode; +import com.com.cnu.devlog_springboot.type.Tag; +import io.micrometer.common.lang.Nullable; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -13,11 +17,18 @@ public class PostService { private final PostRepository postRepository; - public List getPosts() { + public List getPosts(@Nullable Tag tag) { + if (tag != null) + return postRepository.findAllByTag(tag); + return postRepository.findAll(); } - public Post creatPost(PostRequest postRequest) { + public Post getPost(Integer postId) { + return postRepository.findById(postId) + .orElseThrow(() -> new DevlogException(ErrorCode.POST_NOT_FOUND)); + } + public Post createPost(PostRequest postRequest) { return postRepository.save(new Post( null, postRequest.title(), @@ -36,13 +47,10 @@ public Post updatePost(Integer postId, PostRequest postRequest) { .orElse(null); } - public Post getPost(Integer postId) { - return postRepository.findById(postId) - .orElse(null); - } - public void deletePost(Integer postId) { postRepository.findById(postId) .ifPresent(postRepository::delete); } + + } diff --git a/src/main/java/com/com/cnu/devlog_springboot/service/ProjectService.java b/src/main/java/com/com/cnu/devlog_springboot/service/ProjectService.java new file mode 100644 index 0000000..647a8c3 --- /dev/null +++ b/src/main/java/com/com/cnu/devlog_springboot/service/ProjectService.java @@ -0,0 +1,60 @@ +package com.com.cnu.devlog_springboot.service; + +import com.com.cnu.devlog_springboot.exception.DevlogException; +import com.com.cnu.devlog_springboot.model.Project; +import com.com.cnu.devlog_springboot.model.request.ProjectRequest; +import com.com.cnu.devlog_springboot.repository.ProjectRepository; +import com.com.cnu.devlog_springboot.type.ErrorCode; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class ProjectService { + + private final ProjectRepository projectRepository; + + + public List getProjects() { + return projectRepository.findAll(); + } + + public Project getProject(Integer projectId) { + return projectRepository.findById(projectId) + .orElseThrow(() -> new DevlogException(ErrorCode.PROJECT_NOT_FOUND)); + } + + public Project createProject(ProjectRequest projectRequest) { + return projectRepository.save(new Project( + null, + projectRequest.title(), + projectRequest.summary(), + projectRequest.contents(), + projectRequest.startDate(), + projectRequest.endDate() + )); + } + + public Project updateProject(Integer projectId, ProjectRequest projectRequest) { + return projectRepository.findById(projectId) + .map(project -> { + project.setTitle(projectRequest.title()); + project.setContents(projectRequest.contents()); + project.setSummary(projectRequest.summary()); + project.setStartDate(projectRequest.startDate()); + project.setEndDate(projectRequest.endDate()); + return projectRepository.save(project); + }) + .orElseThrow(() -> new DevlogException(ErrorCode.PROJECT_NOT_FOUND)); + + } + + public void deleteProject(Integer projectId) { + projectRepository.findById(projectId) + .ifPresent(projectRepository::delete); + } + + + +} \ No newline at end of file diff --git a/src/main/java/com/com/cnu/devlog_springboot/type/ErrorCode.java b/src/main/java/com/com/cnu/devlog_springboot/type/ErrorCode.java new file mode 100644 index 0000000..6cde4af --- /dev/null +++ b/src/main/java/com/com/cnu/devlog_springboot/type/ErrorCode.java @@ -0,0 +1,30 @@ +package com.com.cnu.devlog_springboot.type; + + +import lombok.Getter; +import org.springframework.http.HttpStatus; + +@Getter +public enum ErrorCode { + //4xx + + POST_NOT_FOUND(HttpStatus.NOT_FOUND, + 4000, + "해당 게시글을 찾을 수 없음." + ), + PROJECT_NOT_FOUND( + HttpStatus.NOT_FOUND, + 4001, + "해당 프로젝트를 찾을 수 없음.") + ; + + private final HttpStatus httpStatus; + private final Integer errorCode; + private final String description; + + ErrorCode(HttpStatus httpStatus, Integer errorCode, String description) { + this.httpStatus = httpStatus; + this.errorCode = errorCode; + this.description = description; + } +} \ No newline at end of file From 843b3d42209c38fb79051eff46d3bfb9efcd9d5b Mon Sep 17 00:00:00 2001 From: "[Github" <[Github> Date: Mon, 6 May 2024 16:03:59 +0900 Subject: [PATCH 3/3] PostController --- .../controller/PostController.java | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/com/cnu/devlog_springboot/controller/PostController.java b/src/main/java/com/com/cnu/devlog_springboot/controller/PostController.java index b9e7af3..cb43e4c 100644 --- a/src/main/java/com/com/cnu/devlog_springboot/controller/PostController.java +++ b/src/main/java/com/com/cnu/devlog_springboot/controller/PostController.java @@ -3,11 +3,11 @@ import com.com.cnu.devlog_springboot.model.Post; import com.com.cnu.devlog_springboot.model.request.PostRequest; import com.com.cnu.devlog_springboot.service.PostService; -import com.com.cnu.devlog_springboot.type.Tag; -import io.micrometer.common.lang.Nullable; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import com.com.cnu.devlog_springboot.type.Tag; +import org.springframework.lang.Nullable; import java.util.List; @@ -17,17 +17,19 @@ public class PostController { private final PostService postService; - // GET /posts + // GET /posts/{postId} + @GetMapping("{postId}") + public ResponseEntity getPost(@PathVariable("postId")Integer postId) { + return ResponseEntity.ok(postService.getPost(postId)); + } + + + // GET /posts/tag @GetMapping - public ResponseEntity> getPosts(@RequestParam("tag") @Nullable Tag tag) { + public ResponseEntity> getPosts(@RequestParam @Nullable Tag tag) { return ResponseEntity.ok(postService.getPosts(tag)); } - // GET /posts/{postId} - @GetMapping("/{postId}") - public ResponseEntity getPost(@PathVariable("postId") Integer postId) { - return ResponseEntity.ok(postService.getPost(postId)); - } // POST /posts @PostMapping @@ -35,19 +37,23 @@ public ResponseEntity createPost(@RequestBody PostRequest postRequest) { return ResponseEntity.ok(postService.createPost(postRequest)); } + // PUT /posts/{postId} - @PutMapping("/{postId}") + // ex. localhost:8080/posts/3 + @PutMapping("{postId}") public ResponseEntity updatePost( - @PathVariable("postId") Integer postId, + @PathVariable("postId")Integer postId, @RequestBody PostRequest postRequest ) { return ResponseEntity.ok(postService.updatePost(postId, postRequest)); } + // DELETE /posts/{postId} - @DeleteMapping("/{postId}") + @DeleteMapping("{postId}") public ResponseEntity deletePost(@PathVariable("postId") Integer postId) { postService.deletePost(postId); return ResponseEntity.noContent().build(); } } +