BAEL-1176 SPring Could Heroku (#3212)

* BAEL-1176 setting up base application with in memory database

* BAEL-1176 adding postgres stuff

* BAEL-1176 swapping product entity for book entity to make this in line with other spring boot apps.

* BAEL-1176 change products endpoint to books
This commit is contained in:
Tim Schimandle 2017-12-21 10:22:27 -07:00 committed by Grzegorz Piwowarek
parent d14cc42306
commit 4f0ae5d2e3
4 changed files with 100 additions and 0 deletions

View File

@ -0,0 +1,39 @@
package com.baeldung.spring.cloud.connectors.heroku.book;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String author;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.spring.cloud.connectors.heroku.book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/books")
public class BookController {
private final BookService bookService;
@Autowired
public BookController(BookService bookService) {
this.bookService = bookService;
}
@GetMapping("/{bookId}")
public Book findBook(@PathVariable Long bookId) {
return bookService.findBookById(bookId);
}
@PostMapping
public Book createBook(@RequestBody Book book) {
return bookService.createBook(book);
}
}

View File

@ -0,0 +1,6 @@
package com.baeldung.spring.cloud.connectors.heroku.book;
import org.springframework.data.jpa.repository.JpaRepository;
public interface BookRepository extends JpaRepository<Book, Long>{
}

View File

@ -0,0 +1,29 @@
package com.baeldung.spring.cloud.connectors.heroku.book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(readOnly = true)
public class BookService {
private final BookRepository bookRepository;
@Autowired
public BookService(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}
public Book findBookById(Long bookId) {
return bookRepository.findOne(bookId);
}
@Transactional(propagation = Propagation.REQUIRED)
public Book createBook(Book book) {
Book newBook = new Book();
newBook.setTitle(book.getTitle());
newBook.setAuthor(book.getAuthor());
return bookRepository.save(newBook);
}
}