Skip to content

Commit

Permalink
feat:add isScraped boolean in post response
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmy0006 committed Apr 16, 2024
1 parent b847a19 commit 9ae2ef0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.poolc.api.post.service.PostService;
import org.poolc.api.post.vo.PostCreateValues;
import org.poolc.api.post.vo.PostUpdateValues;
import org.poolc.api.scrap.service.ScrapService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
Expand All @@ -28,6 +29,7 @@
public class PostController {
private final PostService postService;
private final LikeService likeService;
private final ScrapService scrapService;

@PostMapping(value = "/post/new")
public ResponseEntity<Void> registerPost(@AuthenticationPrincipal Member member,
Expand All @@ -39,7 +41,8 @@ public ResponseEntity<Void> registerPost(@AuthenticationPrincipal Member member,
@GetMapping("/post/{postId}")
public ResponseEntity<PostResponse> viewPost(@AuthenticationPrincipal Member member, @PathVariable Long postId) {
Post post = postService.findPostById(member, postId);
return ResponseEntity.status(HttpStatus.OK).body(PostResponse.of(post));
PostResponse response = PostResponse.of(post, scrapService.isScrap(member.getLoginID(),postId));
return ResponseEntity.status(HttpStatus.OK).body(response);
}

@GetMapping("/board/{boardTitle}")
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/poolc/api/post/dto/PostResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ public class PostResponse {
private String region;
private String field;
private LocalDate deadline;
private Boolean isScraped;

public static PostResponse of(Post post) {
public static PostResponse of(Post post, boolean isScraped) {
PostResponse response = new PostResponse();

if (post.getIsDeleted()) return null;
Expand Down Expand Up @@ -73,6 +74,7 @@ public static PostResponse of(Post post) {
response.setDeadline(post.getDeadline());
response.setBoardPostCount(response.getBoardType().getPostCount());
response.setBoardPageNum(response.getBoardPostCount() / 15 + 1);
response.setIsScraped(isScraped);
return response;
}
}
7 changes: 7 additions & 0 deletions src/main/java/org/poolc/api/scrap/service/ScrapService.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ public void scrap(String memberId, Long postId) {
}
}

public boolean isScrap(String memberId, Long postId){
if(scrapRepository.existsByMemberIdAndPostId(memberId, postId)) {
return true;
}
return false;
}

public void removeScrap(String memberId, Long postId) {
Scrap scrap = scrapRepository.findByMemberIdAndPostId(memberId, postId)
.orElseThrow(() -> new NoSuchElementException("No scrap with given id."));
Expand Down

0 comments on commit 9ae2ef0

Please sign in to comment.