Skip to content

Commit

Permalink
feat : adds postTags entity
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Nov 10, 2023
1 parent f0b3882 commit 5860cbc
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 3 deletions.
2 changes: 1 addition & 1 deletion graphql/boot-graphql-querydsl/.yo-rc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"features": [],
"buildTool": "maven",
"packageFolder": "com/example/graphql/querydsl",
"liquibaseMigrationCounter": 4
"liquibaseMigrationCounter": 5
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -33,6 +34,9 @@ public class Post {
@OneToOne(cascade = CascadeType.ALL, mappedBy = "post", orphanRemoval = true, fetch = FetchType.LAZY)
private PostDetails details;

@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
private List<PostTag> tags = new ArrayList<>();

public Post setId(Long id) {
this.id = id;
return this;
Expand Down Expand Up @@ -61,6 +65,14 @@ public Post setDetails(PostDetails details) {
return this;
}

public Post setTags(List<PostTag> postTags) {
if (postTags == null) {
postTags = new ArrayList<>();
}
this.tags = postTags;
return this;
}

public void addComment(PostComment comment) {
this.comments.add(comment);
comment.setPost(this);
Expand All @@ -81,6 +93,23 @@ public void removeDetails() {
this.details = null;
}

public void addTag(Tag tag) {
PostTag postTag = new PostTag(this, tag);
this.tags.add(postTag);
}

public void removeTag(Tag tag) {
for (Iterator<PostTag> iterator = this.tags.iterator(); iterator.hasNext(); ) {
PostTag postTag = iterator.next();

if (postTag.getPost().equals(this) && postTag.getTag().equals(tag)) {
iterator.remove();
postTag.setPost(null);
postTag.setTag(null);
}
}
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.example.graphql.querydsl.entities;

import jakarta.persistence.*;
import java.time.LocalDateTime;
import java.util.Objects;
import lombok.Getter;
import lombok.Setter;

@Entity(name = "PostTag")
@Table(name = "post_tags")
@Setter
@Getter
public class PostTag {

@EmbeddedId
private PostTagId id;

@ManyToOne(fetch = FetchType.LAZY)
@MapsId("postId")
private Post post;

@ManyToOne(fetch = FetchType.LAZY)
@MapsId("tagId")
private Tag tag;

@Column(name = "created_on")
private LocalDateTime createdOn = LocalDateTime.now();

public PostTag(Post post, Tag tag) {
this.post = post;
this.tag = tag;
this.id = new PostTagId(post.getId(), tag.getId());
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (o == null || getClass() != o.getClass()) {
return false;
}
PostTag that = (PostTag) o;
return Objects.equals(this.post, that.post) && Objects.equals(this.tag, that.tag);
}

@Override
public int hashCode() {
return Objects.hash(this.post, this.tag);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.example.graphql.querydsl.entities;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import java.io.Serial;
import java.io.Serializable;
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Embeddable
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class PostTagId implements Serializable {

@Serial
private static final long serialVersionUID = 1L;

@Column(name = "post_id")
private Long postId;

@Column(name = "tag_id")
private Long tagId;

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (o == null || getClass() != o.getClass()) {
return false;
}

PostTagId that = (PostTagId) o;
return Objects.equals(this.postId, that.postId) && Objects.equals(this.tagId, that.tagId);
}

@Override
public int hashCode() {
return Objects.hash(this.postId, this.tagId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public interface PostMapper {
void mapPostWithRequest(UpdatePostRequest updatePostRequest, @MappingTarget Post post);

@Mapping(target = "createdOn", source = "details.createdOn")
@Mapping(target = "postCommentResponses", source = "comments")
PostResponse toResponse(Post post);

List<PostResponse> toResponseList(List<Post> postList);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://docs.liquibase.com/concepts/changelogs/xml-format.html -->
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.20.xsd">

<property name="string.type" value="varchar(255)" dbms="!postgresql"/>
<property name="string.type" value="text" dbms="postgresql"/>

<changeSet author="app" id="createTable-post_tags">
<createTable tableName="post_tags">
<column name="created_on" type="DATETIME"/>
<column name="post_id" type="BIGINT">
<constraints nullable="false" primaryKey="true" primaryKeyName="pk_post_tag"/>
</column>
<column name="tag_id" type="BIGINT">
<constraints nullable="false" primaryKey="true" primaryKeyName="pk_post_tag"/>
</column>
</createTable>

<addForeignKeyConstraint baseColumnNames="post_id" baseTableName="post_tags" constraintName="FK_POST_TAG_ON_POST"
referencedColumnNames="id" referencedTableName="posts"/>
<addForeignKeyConstraint baseColumnNames="tag_id" baseTableName="post_tags" constraintName="FK_POST_TAG_ON_TAG"
referencedColumnNames="id" referencedTableName="tags"/>
</changeSet>
</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void shouldCreateNewPost() throws Exception {
"New Post",
"New Content",
"Junit",
List.of(new PostCommentRequest("New Review"), new PostCommentRequest("Second Review")));
List.of(new PostCommentRequest("First Review"), new PostCommentRequest("Second Review")));
this.mockMvc
.perform(post("/api/posts")
.contentType(MediaType.APPLICATION_JSON)
Expand All @@ -89,7 +89,14 @@ void shouldCreateNewPost() throws Exception {
.andExpect(header().exists(HttpHeaders.LOCATION))
.andExpect(jsonPath("$.id", notNullValue()))
.andExpect(jsonPath("$.title", is(postRequest.title())))
.andExpect(jsonPath("$.content", is(postRequest.content())));
.andExpect(jsonPath("$.content", is(postRequest.content())))
.andExpect(jsonPath(
"$.postCommentResponses.size()",
is(postRequest.postCommentRequests().size())))
.andExpect(jsonPath("$.postCommentResponses[0].id", notNullValue()))
.andExpect(jsonPath("$.postCommentResponses[0].review", is("First Review")))
.andExpect(jsonPath("$.postCommentResponses[1].id", notNullValue()))
.andExpect(jsonPath("$.postCommentResponses[1].review", is("Second Review")));
}

@Test
Expand Down

0 comments on commit 5860cbc

Please sign in to comment.