-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feature/#84
- Loading branch information
Showing
4 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/developer/wiki/bookmark/BookmarkController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/developer/wiki/bookmark/BookmarkRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Bookmark, Long> { | ||
|
||
Optional<Bookmark> findByUserIdAndQuestion(Long userId, Question question); | ||
|
||
Boolean existsByUserIdAndQuestion(Long userId, Question question); | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/developer/wiki/bookmark/BookmarkService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |