Skip to content

Commit fb38f6c

Browse files
X
feat:Added CRUD Operations for book and author Signed-off-by: Hitarth Hindocha <hindochahitarth@gmail.com>
1 parent 85cec14 commit fb38f6c

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.keploy.springbootpostgresgraphql.controller;
22

3+
import com.keploy.springbootpostgresgraphql.dto.AuthorInput;
4+
import com.keploy.springbootpostgresgraphql.dto.BookInput;
35
import com.keploy.springbootpostgresgraphql.entity.Author;
46
import com.keploy.springbootpostgresgraphql.entity.Book;
57
import com.keploy.springbootpostgresgraphql.repository.AuthorRepository;
68
import com.keploy.springbootpostgresgraphql.repository.BookRepository;
79
import org.springframework.beans.factory.annotation.Autowired;
810
import org.springframework.graphql.data.method.annotation.Argument;
11+
import org.springframework.graphql.data.method.annotation.MutationMapping;
912
import org.springframework.graphql.data.method.annotation.QueryMapping;
1013
import org.springframework.stereotype.Controller;
1114

@@ -24,6 +27,11 @@ public Book getBookByName(@Argument String name) {
2427
return bookRepository.findBookByName(name);
2528
}
2629

30+
@QueryMapping
31+
public Book getBookById(@Argument int id) {
32+
return bookRepository.findBookById(id);
33+
}
34+
2735
@QueryMapping
2836
public List<Book> getAllBooks() {
2937
return bookRepository.findAll();
@@ -39,5 +47,46 @@ public List<Author> getAllAuthors() {
3947
return authorRepository.findAll();
4048
}
4149

50+
@MutationMapping
51+
public Book addBook(@Argument BookInput book) {
52+
Author author = authorRepository.findAuthorById(book.getAuthorId());
53+
Book newBook = new Book();
54+
newBook.setName(book.getName());
55+
newBook.setPageCount(book.getPageCount());
56+
newBook.setAuthor(author);
57+
return bookRepository.save(newBook);
58+
}
59+
60+
@MutationMapping
61+
public Book updateBook(@Argument int id, @Argument BookInput book) {
62+
Book existingBook = bookRepository.findBookById(id);
63+
if (existingBook != null) {
64+
Author author = authorRepository.findAuthorById(book.getAuthorId());
65+
existingBook.setName(book.getName());
66+
existingBook.setPageCount(book.getPageCount());
67+
existingBook.setAuthor(author);
68+
return bookRepository.save(existingBook);
69+
}
70+
return null;
71+
}
4272

73+
@MutationMapping
74+
public Boolean deleteBook(@Argument int id) {
75+
bookRepository.deleteById(id);
76+
return true;
77+
}
78+
79+
@MutationMapping
80+
public Author addAuthor(@Argument AuthorInput author) {
81+
Author newAuthor = new Author();
82+
newAuthor.setFirstName(author.getFirstName());
83+
newAuthor.setLastName(author.getLastName());
84+
return authorRepository.save(newAuthor);
85+
}
86+
87+
@MutationMapping
88+
public Boolean deleteAuthor(@Argument int id) {
89+
authorRepository.deleteById(id);
90+
return true;
91+
}
4392
}
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 AuthorInput {
11+
private String firstName;
12+
private String lastName;
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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 BookInput {
11+
private String name;
12+
private int pageCount;
13+
private int authorId;
14+
}

spring-boot-postgres-graphql/src/main/java/com/keploy/springbootpostgresgraphql/repository/BookRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public interface BookRepository extends JpaRepository<Book, Integer> {
1111

1212
Book findBookByName(String name);
1313

14+
Book findBookById(int id);
15+
1416
List<Book> findAll();
1517

1618
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
11
type Query {
22
getBookByName(name: String): Book
3+
getBookById(id: ID!): Book
34
getAllBooks: [Book]
45
getAuthorById(id: Int): Author
56
getAllAuthors: [Author]
67
}
78

9+
type Mutation {
10+
addBook(book: BookInput!): Book
11+
updateBook(id: ID!, book: BookInput!): Book
12+
deleteBook(id: ID!): Boolean
13+
addAuthor(author: AuthorInput!): Author
14+
deleteAuthor(id: ID!): Boolean
15+
}
16+
17+
input BookInput {
18+
name: String!
19+
pageCount: Int!
20+
authorId: Int!
21+
}
22+
23+
input AuthorInput {
24+
firstName: String!
25+
lastName: String!
26+
}
27+
828
type Book {
929
id: ID
1030
name: String

0 commit comments

Comments
 (0)