Skip to content

Commit f0773a6

Browse files
Add files via upload
1 parent 819f1cc commit f0773a6

File tree

8 files changed

+192
-0
lines changed

8 files changed

+192
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.graphql.learn;
2+
3+
import com.graphql.learn.entities.Book;
4+
import com.graphql.learn.services.BookService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.CommandLineRunner;
7+
import org.springframework.boot.SpringApplication;
8+
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
10+
@SpringBootApplication
11+
public class GraphqlProjectApplication implements CommandLineRunner {
12+
13+
@Autowired
14+
private BookService bookService;
15+
public static void main(String[] args) {
16+
SpringApplication.run(GraphqlProjectApplication.class, args);
17+
}
18+
19+
20+
@Override
21+
public void run(String... args) throws Exception {
22+
Book b1 = new Book();
23+
b1.setTitle("Complete Reference");
24+
b1.setDesc("For learning Spring boot");
25+
b1.setPages(500);
26+
b1.setPrice(5000);
27+
b1.setAuthor("Kevin Smith");
28+
29+
Book b2 = new Book();
30+
b2.setTitle("Learn GraphL");
31+
b2.setDesc("For learning GraphQL");
32+
b2.setPages(250);
33+
b2.setPrice(4000);
34+
b2.setAuthor("Alex Smith");
35+
36+
Book b3 = new Book();
37+
b3.setTitle("Learn H2 Database");
38+
b3.setDesc("For learning H2 Database");
39+
b3.setPages(250);
40+
b3.setPrice(4000);
41+
b3.setAuthor("Steve Jones");
42+
43+
this.bookService.create(b1);
44+
this.bookService.create(b2);
45+
this.bookService.create(b3);
46+
47+
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.graphql.learn.controllers;
2+
3+
import com.graphql.learn.entities.Book;
4+
import com.graphql.learn.services.BookService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.*;
7+
8+
import java.util.List;
9+
10+
@RestController
11+
@RequestMapping("/books")
12+
public class BookController {
13+
14+
@Autowired
15+
private BookService bookService;
16+
17+
//create
18+
@PostMapping
19+
public Book create(@RequestBody Book book){
20+
return this.bookService.create(book);
21+
}
22+
23+
// get all
24+
@GetMapping
25+
public List<Book> getAll(){
26+
return this.bookService.getAll();
27+
}
28+
29+
//get single book
30+
@GetMapping("/{bookId}")
31+
public Book getBook(@PathVariable int bookId){
32+
return this.bookService.get(bookId);
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.graphql.learn.entities;
2+
import jakarta.persistence.*;
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.NoArgsConstructor;
6+
import lombok.Setter;
7+
8+
9+
10+
@Entity
11+
@Table(name= "project_books")
12+
@Getter
13+
@Setter
14+
@AllArgsConstructor
15+
@NoArgsConstructor
16+
public class Book {
17+
18+
// @Id
19+
// @GeneratedValue(strategy = GenerationType.IDENTITY)
20+
@Id
21+
@GeneratedValue(strategy = GenerationType.IDENTITY)
22+
private int id;
23+
private String title;
24+
private String desc;
25+
private String author;
26+
private double price;
27+
private int pages;
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.graphql.learn.repositories;
2+
3+
import com.graphql.learn.entities.Book;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface BookRep extends JpaRepository <Book, Integer> {
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.graphql.learn.services;
2+
3+
import com.graphql.learn.entities.Book;
4+
5+
import java.util.List;
6+
7+
public interface BookService {
8+
//Create
9+
Book create(Book book);
10+
11+
//get all
12+
List<Book> getAll();
13+
14+
//get single book
15+
Book get(int bookId);
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.graphql.learn.services.impl;
2+
3+
import com.graphql.learn.entities.Book;
4+
import com.graphql.learn.repositories.BookRep;
5+
import com.graphql.learn.services.BookService;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Service;
8+
9+
import java.util.List;
10+
11+
@Service
12+
public class BookServiceImpl implements BookService {
13+
14+
private BookRep bookRep;
15+
16+
@Autowired
17+
public BookServiceImpl(BookRep bookRep){
18+
this.bookRep = bookRep;
19+
}
20+
@Override
21+
public Book create(Book book) {
22+
return this.bookRep.save(book);
23+
}
24+
25+
@Override
26+
public List<Book> getAll() {
27+
return this.bookRep.findAll();
28+
}
29+
30+
@Override
31+
public Book get(int bookId) {
32+
return this.bookRep.findById(bookId).orElseThrow(() ->new RuntimeException("Book you are looking for not found on server !!"));
33+
}
34+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
server.port = 8009
2+
spring.h2.console.enabled=true
3+
spring.datasource.url= jdbc:h2:mem:testdb
4+
spring.datasource.driver-class-name=org.h2.Driver
5+
spring.datasource.username=sa
6+
spring.datasource.password=admin
7+
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
8+
spring.jpa.defer-datasource-initialization=true
9+
spring.jpa.hibernate.ddl-auto=update
10+
spring.jpa.show-sql=true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.graphql.learn;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.boot.test.context.SpringBootTest;
5+
6+
@SpringBootTest
7+
class GraphqlProjectApplicationTests {
8+
9+
@Test
10+
void contextLoads() {
11+
}
12+
13+
}

0 commit comments

Comments
 (0)