forked from gothinkster/spring-boot-realworld-example-app
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add bookmark article feature #146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devin-ai-integration
wants to merge
5
commits into
main
Choose a base branch
from
devin/1781526846-feature-bookmark-article
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5b444d6
feat: add bookmark article feature
devin-ai-integration[bot] e99e6fe
feat: add read-side bookmark integration
devin-ai-integration[bot] e9ada97
feat: add bookmark UI to frontend article pages
devin-ai-integration[bot] 85a5089
fix: correct stale closure in bookmark error-revert handlers
devin-ai-integration[bot] dfdd306
fix: bundle Conduit CSS locally (external CDN returns 404)
devin-ai-integration[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
|
devin-ai-integration[bot] marked this conversation as resolved.
|
This file contains hidden or 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,62 @@ | ||
| package io.spring.api; | ||
|
|
||
| import io.spring.api.exception.ResourceNotFoundException; | ||
| import io.spring.application.ArticleQueryService; | ||
| import io.spring.application.data.ArticleData; | ||
| import io.spring.core.article.Article; | ||
| import io.spring.core.article.ArticleRepository; | ||
| import io.spring.core.bookmark.ArticleBookmark; | ||
| import io.spring.core.bookmark.ArticleBookmarkRepository; | ||
| import io.spring.core.user.User; | ||
| import java.util.HashMap; | ||
| import lombok.AllArgsConstructor; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
| import org.springframework.web.bind.annotation.DeleteMapping; | ||
| 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; | ||
|
|
||
| @RestController | ||
| @RequestMapping(path = "articles/{slug}/bookmark") | ||
| @AllArgsConstructor | ||
| public class ArticleBookmarkApi { | ||
| private ArticleBookmarkRepository articleBookmarkRepository; | ||
| private ArticleRepository articleRepository; | ||
| private ArticleQueryService articleQueryService; | ||
|
|
||
| @PostMapping | ||
| public ResponseEntity bookmarkArticle( | ||
| @PathVariable("slug") String slug, @AuthenticationPrincipal User user) { | ||
| Article article = | ||
| articleRepository.findBySlug(slug).orElseThrow(ResourceNotFoundException::new); | ||
| ArticleBookmark articleBookmark = new ArticleBookmark(article.getId(), user.getId()); | ||
| articleBookmarkRepository.save(articleBookmark); | ||
| return responseArticleData(articleQueryService.findBySlug(slug, user).get()); | ||
| } | ||
|
|
||
| @DeleteMapping | ||
| public ResponseEntity unbookmarkArticle( | ||
| @PathVariable("slug") String slug, @AuthenticationPrincipal User user) { | ||
| Article article = | ||
| articleRepository.findBySlug(slug).orElseThrow(ResourceNotFoundException::new); | ||
| articleBookmarkRepository | ||
| .find(article.getId(), user.getId()) | ||
| .ifPresent( | ||
| bookmark -> { | ||
| articleBookmarkRepository.remove(bookmark); | ||
| }); | ||
| return responseArticleData(articleQueryService.findBySlug(slug, user).get()); | ||
| } | ||
|
|
||
| private ResponseEntity<HashMap<String, Object>> responseArticleData( | ||
| final ArticleData articleData) { | ||
| return ResponseEntity.ok( | ||
| new HashMap<String, Object>() { | ||
| { | ||
| put("article", articleData); | ||
| } | ||
| }); | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
21 changes: 21 additions & 0 deletions
21
src/main/java/io/spring/core/bookmark/ArticleBookmark.java
This file contains hidden or 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,21 @@ | ||
| package io.spring.core.bookmark; | ||
|
|
||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import org.joda.time.DateTime; | ||
|
|
||
| @NoArgsConstructor | ||
| @Getter | ||
| @EqualsAndHashCode(exclude = "createdAt") | ||
| public class ArticleBookmark { | ||
| private String articleId; | ||
| private String userId; | ||
| private DateTime createdAt; | ||
|
|
||
| public ArticleBookmark(String articleId, String userId) { | ||
| this.articleId = articleId; | ||
| this.userId = userId; | ||
| this.createdAt = new DateTime(); | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/main/java/io/spring/core/bookmark/ArticleBookmarkRepository.java
This file contains hidden or 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,11 @@ | ||
| package io.spring.core.bookmark; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public interface ArticleBookmarkRepository { | ||
| void save(ArticleBookmark articleBookmark); | ||
|
|
||
| Optional<ArticleBookmark> find(String articleId, String userId); | ||
|
|
||
| void remove(ArticleBookmark bookmark); | ||
| } |
This file contains hidden or 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
This file contains hidden or 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 io.spring.graphql; | ||
|
|
||
| import com.netflix.graphql.dgs.DgsComponent; | ||
| import com.netflix.graphql.dgs.DgsMutation; | ||
| import com.netflix.graphql.dgs.InputArgument; | ||
| import graphql.execution.DataFetcherResult; | ||
| import io.spring.api.exception.ResourceNotFoundException; | ||
| import io.spring.core.article.Article; | ||
| import io.spring.core.article.ArticleRepository; | ||
| import io.spring.core.bookmark.ArticleBookmark; | ||
| import io.spring.core.bookmark.ArticleBookmarkRepository; | ||
| import io.spring.core.user.User; | ||
| import io.spring.graphql.DgsConstants.MUTATION; | ||
| import io.spring.graphql.exception.AuthenticationException; | ||
| import io.spring.graphql.types.ArticlePayload; | ||
| import lombok.AllArgsConstructor; | ||
|
|
||
| @DgsComponent | ||
| @AllArgsConstructor | ||
| public class BookmarkMutation { | ||
|
|
||
| private ArticleBookmarkRepository articleBookmarkRepository; | ||
| private ArticleRepository articleRepository; | ||
|
|
||
| @DgsMutation(field = MUTATION.BookmarkArticle) | ||
| public DataFetcherResult<ArticlePayload> bookmarkArticle(@InputArgument("slug") String slug) { | ||
| User user = SecurityUtil.getCurrentUser().orElseThrow(AuthenticationException::new); | ||
| Article article = | ||
| articleRepository.findBySlug(slug).orElseThrow(ResourceNotFoundException::new); | ||
| ArticleBookmark articleBookmark = new ArticleBookmark(article.getId(), user.getId()); | ||
| articleBookmarkRepository.save(articleBookmark); | ||
| return DataFetcherResult.<ArticlePayload>newResult() | ||
| .data(ArticlePayload.newBuilder().build()) | ||
| .localContext(article) | ||
| .build(); | ||
| } | ||
|
|
||
| @DgsMutation(field = MUTATION.UnbookmarkArticle) | ||
| public DataFetcherResult<ArticlePayload> unbookmarkArticle(@InputArgument("slug") String slug) { | ||
| User user = SecurityUtil.getCurrentUser().orElseThrow(AuthenticationException::new); | ||
| Article article = | ||
| articleRepository.findBySlug(slug).orElseThrow(ResourceNotFoundException::new); | ||
| articleBookmarkRepository | ||
| .find(article.getId(), user.getId()) | ||
| .ifPresent( | ||
| bookmark -> { | ||
| articleBookmarkRepository.remove(bookmark); | ||
| }); | ||
| return DataFetcherResult.<ArticlePayload>newResult() | ||
| .data(ArticlePayload.newBuilder().build()) | ||
| .localContext(article) | ||
| .build(); | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/main/java/io/spring/infrastructure/mybatis/mapper/ArticleBookmarkMapper.java
This file contains hidden or 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,14 @@ | ||
| package io.spring.infrastructure.mybatis.mapper; | ||
|
|
||
| import io.spring.core.bookmark.ArticleBookmark; | ||
| import org.apache.ibatis.annotations.Mapper; | ||
| import org.apache.ibatis.annotations.Param; | ||
|
|
||
| @Mapper | ||
| public interface ArticleBookmarkMapper { | ||
| ArticleBookmark find(@Param("articleId") String articleId, @Param("userId") String userId); | ||
|
|
||
| void insert(@Param("articleBookmark") ArticleBookmark articleBookmark); | ||
|
|
||
| void delete(@Param("bookmark") ArticleBookmark bookmark); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚩 Bookmark error handler in ArticlePreview is more correct than the existing favorite error handler
The new
handleClickBookmarkerror handler at line 48-51 usesbookmarked: preview.bookmarked(the original closure value) to revert state on API failure — this is correct. Interestingly, the pre-existinghandleClickFavoriteerror handler atfrontend/components/article/ArticlePreview.tsx:88-94usesfavorited: !preview.favoritedwhich reapplies the toggled value rather than reverting it, effectively making the catch block a no-op for thefavoritedfield. This is a pre-existing bug in the favorite handler that the bookmark handler avoids. Consider fixinghandleClickFavorite's error handler to match the bookmark pattern.Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch — the same stale-closure pattern exists in the pre-existing
handleClickFavorite. That's a pre-existing bug outside the scope of this PR's bookmark feature, but worth noting for a follow-up fix.