diff --git a/src/main/java/com/developer/wiki/bookmark/Bookmark.java b/src/main/java/com/developer/wiki/bookmark/Bookmark.java new file mode 100644 index 0000000..9830638 --- /dev/null +++ b/src/main/java/com/developer/wiki/bookmark/Bookmark.java @@ -0,0 +1,54 @@ +package com.developer.wiki.bookmark; + +import com.developer.wiki.common.exception.BadRequestException; +import com.developer.wiki.question.command.domain.Question; +import java.util.Objects; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Entity +@Table(name = "bookmark") +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class Bookmark { + + @Id + @GeneratedValue + @Column(name = "id") + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "question_id") + private Question question; + + @Column(name = "user_id") + private Long userId; + + public Bookmark(Long userId, Question question) { + setUserId(userId); + setQuestion(question); + } + + private void setUserId(Long userId) { + if (Objects.isNull(userId) || userId <= 0) { + throw new BadRequestException("잘못된 사용자 아이디가 입력되었습니다."); + } + this.userId = userId; + } + + private void setQuestion(Question question) { + if (Objects.isNull(question)) { + throw new BadRequestException("코스로 빈 값을 받을 수 없습니다."); + } + this.question = question; + } +} \ No newline at end of file diff --git a/src/main/java/com/developer/wiki/bookmark/BookmarkController.java b/src/main/java/com/developer/wiki/bookmark/BookmarkController.java new file mode 100644 index 0000000..4f7e3d1 --- /dev/null +++ b/src/main/java/com/developer/wiki/bookmark/BookmarkController.java @@ -0,0 +1,25 @@ +package com.developer.wiki.bookmark; + +import com.developer.wiki.oauth.User; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RequiredArgsConstructor +@RestController +@RequestMapping("/api/v1/bookmarks") +public class BookmarkController { + + private final BookmarkService bookmarkService; + + @PostMapping("/{questionId}") + public ResponseEntity getUserInfo(@AuthenticationPrincipal User currentUser, + @PathVariable Long questionId) { + bookmarkService.toggle(questionId, currentUser.getId()); + return ResponseEntity.ok().build(); + } +} diff --git a/src/main/java/com/developer/wiki/bookmark/BookmarkRepository.java b/src/main/java/com/developer/wiki/bookmark/BookmarkRepository.java new file mode 100644 index 0000000..ae65615 --- /dev/null +++ b/src/main/java/com/developer/wiki/bookmark/BookmarkRepository.java @@ -0,0 +1,12 @@ +package com.developer.wiki.bookmark; + +import com.developer.wiki.question.command.domain.Question; +import java.util.Optional; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface BookmarkRepository extends JpaRepository { + + Optional findByUserIdAndQuestion(Long userId, Question question); + + Boolean existsByUserIdAndQuestion(Long userId, Question question); +} diff --git a/src/main/java/com/developer/wiki/bookmark/BookmarkService.java b/src/main/java/com/developer/wiki/bookmark/BookmarkService.java new file mode 100644 index 0000000..0c635ba --- /dev/null +++ b/src/main/java/com/developer/wiki/bookmark/BookmarkService.java @@ -0,0 +1,38 @@ +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); + } +}