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,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;

Expand All @@ -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<Book> getAllBooks() {
return bookRepository.findAll();
Expand All @@ -39,5 +52,74 @@ public List<Author> getAllAuthors() {
return authorRepository.findAll();
}

@QueryMapping
public List<Category> 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);
}
}
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,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;
}
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 CategoryInput {
private String name;
private String description;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@ public class Book {
@JoinColumn(name = "author_id", nullable = false)
private Author author;

@ManyToOne
@JoinColumn(name = "category_id")
private Category category;


}
Original file line number Diff line number Diff line change
@@ -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<Book> books;
}
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
@@ -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, Integer> {
Category findCategoryById(int id);
Category findCategoryByName(String name);
List<Category> findAll();
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,56 @@
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 {
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]
}