From b0b2c5d2e411cbdaf0233544e2c64ba93e8ed615 Mon Sep 17 00:00:00 2001 From: nuyh Date: Thu, 20 Jul 2023 17:57:13 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20RecommendedPost=20=EC=97=94=ED=8B=B0?= =?UTF-8?q?=ED=8B=B0=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #1397 --- .../prolog/roadmap/domain/Keyword.java | 11 +--- .../roadmap/domain/RecommendedPost.java | 62 +++++++++++++++++++ .../V4__alter_table_keyword_reference.sql | 10 +++ 3 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 backend/src/main/java/wooteco/prolog/roadmap/domain/RecommendedPost.java create mode 100644 backend/src/main/resources/db/migration/prod/V4__alter_table_keyword_reference.sql diff --git a/backend/src/main/java/wooteco/prolog/roadmap/domain/Keyword.java b/backend/src/main/java/wooteco/prolog/roadmap/domain/Keyword.java index d58f5e52a..9a13e24ca 100644 --- a/backend/src/main/java/wooteco/prolog/roadmap/domain/Keyword.java +++ b/backend/src/main/java/wooteco/prolog/roadmap/domain/Keyword.java @@ -8,10 +8,7 @@ import wooteco.prolog.roadmap.exception.KeywordSeqException; import javax.persistence.*; -import java.util.HashSet; -import java.util.List; -import java.util.Objects; -import java.util.Set; +import java.util.*; @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) @@ -36,10 +33,8 @@ public class Keyword { @Column(name = "session_id", nullable = false) private Long sessionId; - @ElementCollection - @CollectionTable(name = "keyword_reference") - @Column(name = "url") - private List references; + @OneToMany(mappedBy = "keyword", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER) + private List recommendedPosts = new ArrayList<>(); @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "parent_id") diff --git a/backend/src/main/java/wooteco/prolog/roadmap/domain/RecommendedPost.java b/backend/src/main/java/wooteco/prolog/roadmap/domain/RecommendedPost.java new file mode 100644 index 000000000..3b28d789f --- /dev/null +++ b/backend/src/main/java/wooteco/prolog/roadmap/domain/RecommendedPost.java @@ -0,0 +1,62 @@ +package wooteco.prolog.roadmap.domain; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import javax.persistence.*; +import java.util.Objects; + +@Entity +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +@Getter +public class RecommendedPost { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(nullable = false) + private String url; + + @ManyToOne + @JoinColumn(nullable = false) + private Keyword keyword; + + public RecommendedPost(final String url) { + this(null, url, null); + } + + public void updateUrl(final String url) { + this.url = url; + } + + public void remove() { + if (this.keyword == null) { + return; + } + + keyword.getRecommendedPosts().remove(this); + this.keyword = null; + } + + public void addKeyword(final Keyword keyword) { + this.keyword = keyword; + keyword.getRecommendedPosts().add(this); + } + + @Override + public boolean equals(final Object o) { + if (this == o) return true; + if (!(o instanceof RecommendedPost)) return false; + final RecommendedPost post = (RecommendedPost) o; + return Objects.equals(id, post.id); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } +} diff --git a/backend/src/main/resources/db/migration/prod/V4__alter_table_keyword_reference.sql b/backend/src/main/resources/db/migration/prod/V4__alter_table_keyword_reference.sql new file mode 100644 index 000000000..0ff33a5b0 --- /dev/null +++ b/backend/src/main/resources/db/migration/prod/V4__alter_table_keyword_reference.sql @@ -0,0 +1,10 @@ +drop table prolog.keyword_reference; + +create table if not exists prolog.recommended_post +( + id bigint auto_increment primary key, + url varchar(255) not null, + keyword_id bigint not null, + constraint FK_KEYWORD_ID + foreign key (keyword_id) references prolog.keyword (id) +);