-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat : add post details table and update endpoints
- Loading branch information
1 parent
39384b7
commit 5abf4a8
Showing
13 changed files
with
264 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...oot-graphql-querydsl/src/main/java/com/example/graphql/querydsl/entities/PostDetails.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
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 = "PostDetails") | ||
@Table(name = "post_details") | ||
@Getter | ||
@Setter | ||
public class PostDetails { | ||
|
||
@Id | ||
private Long id; | ||
|
||
@Column(name = "created_on", nullable = false, updatable = false) | ||
private LocalDateTime createdOn; | ||
|
||
@Column(name = "created_by", nullable = false, updatable = false) | ||
private String createdBy; | ||
|
||
@OneToOne(fetch = FetchType.LAZY) | ||
@MapsId | ||
private Post post; | ||
|
||
public PostDetails setId(Long id) { | ||
this.id = id; | ||
return this; | ||
} | ||
|
||
public PostDetails setCreatedOn(LocalDateTime createdOn) { | ||
this.createdOn = createdOn; | ||
return this; | ||
} | ||
|
||
public PostDetails setCreatedBy(String createdBy) { | ||
this.createdBy = createdBy; | ||
return this; | ||
} | ||
|
||
public PostDetails setPost(Post post) { | ||
this.post = post; | ||
return this; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (obj == null) { | ||
return false; | ||
} | ||
if (getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
PostDetails other = (PostDetails) obj; | ||
return Objects.equals(this.createdBy, other.createdBy); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(this.createdBy); | ||
} | ||
} |
15 changes: 12 additions & 3 deletions
15
...l/boot-graphql-querydsl/src/main/java/com/example/graphql/querydsl/mapper/PostMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,32 @@ | ||
package com.example.graphql.querydsl.mapper; | ||
|
||
import com.example.graphql.querydsl.entities.Post; | ||
import com.example.graphql.querydsl.entities.PostDetails; | ||
import com.example.graphql.querydsl.model.request.CreatePostRequest; | ||
import com.example.graphql.querydsl.model.request.PostRequest; | ||
import com.example.graphql.querydsl.model.response.PostResponse; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import org.mapstruct.*; | ||
|
||
@Mapper | ||
public interface PostMapper { | ||
|
||
@Mapping(target = "id", ignore = true) | ||
@Mapping(target = "createdOn", expression = "java(java.time.LocalDateTime.now())") | ||
Post toEntity(PostRequest postRequest); | ||
Post toEntity(CreatePostRequest createPostRequest); | ||
|
||
@Mapping(target = "id", ignore = true) | ||
@Mapping(target = "createdOn", ignore = true) | ||
// @Mapping(target = "createdOn", ignore = true) | ||
void mapPostWithRequest(PostRequest postRequest, @MappingTarget Post post); | ||
|
||
@Mapping(target = "createdOn", source = "details.createdOn") | ||
PostResponse toResponse(Post post); | ||
|
||
List<PostResponse> toResponseList(List<Post> postList); | ||
|
||
@AfterMapping | ||
default void setAfterMappingToPost(CreatePostRequest createPostRequest, @MappingTarget Post post) { | ||
post.addDetails( | ||
new PostDetails().setCreatedBy(createPostRequest.createdBy()).setCreatedOn(LocalDateTime.now())); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...-querydsl/src/main/java/com/example/graphql/querydsl/model/request/CreatePostRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.example.graphql.querydsl.model.request; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotEmpty; | ||
|
||
public record CreatePostRequest( | ||
@NotEmpty(message = "Title cannot be empty") String title, | ||
@NotBlank(message = "Content cannot be blank") String content, | ||
@NotBlank(message = "CreatedBy cannot be blank") String createdBy) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
9 changes: 0 additions & 9 deletions
9
graphql/boot-graphql-querydsl/src/main/resources/db/changelog/migration/01-init.xml
This file was deleted.
Oops, something went wrong.
35 changes: 35 additions & 0 deletions
35
...aphql-querydsl/src/main/resources/db/changelog/migration/03-create_post_details_table.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?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_details"> | ||
<createTable tableName="post_details"> | ||
<column name="post_id" type="BIGINT"> | ||
<constraints nullable="false" primaryKey="true" primaryKeyName="pk_post_details"/> | ||
</column> | ||
<column name="created_on" type="DATETIME"> | ||
<constraints nullable="false"/> | ||
</column> | ||
<column name="created_by" type="${string.type}"> | ||
<constraints nullable="false"/> | ||
</column> | ||
</createTable> | ||
</changeSet> | ||
|
||
<changeSet id="add_foreign_key_post_id" author="app"> | ||
<addForeignKeyConstraint baseColumnNames="post_id" baseTableName="post_details" | ||
constraintName="FK_POST_DETAILS_ON_POST" referencedColumnNames="id" | ||
referencedTableName="posts"/> | ||
</changeSet> | ||
<changeSet id="add_foreign_key_details_id" author="app"> | ||
<addForeignKeyConstraint baseColumnNames="details_id" baseTableName="posts" constraintName="FK_POST_ON_DETAILS" | ||
referencedColumnNames="post_id" referencedTableName="post_details"/> | ||
</changeSet> | ||
</databaseChangeLog> |
12 changes: 11 additions & 1 deletion
12
...oot-graphql-querydsl/src/test/java/com/example/graphql/querydsl/SchemaValidationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,24 @@ | ||
package com.example.graphql.querydsl; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.example.graphql.querydsl.common.ContainersConfig; | ||
import com.zaxxer.hikari.HikariDataSource; | ||
import javax.sql.DataSource; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
import org.springframework.context.annotation.Import; | ||
|
||
@DataJpaTest(properties = {"spring.jpa.hibernate.ddl-auto=validate", "spring.test.database.replace=none"}) | ||
@Import(ContainersConfig.class) | ||
class SchemaValidationTest { | ||
|
||
@Autowired | ||
private DataSource dataSource; | ||
|
||
@Test | ||
void validateJpaMappingsWithDbSchema() {} | ||
void validateJpaMappingsWithDbSchema() { | ||
assertThat(dataSource).isInstanceOf(HikariDataSource.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.