Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<Book> getAllBooks() {
return bookRepository.findAll();
Expand All @@ -39,5 +47,46 @@ public List<Author> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public interface BookRepository extends JpaRepository<Book, Integer> {

Book findBookByName(String name);

Book findBookById(int id);

List<Book> findAll();

}
Original file line number Diff line number Diff line change
@@ -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
Expand Down