-
Notifications
You must be signed in to change notification settings - Fork 0
[DP-447] 기술블로그 추천 API #133
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| [[TechArticleRecommend]] | ||
| == 기술블로그 추천 API(POST: /devdevdev/api/v1/articles/{techArticleId}/recommend) | ||
| * 회원과 익명회원은 기술블로그를 추천하거나 추천 취소할 수 있다. | ||
|
|
||
| === 정상 요청/응답 | ||
| ==== HTTP Request | ||
| include::{snippets}/tech-article-recommend/http-request.adoc[] | ||
| ==== HTTP Request Header Fields | ||
| include::{snippets}/tech-article-recommend/request-headers.adoc[] | ||
| ==== HTTP Request Path Parameters Fields | ||
| include::{snippets}/tech-article-recommend/path-parameters.adoc[] | ||
|
|
||
| ==== HTTP Response | ||
| include::{snippets}/tech-article-recommend/http-response.adoc[] | ||
| ==== HTTP Response Fields | ||
| include::{snippets}/tech-article-recommend/response-fields.adoc[] | ||
|
|
||
|
|
||
| === 예외 | ||
| ==== HTTP Response | ||
| include::{snippets}/not-found-tech-article-exception/response-body.adoc[] | ||
| include::{snippets}/not-found-member-exception/response-body.adoc[] |
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
62 changes: 54 additions & 8 deletions
62
src/main/java/com/dreamypatisiel/devdevdev/domain/entity/TechArticleRecommend.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 |
|---|---|---|
| @@ -1,27 +1,73 @@ | ||
| package com.dreamypatisiel.devdevdev.domain.entity; | ||
|
|
||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.FetchType; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.JoinColumn; | ||
| import jakarta.persistence.ManyToOne; | ||
| import jakarta.persistence.*; | ||
| import lombok.AccessLevel; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @Entity | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| public class TechArticleRecommend extends BasicTime { | ||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| @Column(nullable = false) | ||
| private boolean status; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "member_id", nullable = false) | ||
| @JoinColumn(name = "member_id") | ||
| private Member member; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "anonymous_member_id") | ||
| private AnonymousMember anonymousMember; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "tech_article_id", nullable = false) | ||
| private TechArticle techArticle; | ||
|
|
||
| @Builder | ||
| private TechArticleRecommend(boolean status, Member member, AnonymousMember anonymousMember, TechArticle techArticle) { | ||
| this.status = status; | ||
| this.member = member; | ||
| this.anonymousMember = anonymousMember; | ||
| this.techArticle = techArticle; | ||
| } | ||
|
|
||
|
|
||
| public static TechArticleRecommend create(Member member, TechArticle techArticle) { | ||
| return TechArticleRecommend.builder() | ||
| .member(member) | ||
| .techArticle(techArticle) | ||
| .status(true) | ||
| .build(); | ||
| } | ||
|
|
||
| public static TechArticleRecommend create(AnonymousMember anonymousMember, TechArticle techArticle) { | ||
| return TechArticleRecommend.builder() | ||
| .anonymousMember(anonymousMember) | ||
| .techArticle(techArticle) | ||
| .status(true) | ||
| .build(); | ||
| } | ||
|
|
||
| public void changeTechArticle(TechArticle techArticle) { | ||
| this.techArticle = techArticle; | ||
| techArticle.getRecommends().add(this); | ||
| } | ||
|
|
||
| public void cancelRecommend() { | ||
| this.status = false; | ||
| } | ||
|
|
||
| public void registerRecommend() { | ||
| this.status = true; | ||
| } | ||
|
|
||
| public boolean isRecommended() { | ||
| return this.status; | ||
| } | ||
| } |
2 changes: 1 addition & 1 deletion
2
...domain/repository/BookmarkRepository.java → ...itory/techArticle/BookmarkRepository.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
15 changes: 15 additions & 0 deletions
15
...reamypatisiel/devdevdev/domain/repository/techArticle/TechArticleRecommendRepository.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,15 @@ | ||
| package com.dreamypatisiel.devdevdev.domain.repository.techArticle; | ||
|
|
||
| import com.dreamypatisiel.devdevdev.domain.entity.AnonymousMember; | ||
| import com.dreamypatisiel.devdevdev.domain.entity.Member; | ||
| import com.dreamypatisiel.devdevdev.domain.entity.TechArticle; | ||
| import com.dreamypatisiel.devdevdev.domain.entity.TechArticleRecommend; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public interface TechArticleRecommendRepository extends JpaRepository<TechArticleRecommend, Long> { | ||
| Optional<TechArticleRecommend> findByTechArticleAndMember(TechArticle techArticle, Member member); | ||
|
|
||
| Optional<TechArticleRecommend> findByTechArticleAndAnonymousMember(TechArticle techArticle, AnonymousMember anonymousMember); | ||
| } |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/dreamypatisiel/devdevdev/domain/service/member/AnonymousMemberService.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,34 @@ | ||
| package com.dreamypatisiel.devdevdev.domain.service.member; | ||
|
|
||
| import com.dreamypatisiel.devdevdev.domain.entity.AnonymousMember; | ||
| import com.dreamypatisiel.devdevdev.domain.repository.member.AnonymousMemberRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
| import org.springframework.util.StringUtils; | ||
|
|
||
| import static com.dreamypatisiel.devdevdev.domain.exception.PickExceptionMessage.INVALID_ANONYMOUS_MEMBER_ID_MESSAGE; | ||
|
|
||
| @Transactional(readOnly = true) | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class AnonymousMemberService { | ||
|
|
||
| private final AnonymousMemberRepository anonymousMemberRepository; | ||
|
|
||
| @Transactional | ||
| public AnonymousMember findOrCreateAnonymousMember(String anonymousMemberId) { | ||
| // 익명 사용자 검증 | ||
| validateAnonymousMemberId(anonymousMemberId); | ||
|
|
||
| // 익명회원 조회 또는 생성 | ||
| return anonymousMemberRepository.findByAnonymousMemberId(anonymousMemberId) | ||
| .orElseGet(() -> anonymousMemberRepository.save(AnonymousMember.create(anonymousMemberId))); | ||
| } | ||
|
|
||
| private void validateAnonymousMemberId(String anonymousMemberId) { | ||
| if (!StringUtils.hasText(anonymousMemberId)) { | ||
| throw new IllegalArgumentException(INVALID_ANONYMOUS_MEMBER_ID_MESSAGE); | ||
| } | ||
| } | ||
| } |
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
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.
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.
TechArticleRecommend 클래스에 TechArticle 관련 연관관계 편의 메소드가 있어야 할 것 같아요~!
양방향인 경우에 연관관계 편의 메소드가 있는것이 편하고 실수를 방지합니당.
아래 처럼 서로 관계를 맺어주는 메소드가 필요 합니다~!
e.g)
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.
changeTechArticle() 메서드에 techArticle.getRecommends().add(this); 이 코드 추가하겠습니다!