Skip to content

Commit

Permalink
feat: RecommendedPost 엔티티 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
nuyh99 committed Jul 20, 2023
1 parent 6fc9e49 commit b0b2c5d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 8 deletions.
11 changes: 3 additions & 8 deletions backend/src/main/java/wooteco/prolog/roadmap/domain/Keyword.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<String> references;
@OneToMany(mappedBy = "keyword", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
private List<RecommendedPost> recommendedPosts = new ArrayList<>();

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id")
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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)
);

0 comments on commit b0b2c5d

Please sign in to comment.