Skip to content

Commit

Permalink
[feat] #5 게시물 엔티티 클래스 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
SunwoongH committed Nov 9, 2023
1 parent c9236da commit b616725
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/main/java/org/sopt/seminar/domain/post/domain/Post.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.sopt.seminar.domain.post.domain;

import jakarta.persistence.*;
import lombok.*;
import org.sopt.seminar.domain.member.domain.Member;
import org.sopt.seminar.global.common.BaseTimeEntity;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder(access = AccessLevel.PRIVATE)
@Getter
@Entity
public class Post extends BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "post_id")
private Long id;
private String title;
private String content;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id")
private Category category;

public static Post createPost(final String title, final String content, final Category category, final Member member) {
Post post = Post.builder()
.title(title)
.content(content)
.build();
post.changeMember(member);
post.changeCategory(category);
return post;
}

public void changeMember(Member member) {
if (this.member != null) {
this.member.removePost(this);
}
this.member = member;
member.addPost(this);
}

public void changeCategory(Category category) {
if (this.category != null) {
this.category.removePost(this);
}
this.category = category;
category.addPost(this);
}

public void updateTitleAndContent(String title, String content) {
updateTitle(title);
updateContent(content);
}

public void updateTitle(String title) {
this.title = title;
}

public void updateContent(String content) {
this.content = content;
}
}

0 comments on commit b616725

Please sign in to comment.