-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: #2
Are you sure you want to change the base?
2주차 심화과제 #3
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -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,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())) { | ||
|
@@ -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() | ||
); | ||
updatePost.setId((long) postId); | ||
postList.set(postId, updatePost); | ||
return Optional.of(postList.get(postId).toString()); | ||
} | ||
return Optional.empty(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기도 위와 마찬가지로 이렇게 if문을 작성해주신 이유가 궁금합니다!! |
||
} | ||
return Optional.empty(); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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로 잘못 입력해도 수정이 가능할 것 같아서요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아마 if문으로 postId가 postList의 사이즈보다 작아야 해당 포스트가 존재해서 예외처리 해 주신 것 같아요!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
맞습니다! list 크기를 초과하는 postId가 들어오면 존재하지 않는 게시물이기 때문에 if문을 써줬어요!