Skip to content

Commit

Permalink
feat: 북마크 서비스를 생성하라.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhkstnaos committed Nov 14, 2022
1 parent e776374 commit 621cecb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/bookmarka")
@RequestMapping("/api/v1/bookmarks")
public class BookmarkController {

private final BookmarkService bookmarkService;
Expand Down
35 changes: 34 additions & 1 deletion src/main/java/com/developer/wiki/bookmark/BookmarkService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
package com.developer.wiki.oauth.service;
package com.developer.wiki.bookmark;

import com.developer.wiki.common.exception.NotFoundException;
import com.developer.wiki.oauth.UserRepository;
import com.developer.wiki.question.command.domain.Question;
import com.developer.wiki.question.command.domain.QuestionRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RequiredArgsConstructor
public class BookmarkService {

private final QuestionRepository questionRepository;
private final UserRepository userRepository;
private final BookmarkRepository bookmarkRepository;

public void toggle(Long questionId, Long userId) {
Question question = questionRepository.findById(questionId)
.orElseThrow(() -> new NotFoundException("존재하지 않는 ID입니다."));
bookmarkRepository.findByUserIdAndQuestion(userId, question).ifPresentOrElse(bookmark -> {
unBookmark(bookmark);
}, () -> {
bookmark(userId, question);
});
}

private void bookmark(Long userId, Question question) {
Bookmark bookmark = new Bookmark(userId, question);
bookmarkRepository.save(bookmark);
}

private void unBookmark(Bookmark bookmark) {
bookmarkRepository.delete(bookmark);
}
}

0 comments on commit 621cecb

Please sign in to comment.