File tree Expand file tree Collapse file tree
spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .spring .cloud .connectors .heroku .book ;
2+
3+ import javax .persistence .Entity ;
4+ import javax .persistence .GeneratedValue ;
5+ import javax .persistence .GenerationType ;
6+ import javax .persistence .Id ;
7+
8+ @ Entity
9+ public class Book {
10+ @ Id
11+ @ GeneratedValue (strategy = GenerationType .IDENTITY )
12+ private Long id ;
13+ private String title ;
14+ private String author ;
15+
16+ public Long getId () {
17+ return id ;
18+ }
19+
20+ public void setId (Long id ) {
21+ this .id = id ;
22+ }
23+
24+ public String getTitle () {
25+ return title ;
26+ }
27+
28+ public void setTitle (String title ) {
29+ this .title = title ;
30+ }
31+
32+ public String getAuthor () {
33+ return author ;
34+ }
35+
36+ public void setAuthor (String author ) {
37+ this .author = author ;
38+ }
39+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .spring .cloud .connectors .heroku .book ;
2+
3+ import org .springframework .beans .factory .annotation .Autowired ;
4+ import org .springframework .web .bind .annotation .*;
5+
6+ @ RestController
7+ @ RequestMapping ("/books" )
8+ public class BookController {
9+
10+ private final BookService bookService ;
11+
12+ @ Autowired
13+ public BookController (BookService bookService ) {
14+ this .bookService = bookService ;
15+ }
16+
17+ @ GetMapping ("/{bookId}" )
18+ public Book findBook (@ PathVariable Long bookId ) {
19+ return bookService .findBookById (bookId );
20+ }
21+
22+ @ PostMapping
23+ public Book createBook (@ RequestBody Book book ) {
24+ return bookService .createBook (book );
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .spring .cloud .connectors .heroku .book ;
2+
3+ import org .springframework .data .jpa .repository .JpaRepository ;
4+
5+ public interface BookRepository extends JpaRepository <Book , Long >{
6+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .spring .cloud .connectors .heroku .book ;
2+
3+ import org .springframework .beans .factory .annotation .Autowired ;
4+ import org .springframework .stereotype .Service ;
5+ import org .springframework .transaction .annotation .Propagation ;
6+ import org .springframework .transaction .annotation .Transactional ;
7+
8+ @ Service
9+ @ Transactional (readOnly = true )
10+ public class BookService {
11+ private final BookRepository bookRepository ;
12+
13+ @ Autowired
14+ public BookService (BookRepository bookRepository ) {
15+ this .bookRepository = bookRepository ;
16+ }
17+
18+ public Book findBookById (Long bookId ) {
19+ return bookRepository .findOne (bookId );
20+ }
21+
22+ @ Transactional (propagation = Propagation .REQUIRED )
23+ public Book createBook (Book book ) {
24+ Book newBook = new Book ();
25+ newBook .setTitle (book .getTitle ());
26+ newBook .setAuthor (book .getAuthor ());
27+ return bookRepository .save (newBook );
28+ }
29+ }
You can’t perform that action at this time.
0 commit comments