-
Notifications
You must be signed in to change notification settings - Fork 28
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/#1526 아티클 북마크 기능 추가
- Loading branch information
Showing
16 changed files
with
446 additions
and
21 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
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
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 @@ | ||
[[article]] | ||
== 아티클 | ||
|
||
=== 아티클 북마크 추가 | ||
|
||
==== Request | ||
|
||
include::{snippets}/article/bookmark/http-request.adoc[] | ||
|
||
==== Response | ||
|
||
include::{snippets}/article/bookmark/http-response.adoc[] |
40 changes: 40 additions & 0 deletions
40
backend/src/documentation/java/wooteco/prolog/docu/ArticleDocumentation.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,40 @@ | ||
package wooteco.prolog.docu; | ||
|
||
import static io.netty.handler.codec.http.HttpHeaders.Values.APPLICATION_JSON; | ||
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import io.restassured.module.mockmvc.response.ValidatableMockMvcResponse; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import wooteco.prolog.NewDocumentation; | ||
import wooteco.prolog.article.application.ArticleService; | ||
import wooteco.prolog.article.ui.ArticleBookmarkRequest; | ||
import wooteco.prolog.article.ui.ArticleController; | ||
|
||
@WebMvcTest(controllers = ArticleController.class) | ||
public class ArticleDocumentation extends NewDocumentation { | ||
|
||
@MockBean | ||
private ArticleService articleService; | ||
|
||
@Test | ||
void 아티클에_북마크를_변경한다() { | ||
//given, when | ||
final ArticleBookmarkRequest bookmarkRequest = new ArticleBookmarkRequest(true); | ||
|
||
final ValidatableMockMvcResponse response = given | ||
.header("Authorization", "Bearer " + accessToken) | ||
.contentType(APPLICATION_JSON) | ||
.body(bookmarkRequest) | ||
.when().put("/articles/{bookmark-id}/bookmark", 1L) | ||
.then().log().all(); | ||
|
||
//then | ||
response.expect(status().isOk()); | ||
|
||
//docs | ||
response.apply(document("article/bookmark")); | ||
} | ||
} |
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
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
43 changes: 43 additions & 0 deletions
43
backend/src/main/java/wooteco/prolog/article/domain/ArticleBookmark.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,43 @@ | ||
package wooteco.prolog.article.domain; | ||
|
||
import java.util.Objects; | ||
import javax.persistence.Entity; | ||
import javax.persistence.FetchType; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.ManyToOne; | ||
import javax.persistence.Table; | ||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Table(name = "article_bookmark") | ||
public class ArticleBookmark { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "article_id") | ||
private Article article; | ||
|
||
private Long memberId; | ||
|
||
private ArticleBookmark(final Long id, final Article article, final Long memberId) { | ||
this.id = id; | ||
this.article = article; | ||
this.memberId = memberId; | ||
} | ||
|
||
public ArticleBookmark(final Article article, final Long memberId) { | ||
this(null, article, memberId); | ||
} | ||
|
||
public boolean isOwner(final Long memberId) { | ||
return Objects.equals(this.memberId, memberId); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
backend/src/main/java/wooteco/prolog/article/domain/ArticleBookmarks.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,36 @@ | ||
package wooteco.prolog.article.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import javax.persistence.Embeddable; | ||
import javax.persistence.OneToMany; | ||
import org.hibernate.annotations.Cascade; | ||
import org.hibernate.annotations.CascadeType; | ||
|
||
@Embeddable | ||
public class ArticleBookmarks { | ||
|
||
@OneToMany(mappedBy = "article") | ||
@Cascade(value = {CascadeType.PERSIST, CascadeType.DELETE}) | ||
private List<ArticleBookmark> articleBookmarks; | ||
|
||
public ArticleBookmarks() { | ||
articleBookmarks = new ArrayList<>(); | ||
} | ||
|
||
public void addBookmark(final ArticleBookmark bookmark) { | ||
articleBookmarks.add(bookmark); | ||
} | ||
|
||
public void removeBookmark(final Long memberId) { | ||
articleBookmarks.stream() | ||
.filter(bookmark -> bookmark.isOwner(memberId)) | ||
.findAny() | ||
.ifPresent(bookmark -> articleBookmarks.remove(bookmark)); | ||
} | ||
|
||
public boolean containBookmark(final Long memberId) { | ||
return articleBookmarks.stream() | ||
.anyMatch(bookmark -> bookmark.isOwner(memberId)); | ||
} | ||
} |
9 changes: 7 additions & 2 deletions
9
backend/src/main/java/wooteco/prolog/article/domain/repository/ArticleRepository.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 |
---|---|---|
@@ -1,11 +1,16 @@ | ||
package wooteco.prolog.article.domain.repository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
import wooteco.prolog.article.domain.Article; | ||
|
||
import java.util.List; | ||
|
||
public interface ArticleRepository extends JpaRepository<Article, Long> { | ||
|
||
List<Article> findAllByOrderByCreatedAtDesc(); | ||
|
||
@Query("select a from Article a join fetch a.articleBookmarks where a.id = :id") | ||
Optional<Article> findFetchById(@Param("id") final Long id); | ||
} |
15 changes: 15 additions & 0 deletions
15
backend/src/main/java/wooteco/prolog/article/ui/ArticleBookmarkRequest.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,15 @@ | ||
package wooteco.prolog.article.ui; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public class ArticleBookmarkRequest { | ||
|
||
private final Boolean bookmark; | ||
|
||
public ArticleBookmarkRequest() { | ||
this(null); | ||
} | ||
} |
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
8 changes: 8 additions & 0 deletions
8
backend/src/main/resources/db/migration/prod/V6__create_table_article_bookmark.sql
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,8 @@ | ||
create table if not exists prolog.article_bookmark | ||
( | ||
id bigint auto_increment primary key, | ||
article_id bigint not null, | ||
member_id bigint not null | ||
) ENGINE = InnoDB | ||
DEFAULT CHARSET = utf8mb4 | ||
COLLATE = utf8mb4_0900_ai_ci; |
Oops, something went wrong.