From a3eb011479cb62b7be2a118e8866dfc49634a56a Mon Sep 17 00:00:00 2001 From: onpyeong Date: Thu, 27 Apr 2023 12:01:52 +0900 Subject: [PATCH] =?UTF-8?q?2=EC=A3=BC=EC=B0=A8=20=EC=8B=AC=ED=99=94?= =?UTF-8?q?=EA=B3=BC=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/post/PostController.java | 13 +++ .../controller/post/dto/UpdatePostDTO.java | 13 +++ .../service/post/PostService.java | 34 +++++- ThirdSeminar/.idea/workspace.xml | 109 ++++++++++++++++++ 4 files changed, 164 insertions(+), 5 deletions(-) create mode 100644 SecondSeminar/src/main/java/sopt/org/SecondSeminar/controller/post/dto/UpdatePostDTO.java create mode 100644 ThirdSeminar/.idea/workspace.xml diff --git a/SecondSeminar/src/main/java/sopt/org/SecondSeminar/controller/post/PostController.java b/SecondSeminar/src/main/java/sopt/org/SecondSeminar/controller/post/PostController.java index 401d003..ac029a8 100644 --- a/SecondSeminar/src/main/java/sopt/org/SecondSeminar/controller/post/PostController.java +++ b/SecondSeminar/src/main/java/sopt/org/SecondSeminar/controller/post/PostController.java @@ -3,6 +3,7 @@ import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; import sopt.org.SecondSeminar.controller.post.dto.CreatePostDTO; +import sopt.org.SecondSeminar.controller.post.dto.UpdatePostDTO; import sopt.org.SecondSeminar.domain.post.Post; import sopt.org.SecondSeminar.service.post.PostService; @@ -35,4 +36,16 @@ public String getPostByTitle(@RequestParam final String title) { return "조회된 게시물이 없습니다."; return postList; } + + @PutMapping("/post/{postId}") + public String updatePost(@PathVariable final int postId, @RequestBody final UpdatePostDTO post) { + Optional updateResult = postService.updatePost(postId, post); + return updateResult.orElse("게시물 업데이트를 실패했습니다."); + } + + @DeleteMapping("/post/{postId}") + public String deletePost(@PathVariable final int postId) { + Optional deletePostId = postService.deletePost(postId); + return deletePostId.map(id -> id + "번 게시물이 삭제되었습니다.").orElse("게시물 삭제를 실패했습니다."); + } } diff --git a/SecondSeminar/src/main/java/sopt/org/SecondSeminar/controller/post/dto/UpdatePostDTO.java b/SecondSeminar/src/main/java/sopt/org/SecondSeminar/controller/post/dto/UpdatePostDTO.java new file mode 100644 index 0000000..38a1bc8 --- /dev/null +++ b/SecondSeminar/src/main/java/sopt/org/SecondSeminar/controller/post/dto/UpdatePostDTO.java @@ -0,0 +1,13 @@ +package sopt.org.SecondSeminar.controller.post.dto; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor(access = AccessLevel.PRIVATE) +public class UpdatePostDTO { + private String writer; + private String title; + private String content; +} diff --git a/SecondSeminar/src/main/java/sopt/org/SecondSeminar/service/post/PostService.java b/SecondSeminar/src/main/java/sopt/org/SecondSeminar/service/post/PostService.java index afe51fc..0af7362 100644 --- a/SecondSeminar/src/main/java/sopt/org/SecondSeminar/service/post/PostService.java +++ b/SecondSeminar/src/main/java/sopt/org/SecondSeminar/service/post/PostService.java @@ -2,6 +2,7 @@ import org.springframework.stereotype.Service; import sopt.org.SecondSeminar.controller.post.dto.CreatePostDTO; +import sopt.org.SecondSeminar.controller.post.dto.UpdatePostDTO; import sopt.org.SecondSeminar.domain.post.Post; import java.util.Optional; @@ -11,7 +12,7 @@ @Service public class PostService { - public Long createPost(CreatePostDTO post) { + public Long createPost(final CreatePostDTO post) { Post newPost = new Post( post.getWriter(), post.getTitle(), @@ -19,21 +20,21 @@ public Long createPost(CreatePostDTO post) { post.getLike() ); - postList.add(newPost); newPost.setId((long) postList.size()); + postList.add(newPost); return newPost.getId(); } - public Optional getPostById(Integer postId) { //코틀린 nullable ? 대신 자바는 Optional - if (postList.size() >= postId) { + public Optional getPostById(final Integer postId) { //코틀린 nullable ? 대신 자바는 Optional + if (postList.size() > postId) { return Optional.of(postList.get(postId)); //코틀린 !! -> 무조건 null이 아님 } else { return Optional.empty(); //null 객체 } } - public String getPostByTitle(String title) { + public String getPostByTitle(final String title) { StringBuilder resultPostList = new StringBuilder(); postList.forEach((post) -> { if (title.equals(post.getTitle())) { @@ -45,4 +46,27 @@ public String getPostByTitle(String title) { return resultPostList.toString(); } + + public Optional updatePost(final int postId, final UpdatePostDTO post) { + if (postId < postList.size()) { + Post updatePost = new Post( + post.getWriter(), + post.getTitle(), + post.getContent(), + postList.get(postId).getLike() + ); + updatePost.setId((long) postId); + postList.set(postId, updatePost); + return Optional.of(postList.get(postId).toString()); + } + return Optional.empty(); + } + + public Optional deletePost(final int postId) { + if (postId < postList.size()) { + postList.remove(postId); + return Optional.of(postId); + } + return Optional.empty(); + } } diff --git a/ThirdSeminar/.idea/workspace.xml b/ThirdSeminar/.idea/workspace.xml new file mode 100644 index 0000000..2de943f --- /dev/null +++ b/ThirdSeminar/.idea/workspace.xml @@ -0,0 +1,109 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + true + false + false + + + + + + + + + + + + + + + + 1682147754107 + + + + + + \ No newline at end of file