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 8518c806c..47e05b0f3 100644 --- a/backend/src/main/java/wooteco/prolog/roadmap/domain/Keyword.java +++ b/backend/src/main/java/wooteco/prolog/roadmap/domain/Keyword.java @@ -37,10 +37,7 @@ import java.util.Set; 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) @@ -65,10 +62,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) +);