Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2주차 심화과제 #3

Open
wants to merge 1 commit into
base: #2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String> updateResult = postService.updatePost(postId, post);
return updateResult.orElse("게시물 업데이트를 실패했습니다.");
}

@DeleteMapping("/post/{postId}")
public String deletePost(@PathVariable final int postId) {
Optional<Integer> deletePostId = postService.deletePost(postId);
return deletePostId.map(id -> id + "번 게시물이 삭제되었습니다.").orElse("게시물 삭제를 실패했습니다.");
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -11,29 +12,29 @@
@Service
public class PostService {

public Long createPost(CreatePostDTO post) {
public Long createPost(final CreatePostDTO post) {
Post newPost = new Post(
post.getWriter(),
post.getTitle(),
post.getContent(),
post.getLike()
);

postList.add(newPost);
newPost.setId((long) postList.size());
postList.add(newPost);

return newPost.getId();
}

public Optional<Post> getPostById(Integer postId) { //코틀린 nullable ? 대신 자바는 Optional
if (postList.size() >= postId) {
public Optional<Post> 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())) {
Expand All @@ -45,4 +46,27 @@ public String getPostByTitle(String title) {

return resultPostList.toString();
}

public Optional<String> 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()
);
Comment on lines +51 to +57

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if문 조건에서 아직 DB 구현이 되지 않아서 postId < postList.size()일 때만 수정이 가능하게 코드를 작성해주신걸까요?!
해당 로직에선 postId를 다른 postId로 잘못 입력해도 수정이 가능할 것 같아서요!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아마 if문으로 postId가 postList의 사이즈보다 작아야 해당 포스트가 존재해서 예외처리 해 주신 것 같아요!!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

맞습니다! list 크기를 초과하는 postId가 들어오면 존재하지 않는 게시물이기 때문에 if문을 써줬어요!

updatePost.setId((long) postId);
postList.set(postId, updatePost);
return Optional.of(postList.get(postId).toString());
}
return Optional.empty();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional.of와 Optional.empty 제가 처음 보는 메서드인데 뭔가 예외처리를 해줄 수 있는 애들 같아서 기능 찾아보고 저도 나중에 예외처리 할 때 사용해보겠습니다ㅎㅎ 예외처리 하는 방법 하나 배워갑니다!!

}

public Optional<Integer> deletePost(final int postId) {
if (postId < postList.size()) {
postList.remove(postId);
return Optional.of(postId);
Comment on lines +66 to +68

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기도 위와 마찬가지로 이렇게 if문을 작성해주신 이유가 궁금합니다!!

}
return Optional.empty();
}
}
109 changes: 109 additions & 0 deletions ThirdSeminar/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.