diff --git a/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/controller/BookController.java b/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/controller/BookController.java index 3b3636db..1f3e28f7 100644 --- a/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/controller/BookController.java +++ b/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/controller/BookController.java @@ -1,11 +1,14 @@ package com.keploy.springbootpostgresgraphql.controller; +import com.keploy.springbootpostgresgraphql.dto.AuthorInput; +import com.keploy.springbootpostgresgraphql.dto.BookInput; import com.keploy.springbootpostgresgraphql.entity.Author; import com.keploy.springbootpostgresgraphql.entity.Book; import com.keploy.springbootpostgresgraphql.repository.AuthorRepository; import com.keploy.springbootpostgresgraphql.repository.BookRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.graphql.data.method.annotation.Argument; +import org.springframework.graphql.data.method.annotation.MutationMapping; import org.springframework.graphql.data.method.annotation.QueryMapping; import org.springframework.stereotype.Controller; @@ -24,6 +27,11 @@ public Book getBookByName(@Argument String name) { return bookRepository.findBookByName(name); } + @QueryMapping + public Book getBookById(@Argument int id) { + return bookRepository.findBookById(id); + } + @QueryMapping public List getAllBooks() { return bookRepository.findAll(); @@ -39,5 +47,46 @@ public List getAllAuthors() { return authorRepository.findAll(); } + @MutationMapping + public Book addBook(@Argument BookInput book) { + Author author = authorRepository.findAuthorById(book.getAuthorId()); + Book newBook = new Book(); + newBook.setName(book.getName()); + newBook.setPageCount(book.getPageCount()); + newBook.setAuthor(author); + return bookRepository.save(newBook); + } + + @MutationMapping + public Book updateBook(@Argument int id, @Argument BookInput book) { + Book existingBook = bookRepository.findBookById(id); + if (existingBook != null) { + Author author = authorRepository.findAuthorById(book.getAuthorId()); + existingBook.setName(book.getName()); + existingBook.setPageCount(book.getPageCount()); + existingBook.setAuthor(author); + return bookRepository.save(existingBook); + } + return null; + } + @MutationMapping + public Boolean deleteBook(@Argument int id) { + bookRepository.deleteById(id); + return true; + } + + @MutationMapping + public Author addAuthor(@Argument AuthorInput author) { + Author newAuthor = new Author(); + newAuthor.setFirstName(author.getFirstName()); + newAuthor.setLastName(author.getLastName()); + return authorRepository.save(newAuthor); + } + + @MutationMapping + public Boolean deleteAuthor(@Argument int id) { + authorRepository.deleteById(id); + return true; + } } diff --git a/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/dto/AuthorInput.java b/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/dto/AuthorInput.java new file mode 100644 index 00000000..c2d5e66e --- /dev/null +++ b/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/dto/AuthorInput.java @@ -0,0 +1,13 @@ +package com.keploy.springbootpostgresgraphql.dto; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class AuthorInput { + private String firstName; + private String lastName; +} diff --git a/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/dto/BookInput.java b/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/dto/BookInput.java new file mode 100644 index 00000000..0d237e53 --- /dev/null +++ b/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/dto/BookInput.java @@ -0,0 +1,14 @@ +package com.keploy.springbootpostgresgraphql.dto; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class BookInput { + private String name; + private int pageCount; + private int authorId; +} diff --git a/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/repository/BookRepository.java b/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/repository/BookRepository.java index ba4eedc0..d9003192 100644 --- a/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/repository/BookRepository.java +++ b/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/repository/BookRepository.java @@ -11,6 +11,8 @@ public interface BookRepository extends JpaRepository { Book findBookByName(String name); + Book findBookById(int id); + List findAll(); } diff --git a/spring-boot-postgres-graphql/src/main/resources/graphql/schema.graphqls b/spring-boot-postgres-graphql/src/main/resources/graphql/schema.graphqls index 5a5c47bc..a9ee1530 100644 --- a/spring-boot-postgres-graphql/src/main/resources/graphql/schema.graphqls +++ b/spring-boot-postgres-graphql/src/main/resources/graphql/schema.graphqls @@ -1,10 +1,30 @@ type Query { getBookByName(name: String): Book + getBookById(id: ID!): Book getAllBooks: [Book] getAuthorById(id: Int): Author getAllAuthors: [Author] } +type Mutation { + addBook(book: BookInput!): Book + updateBook(id: ID!, book: BookInput!): Book + deleteBook(id: ID!): Boolean + addAuthor(author: AuthorInput!): Author + deleteAuthor(id: ID!): Boolean +} + +input BookInput { + name: String! + pageCount: Int! + authorId: Int! +} + +input AuthorInput { + firstName: String! + lastName: String! +} + type Book { id: ID name: String