Skip to content

Commit

Permalink
Feat: 북마크 여부 확인 API (#286)
Browse files Browse the repository at this point in the history
* Refactor: 유저가 북마크 했는지 찾는 로직 분리

* Add: 북마크 했는지 확인하는 api 컨트롤러 추가
  • Loading branch information
muncool39 authored Oct 22, 2024
1 parent 1d5cec7 commit 5a4a5af
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import com.openbook.openbook.api.SliceResponse;
import com.openbook.openbook.api.user.request.BookmarkRequest;
import com.openbook.openbook.api.user.response.BookmarkResponse;
import com.openbook.openbook.domain.user.dto.BookmarkType;
import com.openbook.openbook.service.user.BookmarkService;
import jakarta.validation.Valid;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
Expand All @@ -25,6 +27,15 @@ public class BookmarkController {

private final BookmarkService bookmarkService;

@ResponseStatus(HttpStatus.OK)
@GetMapping("/bookmark")
public Map<String, Boolean> bookmarked(Authentication authentication,
@RequestParam(value = "type") BookmarkType type,
@RequestParam(value = "resourceId") long resourceId) {
Boolean bookmark = bookmarkService.isUserBookmark(Long.parseLong(authentication.getName()), type, resourceId);
return Map.of("bookmark", bookmark);
}

@ResponseStatus(HttpStatus.CREATED)
@PostMapping("/bookmark")
public ResponseMessage bookmark(Authentication authentication,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ public class BookmarkService {

private final BookmarkRepository bookmarkRepository;

public boolean isUserBookmark(long userId, BookmarkType type, long resourceId) {
return bookmarkRepository.existsByUserIdAndResourceIdAndBookmarkType(userId, resourceId, type);
}

@Transactional
public void createBookmark(long userId, BookmarkRequest request) {
User user = userService.getUserOrException(userId);
BookmarkType type = BookmarkType.fromString(request.type());
if(bookmarkRepository.existsByUserIdAndResourceIdAndBookmarkType(userId, request.resourceId(), type)) {
if(isUserBookmark(userId, type, request.resourceId())) {
throw new OpenBookException(ErrorCode.ALREADY_BOOKMARK);
}
bookmarkRepository.save( Bookmark.builder()
Expand Down

0 comments on commit 5a4a5af

Please sign in to comment.