Added solution

This commit is contained in:
Julius Freudenberger 2023-05-05 10:53:14 +02:00
parent 704c0d0fcd
commit 497807c006
5 changed files with 97 additions and 27 deletions

View file

@ -1,14 +1,23 @@
package de.hftstuttgart.vs.task06.api.graphql; 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 de.hftstuttgart.vs.task06.bm.BlogController;
import jakarta.annotation.Nonnull; import jakarta.annotation.Nonnull;
import org.springframework.graphql.data.method.annotation.MutationMapping;
import org.springframework.graphql.data.method.annotation.QueryMapping; import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.graphql.data.method.annotation.SchemaMapping; import org.springframework.graphql.data.method.annotation.SchemaMapping;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import java.util.List;
@Controller @Controller
public class GraphQLAPI implements QueryResolver { public class GraphQLAPI implements QueryResolver, MutationResolver {
private final BlogController blogController; private final BlogController blogController;
@ -19,16 +28,46 @@ public class GraphQLAPI implements QueryResolver {
@Nonnull @Nonnull
@Override @Override
@QueryMapping @QueryMapping
public HelloWorldDO helloWorld() { public List<PostDO> posts() {
return new HelloWorldDO(); return blogController.getPosts().stream().map(PostMapper::map).toList();
} }
@SchemaMapping @Override
public String message(final HelloWorldDO helloWorld) { @MutationMapping
return "Hello World!"; 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
@MutationMapping
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
@MutationMapping
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
@MutationMapping
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<CommentDO> 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());
}
} }

View file

@ -1,13 +1,13 @@
package de.hftstuttgart.vs.task06.api.graphql.mapper; 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 class CommentMapper {
/*public static CommentDO map(final Comment comment) { public static CommentDO map(final Comment comment) {
return CommentDO.builder() return CommentDO.builder()
.setText(comment.getText()) .setText(comment.getText())
.build(); .build();
}*/ }
} }

View file

@ -1,15 +1,16 @@
package de.hftstuttgart.vs.task06.api.graphql.mapper; package de.hftstuttgart.vs.task06.api.graphql.mapper;
//import de.hftstuttgart.vs.task06.api.graphql.model.PostDO; import de.hftstuttgart.vs.task06.api.graphql.model.PostDO;
//import de.hftstuttgart.vs.task06.bm.model.Post; import de.hftstuttgart.vs.task06.bm.model.Post;
public class PostMapper { public class PostMapper {
/*public static PostDO map(final Post post) { public static PostDO map(final Post post) {
return PostDO.builder() return PostDO.builder()
.setPostID(post.getPostID()) .setPostID(post.getPostID())
.setText(post.getText()) .setText(post.getText())
.setState(PostStateMapper.map(post.getState())) .setState(PostStateMapper.map(post.getState()))
.build(); .build();
}*/ }
} }

View file

@ -1,15 +1,15 @@
package de.hftstuttgart.vs.task06.api.graphql.mapper; package de.hftstuttgart.vs.task06.api.graphql.mapper;
//import de.hftstuttgart.vs.task06.api.graphql.model.PostStateDO; import de.hftstuttgart.vs.task06.api.graphql.model.PostStateDO;
//import de.hftstuttgart.vs.task06.bm.model.PostState; import de.hftstuttgart.vs.task06.bm.model.PostState;
public class PostStateMapper { public class PostStateMapper {
/*public static PostStateDO map(final PostState state) { public static PostStateDO map(final PostState state) {
return PostStateDO.valueOf(state.toString()); return PostStateDO.valueOf(state.toString());
} }
public static PostState map(final PostStateDO state) { public static PostState map(final PostStateDO state) {
return PostState.valueOf(state.toString()); return PostState.valueOf(state.toString());
}*/ }
} }

View file

@ -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 { type Query {
helloWorld: HelloWorld! posts: [Post!]!
} }
type HelloWorld { type Mutation {
message: String addPost(text: String!, userName: String!): Post
editPost(postID: Int!, text: String!): Post
addComment(postID: Int!, comment: CommentInput!): Comment
changePostState(postID: Int!, state: PostState!): Post
} }
# TODO 1: Add type definitions for the blog service