|
6 | 6 | import org.baeldung.web.exception.BookNotFoundException; |
7 | 7 | import org.springframework.beans.factory.annotation.Autowired; |
8 | 8 | import org.springframework.http.HttpStatus; |
| 9 | +import org.springframework.web.bind.annotation.CrossOrigin; |
9 | 10 | import org.springframework.web.bind.annotation.DeleteMapping; |
10 | 11 | import org.springframework.web.bind.annotation.GetMapping; |
11 | 12 | import org.springframework.web.bind.annotation.PathVariable; |
@@ -37,29 +38,30 @@ public List<Book> findByTitle(@PathVariable String bookTitle) { |
37 | 38 |
|
38 | 39 | @GetMapping("/{id}") |
39 | 40 | public Book findOne(@PathVariable long id) { |
40 | | - return bookRepository.findOne(id) |
| 41 | + return bookRepository.findById(id) |
41 | 42 | .orElseThrow(BookNotFoundException::new); |
42 | 43 | } |
43 | 44 |
|
44 | 45 | @PostMapping |
45 | 46 | @ResponseStatus(HttpStatus.CREATED) |
46 | 47 | public Book create(@RequestBody Book book) { |
47 | | - return bookRepository.save(book); |
| 48 | + Book book1 = bookRepository.save(book); |
| 49 | + return book1; |
48 | 50 | } |
49 | 51 |
|
50 | 52 | @DeleteMapping("/{id}") |
51 | 53 | public void delete(@PathVariable long id) { |
52 | | - bookRepository.findOne(id) |
| 54 | + bookRepository.findById(id) |
53 | 55 | .orElseThrow(BookNotFoundException::new); |
54 | | - bookRepository.delete(id); |
| 56 | + bookRepository.deleteById(id); |
55 | 57 | } |
56 | 58 |
|
57 | 59 | @PutMapping("/{id}") |
58 | 60 | public Book updateBook(@RequestBody Book book, @PathVariable long id) { |
59 | 61 | if (book.getId() != id) { |
60 | 62 | throw new BookIdMismatchException(); |
61 | 63 | } |
62 | | - bookRepository.findOne(id) |
| 64 | + bookRepository.findById(id) |
63 | 65 | .orElseThrow(BookNotFoundException::new); |
64 | 66 | return bookRepository.save(book); |
65 | 67 | } |
|
0 commit comments