From fb38f6c6debf95f6421f19af3e80c58f32b6d081 Mon Sep 17 00:00:00 2001 From: Hitarth Hindocha Date: Tue, 24 Feb 2026 09:16:35 +0530 Subject: [PATCH 1/2] X feat:Added CRUD Operations for book and author Signed-off-by: Hitarth Hindocha --- .../controller/BookController.java | 49 +++++++++++++++++++ .../dto/AuthorInput.java | 13 +++++ .../dto/BookInput.java | 14 ++++++ .../repository/BookRepository.java | 2 + .../main/resources/graphql/schema.graphqls | 20 ++++++++ 5 files changed, 98 insertions(+) create mode 100644 spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/dto/AuthorInput.java create mode 100644 spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/dto/BookInput.java 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 From e266fff52eba431986dd5165e33a96021eef3764 Mon Sep 17 00:00:00 2001 From: Hitarth Hindocha Date: Fri, 27 Feb 2026 20:13:21 +0530 Subject: [PATCH 2/2] feat:Added Book Category Signed-off-by: Hitarth Hindocha --- .../controller/BookController.java | 33 +++++++++++++++++++ .../dto/BookInput.java | 1 + .../dto/CategoryInput.java | 13 ++++++++ .../entity/Book.java | 4 +++ .../entity/Category.java | 28 ++++++++++++++++ .../repository/CategoryRepository.java | 14 ++++++++ .../main/resources/graphql/schema.graphqls | 17 ++++++++++ 7 files changed, 110 insertions(+) create mode 100644 spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/dto/CategoryInput.java create mode 100644 spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/entity/Category.java create mode 100644 spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/repository/CategoryRepository.java 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 1f3e28f7..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 @@ -2,10 +2,13 @@ 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; @@ -21,6 +24,8 @@ public class BookController { BookRepository bookRepository; @Autowired AuthorRepository authorRepository; + @Autowired + CategoryRepository categoryRepository; @QueryMapping public Book getBookByName(@Argument String name) { @@ -47,6 +52,16 @@ 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()); @@ -54,6 +69,10 @@ public Book addBook(@Argument BookInput 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); } @@ -65,6 +84,12 @@ public Book updateBook(@Argument int id, @Argument BookInput book) { 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; @@ -89,4 +114,12 @@ 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/BookInput.java b/spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/dto/BookInput.java index 0d237e53..e0aba45b 100644 --- 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 @@ -11,4 +11,5 @@ 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/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 a9ee1530..cf94ce05 100644 --- a/spring-boot-postgres-graphql/src/main/resources/graphql/schema.graphqls +++ b/spring-boot-postgres-graphql/src/main/resources/graphql/schema.graphqls @@ -4,6 +4,8 @@ type Query { getAllBooks: [Book] getAuthorById(id: Int): Author getAllAuthors: [Author] + getAllCategories: [Category] + getCategoryById(id: ID!): Category } type Mutation { @@ -12,12 +14,14 @@ type Mutation { 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 { @@ -25,15 +29,28 @@ input AuthorInput { lastName: String! } +input CategoryInput { + name: String! + description: String +} + type Book { id: ID 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