Skip to content

Commit e266fff

Browse files
feat:Added Book Category
Signed-off-by: Hitarth Hindocha <hindochahitarth@gmail.com>
1 parent fb38f6c commit e266fff

File tree

7 files changed

+110
-0
lines changed

7 files changed

+110
-0
lines changed

spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/controller/BookController.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
import com.keploy.springbootpostgresgraphql.dto.AuthorInput;
44
import com.keploy.springbootpostgresgraphql.dto.BookInput;
5+
import com.keploy.springbootpostgresgraphql.dto.CategoryInput;
56
import com.keploy.springbootpostgresgraphql.entity.Author;
67
import com.keploy.springbootpostgresgraphql.entity.Book;
8+
import com.keploy.springbootpostgresgraphql.entity.Category;
79
import com.keploy.springbootpostgresgraphql.repository.AuthorRepository;
810
import com.keploy.springbootpostgresgraphql.repository.BookRepository;
11+
import com.keploy.springbootpostgresgraphql.repository.CategoryRepository;
912
import org.springframework.beans.factory.annotation.Autowired;
1013
import org.springframework.graphql.data.method.annotation.Argument;
1114
import 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
}

spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/dto/BookInput.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ public class BookInput {
1111
private String name;
1212
private int pageCount;
1313
private int authorId;
14+
private Integer categoryId;
1415
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.keploy.springbootpostgresgraphql.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
@Data
8+
@AllArgsConstructor
9+
@NoArgsConstructor
10+
public class CategoryInput {
11+
private String name;
12+
private String description;
13+
}

spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/entity/Book.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,9 @@ public class Book {
2525
@JoinColumn(name = "author_id", nullable = false)
2626
private Author author;
2727

28+
@ManyToOne
29+
@JoinColumn(name = "category_id")
30+
private Category category;
31+
2832

2933
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.keploy.springbootpostgresgraphql.entity;
2+
3+
import jakarta.persistence.*;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
import java.util.List;
9+
10+
@Data
11+
@AllArgsConstructor
12+
@NoArgsConstructor
13+
@Entity
14+
@Table(name = "category")
15+
public class Category {
16+
17+
@Id
18+
@GeneratedValue(strategy = GenerationType.IDENTITY)
19+
private int id;
20+
21+
@Column(nullable = false, unique = true)
22+
private String name;
23+
24+
private String description;
25+
26+
@OneToMany(mappedBy = "category", cascade = CascadeType.ALL)
27+
private List<Book> books;
28+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.keploy.springbootpostgresgraphql.repository;
2+
3+
import com.keploy.springbootpostgresgraphql.entity.Category;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
import java.util.List;
8+
9+
@Repository
10+
public interface CategoryRepository extends JpaRepository<Category, Integer> {
11+
Category findCategoryById(int id);
12+
Category findCategoryByName(String name);
13+
List<Category> findAll();
14+
}

spring-boot-postgres-graphql/src/main/resources/graphql/schema.graphqls

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ type Query {
44
getAllBooks: [Book]
55
getAuthorById(id: Int): Author
66
getAllAuthors: [Author]
7+
getAllCategories: [Category]
8+
getCategoryById(id: ID!): Category
79
}
810

911
type Mutation {
@@ -12,28 +14,43 @@ type Mutation {
1214
deleteBook(id: ID!): Boolean
1315
addAuthor(author: AuthorInput!): Author
1416
deleteAuthor(id: ID!): Boolean
17+
addCategory(category: CategoryInput!): Category
1518
}
1619

1720
input BookInput {
1821
name: String!
1922
pageCount: Int!
2023
authorId: Int!
24+
categoryId: Int
2125
}
2226

2327
input AuthorInput {
2428
firstName: String!
2529
lastName: String!
2630
}
2731

32+
input CategoryInput {
33+
name: String!
34+
description: String
35+
}
36+
2837
type Book {
2938
id: ID
3039
name: String
3140
pageCount: Int
3241
author: Author
42+
category: Category
3343
}
3444

3545
type Author {
3646
id: ID
3747
firstName: String
3848
lastName: String
49+
}
50+
51+
type Category {
52+
id: ID
53+
name: String
54+
description: String
55+
books: [Book]
3956
}

0 commit comments

Comments
 (0)