Skip to content

Commit

Permalink
feat : introduce fulent setters in entity
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Nov 9, 2023
1 parent 50da9a2 commit 5c28493
Show file tree
Hide file tree
Showing 15 changed files with 235 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,59 +28,51 @@ public void run(String... args) {
.forEach(
i -> {
PostCommentEntity post1Comment =
PostCommentEntity.builder()
.title("Sample Review" + i)
.content("Sample Content" + i)
.published(true)
.build();
new PostCommentEntity()
.setTitle("Sample Review" + i)
.setContent("Sample Content" + i)
.setPublished(true);
PostCommentEntity post1Comment2 =
PostCommentEntity.builder()
.title("Complicated Review" + i)
.content("Complicated Content" + i)
.published(false)
.build();
new PostCommentEntity()
.setTitle("Complicated Review" + i)
.setContent("Complicated Content" + i)
.setPublished(false);
PostDetailsEntity post1Details =
PostDetailsEntity.builder()
.createdBy("user" + i)
.detailsKey("key" + i)
.build();
new PostDetailsEntity()
.setCreatedBy("user" + i)
.setDetailsKey("key" + i);
PostEntity postEntity =
PostEntity.builder()
.title("Title" + i)
.content("content" + 1)
.published(true)
.build();
new PostEntity()
.setTitle("Title" + i)
.setContent("content" + 1)
.setPublished(true);
postEntity.setDetails(post1Details);
postEntity.addComment(post1Comment);
postEntity.addComment(post1Comment2);

PostEntity postEntity1 =
PostEntity.builder()
.title("Second Title" + i)
.content("Second Content" + 1)
.published(false)
.build();
new PostEntity()
.setTitle("Second Title" + i)
.setContent("Second Content" + 1)
.setPublished(false);
PostCommentEntity post2Comment =
PostCommentEntity.builder()
.title("Complicated Title" + i)
.content("Complicated Content" + i)
.published(true)
.publishedAt(OffsetDateTime.now())
.build();
new PostCommentEntity()
.setTitle("Complicated Title" + i)
.setContent("Complicated Content" + i)
.setPublished(true)
.setPublishedAt(OffsetDateTime.now());
PostDetailsEntity post2Details =
PostDetailsEntity.builder()
.createdBy("user" + i)
.detailsKey("keys" + i)
.build();
new PostDetailsEntity()
.setCreatedBy("user" + i)
.setDetailsKey("keys" + i);
postEntity1.setDetails(post2Details);
postEntity1.addComment(post2Comment);
AuthorEntity authorEntity =
AuthorEntity.builder()
.email("user" + i + "@example.com")
.firstName("first name" + i)
.lastName("last name" + i)
.mobile(9848922338L)
.build();
new AuthorEntity()
.setEmail("user" + i + "@example.com")
.setFirstName("first name" + i)
.setLastName("last name" + i)
.setMobile(9848922338L);
authorEntity.addPost(postEntity);
authorEntity.addPost(postEntity1);
this.authorRepository.save(authorEntity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,14 @@
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Table(name = "authors")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class AuthorEntity implements Serializable {

@Id
Expand All @@ -52,9 +48,56 @@ public class AuthorEntity implements Serializable {
@Version private Long version;

@OneToMany(mappedBy = "authorEntity", cascade = CascadeType.ALL, orphanRemoval = true)
@Builder.Default
private List<PostEntity> postEntities = new ArrayList<>();

public AuthorEntity setId(Long id) {
this.id = id;
return this;
}

public AuthorEntity setFirstName(String firstName) {
this.firstName = firstName;
return this;
}

public AuthorEntity setMiddleName(String middleName) {
this.middleName = middleName;
return this;
}

public AuthorEntity setLastName(String lastName) {
this.lastName = lastName;
return this;
}

public AuthorEntity setMobile(Long mobile) {
this.mobile = mobile;
return this;
}

public AuthorEntity setEmail(String email) {
this.email = email;
return this;
}

public AuthorEntity setRegisteredAt(LocalDateTime registeredAt) {
this.registeredAt = registeredAt;
return this;
}

public AuthorEntity setVersion(Long version) {
this.version = version;
return this;
}

public AuthorEntity setPostEntities(List<PostEntity> postEntities) {
if (postEntities == null) {
postEntities = new ArrayList<>();
}
this.postEntities = postEntities;
return this;
}

public void addPost(PostEntity postEntity) {
this.postEntities.add(postEntity);
postEntity.setAuthorEntity(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,13 @@
import java.time.OffsetDateTime;
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.Hibernate;

@Entity
@Table(name = "post_comments")
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PostCommentEntity extends Auditable implements Serializable {
Expand All @@ -45,6 +41,36 @@ public class PostCommentEntity extends Auditable implements Serializable {
@JoinColumn(name = "post_id")
private PostEntity postEntity;

public PostCommentEntity setId(Long id) {
this.id = id;
return this;
}

public PostCommentEntity setTitle(String title) {
this.title = title;
return this;
}

public PostCommentEntity setContent(String content) {
this.content = content;
return this;
}

public PostCommentEntity setPublished(boolean published) {
this.published = published;
return this;
}

public PostCommentEntity setPublishedAt(OffsetDateTime publishedAt) {
this.publishedAt = publishedAt;
return this;
}

public PostCommentEntity setPostEntity(PostEntity postEntity) {
this.postEntity = postEntity;
return this;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@
import java.io.Serializable;
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Table(name = "post_details")
@Getter
@Setter
@AllArgsConstructor
@Builder
@NoArgsConstructor
public class PostDetailsEntity extends Auditable implements Serializable {

Expand All @@ -37,8 +33,24 @@ public class PostDetailsEntity extends Auditable implements Serializable {
@JoinColumn(name = "id")
private PostEntity postEntity;

public PostDetailsEntity(String createdBy) {
public PostDetailsEntity setId(Long id) {
this.id = id;
return this;
}

public PostDetailsEntity setDetailsKey(String detailsKey) {
this.detailsKey = detailsKey;
return this;
}

public PostDetailsEntity setCreatedBy(String createdBy) {
this.createdBy = createdBy;
return this;
}

public PostDetailsEntity setPostEntity(PostEntity postEntity) {
this.postEntity = postEntity;
return this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,14 @@
import java.util.List;
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.Hibernate;

@Entity
@Table(name = "posts")
@Getter
@Setter
@AllArgsConstructor
@Builder
@NoArgsConstructor
public class PostEntity extends Auditable implements Serializable {

Expand All @@ -47,20 +43,64 @@ public class PostEntity extends Auditable implements Serializable {
private LocalDateTime publishedAt;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "postEntity", orphanRemoval = true)
@Builder.Default
private List<PostCommentEntity> comments = new ArrayList<>();

@OneToOne(mappedBy = "postEntity", cascade = CascadeType.ALL, optional = false)
private PostDetailsEntity details;

@OneToMany(mappedBy = "postEntity", cascade = CascadeType.ALL, orphanRemoval = true)
@Builder.Default
private List<PostTagEntity> tags = new ArrayList<>();

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "author_id")
private AuthorEntity authorEntity;

public PostEntity setId(Long id) {
this.id = id;
return this;
}

public PostEntity setTitle(String title) {
this.title = title;
return this;
}

public PostEntity setContent(String content) {
this.content = content;
return this;
}

public PostEntity setPublished(boolean published) {
this.published = published;
return this;
}

public PostEntity setPublishedAt(LocalDateTime publishedAt) {
this.publishedAt = publishedAt;
return this;
}

public PostEntity setComments(List<PostCommentEntity> comments) {
if (comments == null) {
comments = new ArrayList<>();
}
this.comments = comments;
return this;
}

public PostEntity setTags(List<PostTagEntity> tags) {
if (tags == null) {
tags = new ArrayList<>();
}
this.tags = tags;
return this;
}

public PostEntity setAuthorEntity(AuthorEntity authorEntity) {
this.authorEntity = authorEntity;
return this;
}

public void addComment(PostCommentEntity comment) {
this.comments.add(comment);
comment.setPostEntity(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,19 @@ public class TagEntity implements Serializable {

private String tagDescription;

public TagEntity(String tagName, String tagDescription) {
public TagEntity setId(Long id) {
this.id = id;
return this;
}

public TagEntity setTagName(String tagName) {
this.tagName = tagName;
return this;
}

public TagEntity setTagDescription(String tagDescription) {
this.tagDescription = tagDescription;
return this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ default TagEntity getTagEntity(TagRepository tagRepository, TagsRequest tagsRequ
.orElseGet(
() ->
tagRepository.save(
new TagEntity(
tagsRequest.tagName(),
tagsRequest.tagDescription())));
new TagEntity()
.setTagName(tagsRequest.tagName())
.setTagDescription(tagsRequest.tagDescription())));
}
}
Loading

0 comments on commit 5c28493

Please sign in to comment.