22
33import com .keploy .springbootpostgresgraphql .dto .AuthorInput ;
44import com .keploy .springbootpostgresgraphql .dto .BookInput ;
5+ import com .keploy .springbootpostgresgraphql .dto .CategoryInput ;
56import com .keploy .springbootpostgresgraphql .entity .Author ;
67import com .keploy .springbootpostgresgraphql .entity .Book ;
8+ import com .keploy .springbootpostgresgraphql .entity .Category ;
79import com .keploy .springbootpostgresgraphql .repository .AuthorRepository ;
810import com .keploy .springbootpostgresgraphql .repository .BookRepository ;
11+ import com .keploy .springbootpostgresgraphql .repository .CategoryRepository ;
912import org .springframework .beans .factory .annotation .Autowired ;
1013import org .springframework .graphql .data .method .annotation .Argument ;
1114import org .springframework .graphql .data .method .annotation .MutationMapping ;
@@ -21,6 +24,8 @@ public class BookController {
2124 BookRepository bookRepository ;
2225 @ Autowired
2326 AuthorRepository authorRepository ;
27+ @ Autowired
28+ CategoryRepository categoryRepository ;
2429
2530 @ QueryMapping
2631 public Book getBookByName (@ Argument String name ) {
@@ -47,13 +52,27 @@ public List<Author> getAllAuthors() {
4752 return authorRepository .findAll ();
4853 }
4954
55+ @ QueryMapping
56+ public List <Category > getAllCategories () {
57+ return categoryRepository .findAll ();
58+ }
59+
60+ @ QueryMapping
61+ public Category getCategoryById (@ Argument int id ) {
62+ return categoryRepository .findCategoryById (id );
63+ }
64+
5065 @ MutationMapping
5166 public Book addBook (@ Argument BookInput book ) {
5267 Author author = authorRepository .findAuthorById (book .getAuthorId ());
5368 Book newBook = new Book ();
5469 newBook .setName (book .getName ());
5570 newBook .setPageCount (book .getPageCount ());
5671 newBook .setAuthor (author );
72+ if (book .getCategoryId () != null ) {
73+ Category category = categoryRepository .findCategoryById (book .getCategoryId ());
74+ newBook .setCategory (category );
75+ }
5776 return bookRepository .save (newBook );
5877 }
5978
@@ -65,6 +84,12 @@ public Book updateBook(@Argument int id, @Argument BookInput book) {
6584 existingBook .setName (book .getName ());
6685 existingBook .setPageCount (book .getPageCount ());
6786 existingBook .setAuthor (author );
87+ if (book .getCategoryId () != null ) {
88+ Category category = categoryRepository .findCategoryById (book .getCategoryId ());
89+ existingBook .setCategory (category );
90+ } else {
91+ existingBook .setCategory (null );
92+ }
6893 return bookRepository .save (existingBook );
6994 }
7095 return null ;
@@ -89,4 +114,12 @@ public Boolean deleteAuthor(@Argument int id) {
89114 authorRepository .deleteById (id );
90115 return true ;
91116 }
117+
118+ @ MutationMapping
119+ public Category addCategory (@ Argument CategoryInput category ) {
120+ Category newCategory = new Category ();
121+ newCategory .setName (category .getName ());
122+ newCategory .setDescription (category .getDescription ());
123+ return categoryRepository .save (newCategory );
124+ }
92125}
0 commit comments