From 4f0ae5d2e33cae3a9dcdb7e29001416200f2a913 Mon Sep 17 00:00:00 2001 From: Tim Schimandle Date: Thu, 21 Dec 2017 10:22:27 -0700 Subject: [PATCH] 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 --- .../cloud/connectors/heroku/book/Book.java | 39 +++++++++++++++++++ .../heroku/book/BookController.java | 26 +++++++++++++ .../heroku/book/BookRepository.java | 6 +++ .../connectors/heroku/book/BookService.java | 29 ++++++++++++++ 4 files changed, 100 insertions(+) create mode 100644 spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/Book.java create mode 100644 spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/BookController.java create mode 100644 spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/BookRepository.java create mode 100644 spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/BookService.java diff --git a/spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/Book.java b/spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/Book.java new file mode 100644 index 0000000000..6f45ef6a29 --- /dev/null +++ b/spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/Book.java @@ -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; + } +} diff --git a/spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/BookController.java b/spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/BookController.java new file mode 100644 index 0000000000..eb2972f35a --- /dev/null +++ b/spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/BookController.java @@ -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); + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/BookRepository.java b/spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/BookRepository.java new file mode 100644 index 0000000000..9bdf16148d --- /dev/null +++ b/spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/BookRepository.java @@ -0,0 +1,6 @@ +package com.baeldung.spring.cloud.connectors.heroku.book; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface BookRepository extends JpaRepository{ +} diff --git a/spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/BookService.java b/spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/BookService.java new file mode 100644 index 0000000000..4978ded65f --- /dev/null +++ b/spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/BookService.java @@ -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); + } +}