Skip to content

Commit 4f0ae5d

Browse files
tschimanpivovarit
authored andcommitted
BAEL-1176 SPring Could Heroku (eugenp#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
1 parent d14cc42 commit 4f0ae5d

4 files changed

Lines changed: 100 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
}

0 commit comments

Comments
 (0)