|
3 | 3 | import com.kaluzny.domain.Book;
|
4 | 4 | import com.kaluzny.domain.BookRepository;
|
5 | 5 | import lombok.RequiredArgsConstructor;
|
| 6 | +import lombok.extern.slf4j.Slf4j; |
6 | 7 | import org.springframework.http.HttpStatus;
|
7 | 8 | import org.springframework.http.MediaType;
|
8 | 9 | import org.springframework.web.bind.annotation.*;
|
|
12 | 13 | @RestController
|
13 | 14 | @RequestMapping(value = "/api", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
14 | 15 | @RequiredArgsConstructor
|
| 16 | +@Slf4j |
15 | 17 | public class BookRestController {
|
16 | 18 |
|
17 | 19 | private final BookRepository repository;
|
18 | 20 |
|
19 | 21 | @PostMapping("/books")
|
20 | 22 | @ResponseStatus(HttpStatus.CREATED)
|
21 |
| - public Book addBook(@RequestBody Book book) { |
22 |
| - return repository.save(book); |
| 23 | + public Book saveBook(@RequestBody Book book) { |
| 24 | + log.info("saveBook() - start: book = {}", book); |
| 25 | + Book savedBook = repository.save(book); |
| 26 | + log.info("saveBook() - end: savedBook = {}", savedBook.getId()); |
| 27 | + return savedBook; |
23 | 28 | }
|
24 | 29 |
|
25 | 30 | @GetMapping("/books")
|
26 | 31 | @ResponseStatus(HttpStatus.OK)
|
27 | 32 | public Collection<Book> getAllBooks() {
|
28 |
| - return repository.findAll(); |
| 33 | + log.info("getAllBooks() - start"); |
| 34 | + Collection<Book> collection = repository.findAll(); |
| 35 | + log.info("getAllBooks() - end"); |
| 36 | + return collection; |
29 | 37 | }
|
30 | 38 |
|
31 | 39 | @GetMapping("/books/{id}")
|
32 | 40 | @ResponseStatus(HttpStatus.OK)
|
33 |
| - public Book getBookWithId(@PathVariable Long id) { |
34 |
| - return repository.findOne(id); |
| 41 | + public Book getBookById(@PathVariable Long id) { |
| 42 | + log.info("getBookById() - start: id = {}", id); |
| 43 | + Book receivedBook = repository.findOne(id); |
| 44 | + log.info("getBookById() - end: book = {}", receivedBook.getId()); |
| 45 | + return receivedBook; |
35 | 46 | }
|
36 | 47 |
|
37 | 48 | @GetMapping(value = "/books", params = {"name"})
|
38 | 49 | @ResponseStatus(HttpStatus.OK)
|
39 |
| - public Collection<Book> findBookWithName(@RequestParam(value = "name") String name) { |
40 |
| - return repository.findByName(name); |
| 50 | + public Collection<Book> findBookByName(@RequestParam(value = "name") String name) { |
| 51 | + log.info("findBookByName() - start: name = {}", name); |
| 52 | + Collection<Book> collection = repository.findByName(name); |
| 53 | + log.info("findBookByName() - end: collection = {}", collection); |
| 54 | + return collection; |
41 | 55 | }
|
42 | 56 |
|
43 | 57 | @PutMapping("/books/{id}")
|
44 | 58 | @ResponseStatus(HttpStatus.OK)
|
45 |
| - public Book updateBookFromDB(@PathVariable("id") long id, @RequestBody Book book) { |
46 |
| - return repository.save(putBook(id, book)); |
| 59 | + public Book refreshBook(@PathVariable("id") long id, @RequestBody Book book) { |
| 60 | + log.info("refreshBook() - start: id = {}, book = {}", id, book); |
| 61 | + Book updatedBook = repository.save(putBook(id, book)); |
| 62 | + log.info("refreshBook() - end: updatedBook = {}", updatedBook); |
| 63 | + return updatedBook; |
47 | 64 | }
|
48 | 65 |
|
49 | 66 | @DeleteMapping("/books/{id}")
|
50 | 67 | @ResponseStatus(HttpStatus.NO_CONTENT)
|
51 |
| - public void deleteBookWithId(@PathVariable Long id) { |
| 68 | + public void removeBookById(@PathVariable Long id) { |
| 69 | + log.info("removeBookById() - start: id = {}", id); |
52 | 70 | repository.delete(id);
|
| 71 | + log.info("removeBookById() - end: id = {}", id); |
53 | 72 | }
|
54 | 73 |
|
55 | 74 | @DeleteMapping("/books")
|
56 | 75 | @ResponseStatus(HttpStatus.NO_CONTENT)
|
57 |
| - public void deleteAllBooks() { |
| 76 | + public void removeAllBooks() { |
| 77 | + log.info("removeAllBooks() - start"); |
58 | 78 | repository.deleteAll();
|
| 79 | + log.info("removeAllBooks() - end"); |
59 | 80 | }
|
60 | 81 |
|
61 | 82 | private Book putBook(long id, Book existingBook) {
|
| 83 | + log.info("putBook() - start: id = {} existingBook = {}", id, existingBook); |
62 | 84 | Book putBook = repository.findOne(id);
|
63 | 85 | putBook.setName(existingBook.getName());
|
64 | 86 | putBook.setDescription(existingBook.getDescription());
|
65 | 87 | putBook.setTags(existingBook.getTags());
|
| 88 | + log.info("putBook() - end: putBook = {}", putBook); |
66 | 89 | return putBook;
|
67 | 90 | }
|
68 | 91 | }
|
0 commit comments