Added solution
This commit is contained in:
parent
704c0d0fcd
commit
497807c006
5 changed files with 97 additions and 27 deletions
|
@ -1,14 +1,23 @@
|
|||
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.MutationMapping;
|
||||
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 +28,46 @@ public class GraphQLAPI implements QueryResolver {
|
|||
@Nonnull
|
||||
@Override
|
||||
@QueryMapping
|
||||
public HelloWorldDO helloWorld() {
|
||||
return new HelloWorldDO();
|
||||
public List<PostDO> posts() {
|
||||
return blogController.getPosts().stream().map(PostMapper::map).toList();
|
||||
}
|
||||
|
||||
@SchemaMapping
|
||||
public String message(final HelloWorldDO helloWorld) {
|
||||
return "Hello World!";
|
||||
@Override
|
||||
@MutationMapping
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}*/
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
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
|
||||
}
|
||||
|
||||
# TODO 1: Add type definitions for the blog service
|
Loading…
Reference in a new issue