diff --git a/backend/core/src/main/java/com/rollthedice/backend/domain/bookmark/controller/BookmarkController.java b/backend/core/src/main/java/com/rollthedice/backend/domain/bookmark/controller/BookmarkController.java index eaa46e34..6797e57f 100644 --- a/backend/core/src/main/java/com/rollthedice/backend/domain/bookmark/controller/BookmarkController.java +++ b/backend/core/src/main/java/com/rollthedice/backend/domain/bookmark/controller/BookmarkController.java @@ -5,16 +5,13 @@ import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.ResponseStatus; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequiredArgsConstructor -@RequestMapping("bookmark") +@RequestMapping("bookmarks") public class BookmarkController { private final BookmarkService bookmarkService; @@ -24,4 +21,15 @@ public List getBookmarked(final Pageable pageable) { return bookmarkService.getBookmarkedNews(pageable); } + @ResponseStatus(HttpStatus.CREATED) + @PostMapping("/{newsId}") + public void saveBookmark(@PathVariable final Long newsId) { + bookmarkService.saveBookmark(newsId); + } + + @ResponseStatus(HttpStatus.NO_CONTENT) + @DeleteMapping("/{newsId}") + public void deleteBookmark(@PathVariable final Long newsId) { + bookmarkService.deleteBookmark(newsId); + } } diff --git a/backend/core/src/main/java/com/rollthedice/backend/domain/bookmark/entity/Bookmark.java b/backend/core/src/main/java/com/rollthedice/backend/domain/bookmark/entity/Bookmark.java index d0314627..6af1d02f 100644 --- a/backend/core/src/main/java/com/rollthedice/backend/domain/bookmark/entity/Bookmark.java +++ b/backend/core/src/main/java/com/rollthedice/backend/domain/bookmark/entity/Bookmark.java @@ -5,9 +5,11 @@ import com.rollthedice.backend.global.config.BaseTimeEntity; import jakarta.persistence.*; import lombok.AccessLevel; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; + @Getter @Entity @NoArgsConstructor(access = AccessLevel.PROTECTED) @@ -23,4 +25,10 @@ public class Bookmark extends BaseTimeEntity { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "news_id") private News news; + + @Builder + public Bookmark(Member member, News news) { + this.member = member; + this.news = news; + } } diff --git a/backend/core/src/main/java/com/rollthedice/backend/domain/bookmark/repository/BookmarkRepository.java b/backend/core/src/main/java/com/rollthedice/backend/domain/bookmark/repository/BookmarkRepository.java index 76773a78..a107402f 100644 --- a/backend/core/src/main/java/com/rollthedice/backend/domain/bookmark/repository/BookmarkRepository.java +++ b/backend/core/src/main/java/com/rollthedice/backend/domain/bookmark/repository/BookmarkRepository.java @@ -14,4 +14,6 @@ public interface BookmarkRepository extends JpaRepository { Boolean existsBookmarkByMemberAndNews(Member member, News news); List findAllByMemberOrderByCreatedAt(Member member, Pageable pageable); + + void deleteByNewsId(Long newsId); } diff --git a/backend/core/src/main/java/com/rollthedice/backend/domain/bookmark/service/BookmarkService.java b/backend/core/src/main/java/com/rollthedice/backend/domain/bookmark/service/BookmarkService.java index 2a938a13..667bcf1e 100644 --- a/backend/core/src/main/java/com/rollthedice/backend/domain/bookmark/service/BookmarkService.java +++ b/backend/core/src/main/java/com/rollthedice/backend/domain/bookmark/service/BookmarkService.java @@ -1,5 +1,6 @@ package com.rollthedice.backend.domain.bookmark.service; +import com.rollthedice.backend.domain.bookmark.entity.Bookmark; import com.rollthedice.backend.domain.bookmark.repository.BookmarkRepository; import com.rollthedice.backend.domain.member.entity.Member; import com.rollthedice.backend.domain.member.query.AuthService; @@ -19,6 +20,7 @@ @Service public class BookmarkService { private final AuthService authService; + private final NewsService newsService; private final BookmarkRepository bookmarkRepository; private final NewsMapper newsMapper; @@ -35,5 +37,19 @@ public List getBookmarkedNews(Pageable pageable) { .collect(Collectors.toList()); } + + @Transactional + public void saveBookmark(Long newsId) { + Member member = authService.getMember(); + bookmarkRepository.save(Bookmark.builder() + .member(member) + .news(newsService.getOneNews(newsId)) + .build()); + } + + @Transactional + public void deleteBookmark(Long newsId) { + bookmarkRepository.deleteByNewsId(newsId); + } } diff --git a/backend/core/src/main/java/com/rollthedice/backend/domain/news/service/NewsService.java b/backend/core/src/main/java/com/rollthedice/backend/domain/news/service/NewsService.java index 29099278..cb59ca9a 100644 --- a/backend/core/src/main/java/com/rollthedice/backend/domain/news/service/NewsService.java +++ b/backend/core/src/main/java/com/rollthedice/backend/domain/news/service/NewsService.java @@ -1,6 +1,5 @@ package com.rollthedice.backend.domain.news.service; -import com.rollthedice.backend.domain.bookmark.entity.Bookmark; import com.rollthedice.backend.domain.bookmark.service.BookmarkService; import com.rollthedice.backend.domain.member.entity.Member; import com.rollthedice.backend.domain.member.query.AuthService; @@ -66,4 +65,8 @@ public List getNews(final Pageable pageable) { news, bookmarkService.isBookmarked(member, news))) .collect(Collectors.toList()); } + + public News getOneNews(Long newsId) { + return newsRepository.findById(newsId).orElseThrow(EntityNotFoundException::new); + } }