From 9721b9dfa732266137da7915f43dce903bae1c7e Mon Sep 17 00:00:00 2001 From: Julius Freudenberger Date: Fri, 5 May 2023 10:53:14 +0200 Subject: [PATCH] Added solution --- .../vs/task06/api/graphql/GraphQLAPI.java | 56 ++++++++++++++++--- .../api/graphql/mapper/CommentMapper.java | 8 +-- .../task06/api/graphql/mapper/PostMapper.java | 9 +-- .../api/graphql/mapper/PostStateMapper.java | 8 +-- .../main/resources/graphql/schema.graphqls | 42 ++++++++++++-- 5 files changed, 96 insertions(+), 27 deletions(-) diff --git a/task06/src/main/java/de/hftstuttgart/vs/task06/api/graphql/GraphQLAPI.java b/task06/src/main/java/de/hftstuttgart/vs/task06/api/graphql/GraphQLAPI.java index 354bd79..eaad1e9 100644 --- a/task06/src/main/java/de/hftstuttgart/vs/task06/api/graphql/GraphQLAPI.java +++ b/task06/src/main/java/de/hftstuttgart/vs/task06/api/graphql/GraphQLAPI.java @@ -1,14 +1,22 @@ package de.hftstuttgart.vs.task06.api.graphql; -import de.hftstuttgart.vs.task06.api.graphql.model.HelloWorldDO; +import de.hftstuttgart.vs.task06.api.graphql.mapper.CommentMapper; +import de.hftstuttgart.vs.task06.api.graphql.mapper.PostMapper; +import de.hftstuttgart.vs.task06.api.graphql.mapper.PostStateMapper; +import de.hftstuttgart.vs.task06.api.graphql.model.CommentDO; +import de.hftstuttgart.vs.task06.api.graphql.model.CommentInputDO; +import de.hftstuttgart.vs.task06.api.graphql.model.PostDO; +import de.hftstuttgart.vs.task06.api.graphql.model.PostStateDO; import de.hftstuttgart.vs.task06.bm.BlogController; import jakarta.annotation.Nonnull; import org.springframework.graphql.data.method.annotation.QueryMapping; import org.springframework.graphql.data.method.annotation.SchemaMapping; import org.springframework.stereotype.Controller; +import java.util.List; + @Controller -public class GraphQLAPI implements QueryResolver { +public class GraphQLAPI implements QueryResolver, MutationResolver { private final BlogController blogController; @@ -19,16 +27,46 @@ public class GraphQLAPI implements QueryResolver { @Nonnull @Override @QueryMapping - public HelloWorldDO helloWorld() { - return new HelloWorldDO(); + public List posts() { + return blogController.getPosts().stream().map(PostMapper::map).toList(); } - @SchemaMapping - public String message(final HelloWorldDO helloWorld) { - return "Hello World!"; + @Override + @QueryMapping + public PostDO addPost(@Nonnull final String text, @Nonnull final String userName) { + final var post = blogController.addPost(userName, text); + return PostMapper.map(post); } - //TODO 2: Add mappings for queries and mutations defined in your schema + @Override + @QueryMapping + public PostDO editPost(final int postID, @Nonnull final String text) throws Exception { + final var post = blogController.editPost(postID, text); + return PostMapper.map(post); + } - //TODO 4: Add field mapping for count of comments for a post + @Override + @QueryMapping + public CommentDO addComment(final int postID, @Nonnull final CommentInputDO commentInput) throws Exception { + final var comment = blogController.addComment(postID, commentInput.getUserName(), commentInput.getText()); + return CommentMapper.map(comment); + } + + @Override + @QueryMapping + public PostDO changePostState(final int postID, @Nonnull final PostStateDO state) throws Exception { + final var post = blogController.changePostState(postID, PostStateMapper.map(state)); + return PostMapper.map(post); + } + + // Field mapping + @SchemaMapping(typeName = "Post") + public List comments(final PostDO post) { + return blogController.getPost(post.getPostID()).getComments().stream().map(CommentMapper::map).toList(); + } + + @SchemaMapping(typeName = "Post") + public int commentCount(final PostDO post) { + return blogController.getCommentCount(post.getPostID()); + } } diff --git a/task06/src/main/java/de/hftstuttgart/vs/task06/api/graphql/mapper/CommentMapper.java b/task06/src/main/java/de/hftstuttgart/vs/task06/api/graphql/mapper/CommentMapper.java index 71bfee3..8b2870b 100644 --- a/task06/src/main/java/de/hftstuttgart/vs/task06/api/graphql/mapper/CommentMapper.java +++ b/task06/src/main/java/de/hftstuttgart/vs/task06/api/graphql/mapper/CommentMapper.java @@ -1,13 +1,13 @@ package de.hftstuttgart.vs.task06.api.graphql.mapper; -//import de.hftstuttgart.vs.task06.bm.model.Comment; -//import de.hftstuttgart.vs.task06.api.graphql.model.CommentDO; +import de.hftstuttgart.vs.task06.api.graphql.model.CommentDO; +import de.hftstuttgart.vs.task06.bm.model.Comment; public class CommentMapper { - /*public static CommentDO map(final Comment comment) { + public static CommentDO map(final Comment comment) { return CommentDO.builder() .setText(comment.getText()) .build(); - }*/ + } } diff --git a/task06/src/main/java/de/hftstuttgart/vs/task06/api/graphql/mapper/PostMapper.java b/task06/src/main/java/de/hftstuttgart/vs/task06/api/graphql/mapper/PostMapper.java index 5f3b538..0e9cf2c 100644 --- a/task06/src/main/java/de/hftstuttgart/vs/task06/api/graphql/mapper/PostMapper.java +++ b/task06/src/main/java/de/hftstuttgart/vs/task06/api/graphql/mapper/PostMapper.java @@ -1,15 +1,16 @@ package de.hftstuttgart.vs.task06.api.graphql.mapper; -//import de.hftstuttgart.vs.task06.api.graphql.model.PostDO; -//import de.hftstuttgart.vs.task06.bm.model.Post; +import de.hftstuttgart.vs.task06.api.graphql.model.PostDO; +import de.hftstuttgart.vs.task06.bm.model.Post; + public class PostMapper { - /*public static PostDO map(final Post post) { + public static PostDO map(final Post post) { return PostDO.builder() .setPostID(post.getPostID()) .setText(post.getText()) .setState(PostStateMapper.map(post.getState())) .build(); - }*/ + } } diff --git a/task06/src/main/java/de/hftstuttgart/vs/task06/api/graphql/mapper/PostStateMapper.java b/task06/src/main/java/de/hftstuttgart/vs/task06/api/graphql/mapper/PostStateMapper.java index d4ee701..5e9a258 100644 --- a/task06/src/main/java/de/hftstuttgart/vs/task06/api/graphql/mapper/PostStateMapper.java +++ b/task06/src/main/java/de/hftstuttgart/vs/task06/api/graphql/mapper/PostStateMapper.java @@ -1,15 +1,15 @@ package de.hftstuttgart.vs.task06.api.graphql.mapper; -//import de.hftstuttgart.vs.task06.api.graphql.model.PostStateDO; -//import de.hftstuttgart.vs.task06.bm.model.PostState; +import de.hftstuttgart.vs.task06.api.graphql.model.PostStateDO; +import de.hftstuttgart.vs.task06.bm.model.PostState; public class PostStateMapper { - /*public static PostStateDO map(final PostState state) { + public static PostStateDO map(final PostState state) { return PostStateDO.valueOf(state.toString()); } public static PostState map(final PostStateDO state) { return PostState.valueOf(state.toString()); - }*/ + } } diff --git a/task06/src/main/resources/graphql/schema.graphqls b/task06/src/main/resources/graphql/schema.graphqls index 4200aca..72f5b80 100644 --- a/task06/src/main/resources/graphql/schema.graphqls +++ b/task06/src/main/resources/graphql/schema.graphqls @@ -1,9 +1,39 @@ +type Post { + postID: Int! + text: String! + state: PostState! + user: User! + commentCount: Int! + comments: [Comment!]! +} + +enum PostState { + PUBLISHED, UNPUBLISHED, LOCKED +} + +type User { + userName: String! + firstName: String + lastName: String +} + +type Comment { + text: String! + user: User! +} + +input CommentInput { + text: String! + userName: String! +} + type Query { - helloWorld: HelloWorld! + posts: [Post!]! } -type HelloWorld { - message: String -} - -# TODO 1: Add type definitions for the blog service \ No newline at end of file +type Mutation { + addPost(text: String!, userName: String!): Post + editPost(postID: Int!, text: String!): Post + addComment(postID: Int!, comment: CommentInput!): Comment + changePostState(postID: Int!, state: PostState!): Post +} \ No newline at end of file