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..2f93c8c7 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,17 @@ package com.keploy.springbootpostgresgraphql.controller; +import com.keploy.springbootpostgresgraphql.dto.AuthorInput; +import com.keploy.springbootpostgresgraphql.dto.BookInput; +import com.keploy.springbootpostgresgraphql.dto.CategoryInput; import com.keploy.springbootpostgresgraphql.entity.Author; import com.keploy.springbootpostgresgraphql.entity.Book; +import com.keploy.springbootpostgresgraphql.entity.Category; import com.keploy.springbootpostgresgraphql.repository.AuthorRepository; import com.keploy.springbootpostgresgraphql.repository.BookRepository; +import com.keploy.springbootpostgresgraphql.repository.CategoryRepository; 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; @@ -18,12 +24,19 @@ public class BookController { BookRepository bookRepository; @Autowired AuthorRepository authorRepository; + @Autowired + CategoryRepository categoryRepository; @QueryMapping 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 +52,74 @@ public List getAllAuthors() { return authorRepository.findAll(); } + @QueryMapping + public List getAllCategories() { + return categoryRepository.findAll(); + } + + @QueryMapping + public Category getCategoryById(@Argument int id) { + return categoryRepository.findCategoryById(id); + } + + @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); + if (book.getCategoryId() != null) { + Category category = categoryRepository.findCategoryById(book.getCategoryId()); + newBook.setCategory(category); + } + 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); + if (book.getCategoryId() != null) { + Category category = categoryRepository.findCategoryById(book.getCategoryId()); + existingBook.setCategory(category); + } else { + existingBook.setCategory(null); + } + 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; + } + + @MutationMapping + public Category addCategory(@Argument CategoryInput category) { + Category newCategory = new Category(); + newCategory.setName(category.getName()); + newCategory.setDescription(category.getDescription()); + return categoryRepository.save(newCategory); + } } 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..e0aba45b --- /dev/null +++ b/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/dto/BookInput.java @@ -0,0 +1,15 @@ +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; + private Integer categoryId; +} diff --git a/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/dto/CategoryInput.java b/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/dto/CategoryInput.java new file mode 100644 index 00000000..470c24ca --- /dev/null +++ b/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/dto/CategoryInput.java @@ -0,0 +1,13 @@ +package com.keploy.springbootpostgresgraphql.dto; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class CategoryInput { + private String name; + private String description; +} diff --git a/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/entity/Book.java b/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/entity/Book.java index a84ce7b2..964068fe 100644 --- a/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/entity/Book.java +++ b/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/entity/Book.java @@ -25,5 +25,9 @@ public class Book { @JoinColumn(name = "author_id", nullable = false) private Author author; + @ManyToOne + @JoinColumn(name = "category_id") + private Category category; + } \ No newline at end of file diff --git a/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/entity/Category.java b/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/entity/Category.java new file mode 100644 index 00000000..35fb041f --- /dev/null +++ b/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/entity/Category.java @@ -0,0 +1,28 @@ +package com.keploy.springbootpostgresgraphql.entity; + +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Entity +@Table(name = "category") +public class Category { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private int id; + + @Column(nullable = false, unique = true) + private String name; + + private String description; + + @OneToMany(mappedBy = "category", cascade = CascadeType.ALL) + private List books; +} 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/java/com/keploy/springbootpostgresgraphql/repository/CategoryRepository.java b/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/repository/CategoryRepository.java new file mode 100644 index 00000000..09aef201 --- /dev/null +++ b/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/repository/CategoryRepository.java @@ -0,0 +1,14 @@ +package com.keploy.springbootpostgresgraphql.repository; + +import com.keploy.springbootpostgresgraphql.entity.Category; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface CategoryRepository extends JpaRepository { + Category findCategoryById(int id); + Category findCategoryByName(String name); + 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..cf94ce05 100644 --- a/spring-boot-postgres-graphql/src/main/resources/graphql/schema.graphqls +++ b/spring-boot-postgres-graphql/src/main/resources/graphql/schema.graphqls @@ -1,8 +1,37 @@ type Query { getBookByName(name: String): Book + getBookById(id: ID!): Book getAllBooks: [Book] getAuthorById(id: Int): Author getAllAuthors: [Author] + getAllCategories: [Category] + getCategoryById(id: ID!): Category +} + +type Mutation { + addBook(book: BookInput!): Book + updateBook(id: ID!, book: BookInput!): Book + deleteBook(id: ID!): Boolean + addAuthor(author: AuthorInput!): Author + deleteAuthor(id: ID!): Boolean + addCategory(category: CategoryInput!): Category +} + +input BookInput { + name: String! + pageCount: Int! + authorId: Int! + categoryId: Int +} + +input AuthorInput { + firstName: String! + lastName: String! +} + +input CategoryInput { + name: String! + description: String } type Book { @@ -10,10 +39,18 @@ type Book { name: String pageCount: Int author: Author + category: Category } type Author { id: ID firstName: String lastName: String +} + +type Category { + id: ID + name: String + description: String + books: [Book] } \ No newline at end of file