From b4a6d0377d5bf68cfac27b3fd65203abe7a4cfe4 Mon Sep 17 00:00:00 2001 From: dhkstnaos Date: Tue, 15 Nov 2022 04:55:44 +0900 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20=EB=B6=81=EB=A7=88=ED=81=AC=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=EC=9D=84=20=EC=B6=94=EA=B0=80=ED=95=98?= =?UTF-8?q?=EB=9D=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../oauth/controller/BookmarkController.java | 22 +++++++++++++++++++ .../wiki/oauth/service/BookmarkService.java | 5 +++++ 2 files changed, 27 insertions(+) create mode 100644 src/main/java/com/developer/wiki/oauth/controller/BookmarkController.java create mode 100644 src/main/java/com/developer/wiki/oauth/service/BookmarkService.java diff --git a/src/main/java/com/developer/wiki/oauth/controller/BookmarkController.java b/src/main/java/com/developer/wiki/oauth/controller/BookmarkController.java new file mode 100644 index 0000000..58e64be --- /dev/null +++ b/src/main/java/com/developer/wiki/oauth/controller/BookmarkController.java @@ -0,0 +1,22 @@ +package com.developer.wiki.oauth.controller; + +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/bookmark") +public class BookmarkController { + + @PostMapping("/{questionId}") + public ResponseEntity getUserInfo(@AuthenticationPrincipal User currentUser, + @PathVariable Long questionId) { + return ResponseEntity.ok().build(); + } +} diff --git a/src/main/java/com/developer/wiki/oauth/service/BookmarkService.java b/src/main/java/com/developer/wiki/oauth/service/BookmarkService.java new file mode 100644 index 0000000..3dd408e --- /dev/null +++ b/src/main/java/com/developer/wiki/oauth/service/BookmarkService.java @@ -0,0 +1,5 @@ +package com.developer.wiki.oauth.service; + +public class BookmarkService { + +} From fd4f26a2ed452f8c3c1e45ec1d6c3816c159998c Mon Sep 17 00:00:00 2001 From: dhkstnaos Date: Tue, 15 Nov 2022 05:14:24 +0900 Subject: [PATCH 2/5] =?UTF-8?q?feat:=20=EB=B6=81=EB=A7=88=ED=81=AC=20?= =?UTF-8?q?=EC=97=94=ED=8B=B0=ED=8B=B0=EB=A5=BC=20=EC=83=9D=EC=84=B1?= =?UTF-8?q?=ED=95=98=EB=9D=BC.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/developer/wiki/bookmark/Bookmark.java | 54 +++++++++++++++++++ .../BookmarkController.java | 5 +- .../wiki/bookmark/BookmarkRepository.java | 0 .../service => bookmark}/BookmarkService.java | 0 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/developer/wiki/bookmark/Bookmark.java rename src/main/java/com/developer/wiki/{oauth/controller => bookmark}/BookmarkController.java (81%) create mode 100644 src/main/java/com/developer/wiki/bookmark/BookmarkRepository.java rename src/main/java/com/developer/wiki/{oauth/service => bookmark}/BookmarkService.java (100%) 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/oauth/controller/BookmarkController.java b/src/main/java/com/developer/wiki/bookmark/BookmarkController.java similarity index 81% rename from src/main/java/com/developer/wiki/oauth/controller/BookmarkController.java rename to src/main/java/com/developer/wiki/bookmark/BookmarkController.java index 58e64be..b70a575 100644 --- a/src/main/java/com/developer/wiki/oauth/controller/BookmarkController.java +++ b/src/main/java/com/developer/wiki/bookmark/BookmarkController.java @@ -1,6 +1,7 @@ package com.developer.wiki.oauth.controller; import com.developer.wiki.oauth.User; +import com.developer.wiki.oauth.service.BookmarkService; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; @@ -11,12 +12,14 @@ @RequiredArgsConstructor @RestController -@RequestMapping("/api/v1/bookmark") +@RequestMapping("/api/v1/bookmarka") public class BookmarkController { + private final BookmarkService bookmarkService; @PostMapping("/{questionId}") public ResponseEntity getUserInfo(@AuthenticationPrincipal User currentUser, @PathVariable Long questionId) { + bookmarkService.bookmark 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..e69de29 diff --git a/src/main/java/com/developer/wiki/oauth/service/BookmarkService.java b/src/main/java/com/developer/wiki/bookmark/BookmarkService.java similarity index 100% rename from src/main/java/com/developer/wiki/oauth/service/BookmarkService.java rename to src/main/java/com/developer/wiki/bookmark/BookmarkService.java From abfba2949c6652576cac967be9a6036e845887e6 Mon Sep 17 00:00:00 2001 From: dhkstnaos Date: Tue, 15 Nov 2022 05:14:31 +0900 Subject: [PATCH 3/5] =?UTF-8?q?feat:=20=EB=B6=81=EB=A7=88=ED=81=AC=20?= =?UTF-8?q?=EC=BB=A8=ED=8A=B8=EB=A1=A4=EB=9F=AC=EB=A5=BC=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=ED=95=98=EB=9D=BC.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/developer/wiki/bookmark/BookmarkController.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/developer/wiki/bookmark/BookmarkController.java b/src/main/java/com/developer/wiki/bookmark/BookmarkController.java index b70a575..cdb8e17 100644 --- a/src/main/java/com/developer/wiki/bookmark/BookmarkController.java +++ b/src/main/java/com/developer/wiki/bookmark/BookmarkController.java @@ -1,7 +1,6 @@ -package com.developer.wiki.oauth.controller; +package com.developer.wiki.bookmark; import com.developer.wiki.oauth.User; -import com.developer.wiki.oauth.service.BookmarkService; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; @@ -14,12 +13,13 @@ @RestController @RequestMapping("/api/v1/bookmarka") public class BookmarkController { + private final BookmarkService bookmarkService; @PostMapping("/{questionId}") public ResponseEntity getUserInfo(@AuthenticationPrincipal User currentUser, @PathVariable Long questionId) { - bookmarkService.bookmark + bookmarkService.toggle(questionId, currentUser.getId()); return ResponseEntity.ok().build(); } } From e77637435d170b13a2390cc25e54b387b537778d Mon Sep 17 00:00:00 2001 From: dhkstnaos Date: Tue, 15 Nov 2022 05:14:39 +0900 Subject: [PATCH 4/5] =?UTF-8?q?feat:=20=EB=B6=81=EB=A7=88=ED=81=AC=20?= =?UTF-8?q?=EB=A0=88=ED=8F=AC=EB=A5=BC=20=EC=83=9D=EC=84=B1=ED=95=98?= =?UTF-8?q?=EB=9D=BC.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../developer/wiki/bookmark/BookmarkRepository.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/com/developer/wiki/bookmark/BookmarkRepository.java b/src/main/java/com/developer/wiki/bookmark/BookmarkRepository.java index e69de29..ae65615 100644 --- a/src/main/java/com/developer/wiki/bookmark/BookmarkRepository.java +++ 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); +} From 621cecb27d3d3b99af561d59c32efad451c39b52 Mon Sep 17 00:00:00 2001 From: dhkstnaos Date: Tue, 15 Nov 2022 05:14:48 +0900 Subject: [PATCH 5/5] =?UTF-8?q?feat:=20=EB=B6=81=EB=A7=88=ED=81=AC=20?= =?UTF-8?q?=EC=84=9C=EB=B9=84=EC=8A=A4=EB=A5=BC=20=EC=83=9D=EC=84=B1?= =?UTF-8?q?=ED=95=98=EB=9D=BC.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wiki/bookmark/BookmarkController.java | 2 +- .../wiki/bookmark/BookmarkService.java | 35 ++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/developer/wiki/bookmark/BookmarkController.java b/src/main/java/com/developer/wiki/bookmark/BookmarkController.java index cdb8e17..4f7e3d1 100644 --- a/src/main/java/com/developer/wiki/bookmark/BookmarkController.java +++ b/src/main/java/com/developer/wiki/bookmark/BookmarkController.java @@ -11,7 +11,7 @@ @RequiredArgsConstructor @RestController -@RequestMapping("/api/v1/bookmarka") +@RequestMapping("/api/v1/bookmarks") public class BookmarkController { private final BookmarkService bookmarkService; diff --git a/src/main/java/com/developer/wiki/bookmark/BookmarkService.java b/src/main/java/com/developer/wiki/bookmark/BookmarkService.java index 3dd408e..0c635ba 100644 --- a/src/main/java/com/developer/wiki/bookmark/BookmarkService.java +++ b/src/main/java/com/developer/wiki/bookmark/BookmarkService.java @@ -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); + } }