Added task06
This commit is contained in:
parent
70bc689ad5
commit
a23e7e74dc
24 changed files with 1105 additions and 0 deletions
|
@ -0,0 +1,13 @@
|
|||
package de.hftstuttgart.vs.task06;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Task06Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Task06Application.class, args);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package de.hftstuttgart.vs.task06.api.graphql;
|
||||
|
||||
import de.hftstuttgart.vs.task06.api.graphql.model.HelloWorldDO;
|
||||
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;
|
||||
|
||||
@Controller
|
||||
public class GraphQLAPI implements QueryResolver {
|
||||
|
||||
private final BlogController blogController;
|
||||
|
||||
public GraphQLAPI(final BlogController blogController) {
|
||||
this.blogController = blogController;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
@QueryMapping
|
||||
public HelloWorldDO helloWorld() {
|
||||
return new HelloWorldDO();
|
||||
}
|
||||
|
||||
@SchemaMapping
|
||||
public String message(final HelloWorldDO helloWorld) {
|
||||
return "Hello World!";
|
||||
}
|
||||
|
||||
//TODO 3: Add mappings for queries and mutations defined in your schema
|
||||
|
||||
//TODO 5: Add field mapping for count of comments for a post
|
||||
}
|
|
@ -0,0 +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;
|
||||
|
||||
public class CommentMapper {
|
||||
|
||||
/*public static CommentDO map(final Comment comment) {
|
||||
return CommentDO.builder()
|
||||
.setText(comment.getText())
|
||||
.build();
|
||||
}*/
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
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;
|
||||
public class PostMapper {
|
||||
|
||||
/*public static PostDO map(final Post post) {
|
||||
return PostDO.builder()
|
||||
.setPostID(post.getPostID())
|
||||
.setText(post.getText())
|
||||
.setState(PostStateMapper.map(post.getState()))
|
||||
.build();
|
||||
}*/
|
||||
|
||||
}
|
|
@ -0,0 +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;
|
||||
|
||||
public class PostStateMapper {
|
||||
|
||||
/*public static PostStateDO map(final PostState state) {
|
||||
return PostStateDO.valueOf(state.toString());
|
||||
}
|
||||
|
||||
public static PostState map(final PostStateDO state) {
|
||||
return PostState.valueOf(state.toString());
|
||||
}*/
|
||||
}
|
|
@ -0,0 +1,99 @@
|
|||
package de.hftstuttgart.vs.task06.bm;
|
||||
|
||||
import de.hftstuttgart.vs.task06.bm.exceptions.PostNotFound;
|
||||
import de.hftstuttgart.vs.task06.bm.exceptions.UserNotFound;
|
||||
import de.hftstuttgart.vs.task06.bm.model.Comment;
|
||||
import de.hftstuttgart.vs.task06.bm.model.Post;
|
||||
import de.hftstuttgart.vs.task06.bm.model.PostState;
|
||||
import de.hftstuttgart.vs.task06.bm.model.User;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class BlogController implements UserRepository, PostRepository {
|
||||
|
||||
private final Map<String, User> users;
|
||||
private final Map<Integer, Post> posts;
|
||||
private int nextID = 1;
|
||||
|
||||
public BlogController() {
|
||||
users = new HashMap<>();
|
||||
users.put("mmustermann", new User("mmustermann", "Max", "Mustermann"));
|
||||
users.put("jdoe", new User("jdoe", "John", "Doe"));
|
||||
posts = new HashMap<>();
|
||||
posts.put(1, new Post(1, "This is an example post", PostState.PUBLISHED, "mmustermann", new ArrayList<>()));
|
||||
posts.get(1).getComments().add(new Comment("Great post!", "jdoe"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Post> getPosts() {
|
||||
return posts.values().stream().toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Post getPost(final int postID) {
|
||||
return posts.get(postID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Post addPost(final String userName, final String text) {
|
||||
final var postID = nextID++;
|
||||
final var post = new Post(postID, text, PostState.UNPUBLISHED, userName, new ArrayList<>());
|
||||
posts.put(postID, post);
|
||||
return post;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Post editPost(final int postID, final String text) throws PostNotFound {
|
||||
if (!posts.containsKey(postID)) {
|
||||
throw new PostNotFound();
|
||||
}
|
||||
final var post = posts.get(postID);
|
||||
post.setText(text);
|
||||
return post;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Post changePostState(final int postID, final PostState state) throws PostNotFound {
|
||||
if (!posts.containsKey(postID)) {
|
||||
throw new PostNotFound();
|
||||
}
|
||||
final var post = posts.get(postID);
|
||||
post.setState(state);
|
||||
return post;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Comment addComment(final int postID, final String userName, final String commentText) throws PostNotFound {
|
||||
if (!posts.containsKey(postID)) {
|
||||
throw new PostNotFound();
|
||||
}
|
||||
final var post = posts.get(postID);
|
||||
final var comment = new Comment(commentText, userName);
|
||||
post.addComment(comment);
|
||||
return comment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<User> getUsers() {
|
||||
return users.values().stream().toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public User getUser(final String userName) throws UserNotFound {
|
||||
final var user = users.get(userName);
|
||||
if (user == null) {
|
||||
throw new UserNotFound();
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
public int getCommentCount(final int postID) {
|
||||
throw new RuntimeException("NOT IMPLEMENTED");
|
||||
//TODO 4: implement method counting the comments for a post
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package de.hftstuttgart.vs.task06.bm;
|
||||
|
||||
import de.hftstuttgart.vs.task06.bm.exceptions.PostNotFound;
|
||||
import de.hftstuttgart.vs.task06.bm.model.Comment;
|
||||
import de.hftstuttgart.vs.task06.bm.model.Post;
|
||||
import de.hftstuttgart.vs.task06.bm.model.PostState;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PostRepository {
|
||||
|
||||
List<Post> getPosts();
|
||||
|
||||
Post getPost(int postID);
|
||||
|
||||
Post addPost(String userName, String text);
|
||||
|
||||
Post editPost(int postID, String text) throws PostNotFound;
|
||||
|
||||
Post changePostState(int postID, PostState state) throws PostNotFound;
|
||||
|
||||
Comment addComment(int postID, String userName, String comment) throws PostNotFound;
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package de.hftstuttgart.vs.task06.bm;
|
||||
|
||||
import de.hftstuttgart.vs.task06.bm.exceptions.UserNotFound;
|
||||
import de.hftstuttgart.vs.task06.bm.model.User;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface UserRepository {
|
||||
|
||||
List<User> getUsers();
|
||||
|
||||
User getUser(String userName) throws UserNotFound;
|
||||
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package de.hftstuttgart.vs.task06.bm.exceptions;
|
||||
|
||||
public class PostNotFound extends Exception {
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package de.hftstuttgart.vs.task06.bm.exceptions;
|
||||
|
||||
public class UserNotFound extends Exception {
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package de.hftstuttgart.vs.task06.bm.model;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public final class Comment {
|
||||
private String text;
|
||||
private String userName;
|
||||
|
||||
public Comment(final String text, final String userName) {
|
||||
this.text = text;
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(final String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUserName(final String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
if (obj == this) return true;
|
||||
if (obj == null || obj.getClass() != this.getClass()) return false;
|
||||
final var that = (Comment) obj;
|
||||
return Objects.equals(this.text, that.text) &&
|
||||
Objects.equals(this.userName, that.userName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(text, userName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Comment[" +
|
||||
"text=" + text + ", " +
|
||||
"userName=" + userName + ']';
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
package de.hftstuttgart.vs.task06.bm.model;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public final class Post {
|
||||
private final int postID;
|
||||
private final String userName;
|
||||
private final List<Comment> comments;
|
||||
private String text;
|
||||
private PostState state;
|
||||
|
||||
public Post(final int postID, final String text, final PostState state, final String userName,
|
||||
final List<Comment> comments) {
|
||||
this.postID = postID;
|
||||
this.text = text;
|
||||
this.state = state;
|
||||
this.userName = userName;
|
||||
this.comments = comments;
|
||||
}
|
||||
|
||||
public int getPostID() {
|
||||
return postID;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(final String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public PostState getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(final PostState state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public List<Comment> getComments() {
|
||||
return comments;
|
||||
}
|
||||
|
||||
public void addComment(final Comment comment) {
|
||||
comments.add(comment);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
if (obj == this) return true;
|
||||
if (obj == null || obj.getClass() != this.getClass()) return false;
|
||||
final var that = (Post) obj;
|
||||
return Objects.equals(this.text, that.text) &&
|
||||
Objects.equals(this.state, that.state) && Objects.equals(this.userName, that.userName) &&
|
||||
Objects.equals(this.comments, that.comments);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(text, state, userName, comments);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Post[" + "text=" + text + ", " + "state=" + state + ", " + "userName=" +
|
||||
userName + ", " + "comments=" + comments + ']';
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package de.hftstuttgart.vs.task06.bm.model;
|
||||
|
||||
public enum PostState {
|
||||
PUBLISHED, UNPUBLISHED, LOCKED
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
package de.hftstuttgart.vs.task06.bm.model;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public final class User {
|
||||
private final String userName;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public User(final String userName, final String firstName, final String lastName) {
|
||||
this.userName = userName;
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(final String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(final String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
if (obj == this) return true;
|
||||
if (obj == null || obj.getClass() != this.getClass()) return false;
|
||||
final var that = (User) obj;
|
||||
return Objects.equals(this.userName, that.userName) &&
|
||||
Objects.equals(this.firstName, that.firstName) &&
|
||||
Objects.equals(this.lastName, that.lastName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(userName, firstName, lastName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User[" +
|
||||
"userName=" + userName + ", " +
|
||||
"firstName=" + firstName + ", " +
|
||||
"lastName=" + lastName + ']';
|
||||
}
|
||||
|
||||
}
|
1
task06/src/main/resources/application.properties
Normal file
1
task06/src/main/resources/application.properties
Normal file
|
@ -0,0 +1 @@
|
|||
spring.graphql.graphiql.enabled=true
|
11
task06/src/main/resources/graphql/schema.graphqls
Normal file
11
task06/src/main/resources/graphql/schema.graphqls
Normal file
|
@ -0,0 +1,11 @@
|
|||
type Query {
|
||||
helloWorld: HelloWorld!
|
||||
}
|
||||
|
||||
type HelloWorld {
|
||||
message: String
|
||||
}
|
||||
|
||||
# TODO 1: Add type definitions for the blog service
|
||||
|
||||
# TODO 2: Add queries and mutations with the corresponding parameters and return types
|
|
@ -0,0 +1,13 @@
|
|||
package de.hftstuttgart.vs.task06;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class Task06ApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue