|
2 | 2 |
|
3 | 3 | import com.kaluzny.domain.Book;
|
4 | 4 | import com.kaluzny.domain.BookRepository;
|
| 5 | +import lombok.RequiredArgsConstructor; |
5 | 6 | import org.springframework.http.HttpStatus;
|
6 |
| -import org.springframework.http.ResponseEntity; |
| 7 | +import org.springframework.http.MediaType; |
7 | 8 | import org.springframework.web.bind.annotation.*;
|
8 | 9 |
|
9 |
| -import javax.inject.Inject; |
10 | 10 | import java.util.Collection;
|
11 | 11 |
|
12 | 12 | @RestController
|
13 |
| -@RequestMapping("/api/books") |
| 13 | +@RequestMapping(value = "/api", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) |
| 14 | +@RequiredArgsConstructor |
14 | 15 | public class BookRestController {
|
15 | 16 |
|
16 |
| - private BookRepository repository; |
| 17 | + private final BookRepository repository; |
17 | 18 |
|
18 |
| - @Inject |
19 |
| - public void setRepository(BookRepository repository) { |
20 |
| - this.repository = repository; |
| 19 | + @PostMapping("/books") |
| 20 | + @ResponseStatus(HttpStatus.CREATED) |
| 21 | + public Book addBook(@RequestBody Book book) { |
| 22 | + return repository.save(book); |
21 | 23 | }
|
22 | 24 |
|
23 |
| - @RequestMapping( |
24 |
| - method = RequestMethod.POST) |
25 |
| - public ResponseEntity<?> addBook(@RequestBody Book book) { |
26 |
| - return new ResponseEntity<>(repository.save(book), HttpStatus.CREATED); |
| 25 | + @GetMapping("/books") |
| 26 | + @ResponseStatus(HttpStatus.OK) |
| 27 | + public Collection<Book> getAllBooks() { |
| 28 | + return repository.findAll(); |
27 | 29 | }
|
28 | 30 |
|
29 |
| - @RequestMapping( |
30 |
| - method = RequestMethod.GET) |
31 |
| - public ResponseEntity<Collection<Book>> getAllBooks() { |
32 |
| - return new ResponseEntity<>(repository.findAll(), HttpStatus.OK); |
| 31 | + @GetMapping("/books/{id}") |
| 32 | + @ResponseStatus(HttpStatus.OK) |
| 33 | + public Book getBookWithId(@PathVariable Long id) { |
| 34 | + return repository.findOne(id); |
33 | 35 | }
|
34 | 36 |
|
35 |
| - @RequestMapping( |
36 |
| - value = "/{id}", |
37 |
| - method = RequestMethod.GET) |
38 |
| - public ResponseEntity<Book> getBookWithId(@PathVariable Long id) { |
39 |
| - return new ResponseEntity<>(repository.findOne(id), HttpStatus.OK); |
| 37 | + @GetMapping(value = "/books", params = {"name"}) |
| 38 | + @ResponseStatus(HttpStatus.OK) |
| 39 | + public Collection<Book> findBookWithName(@RequestParam(value = "name") String name) { |
| 40 | + return repository.findByName(name); |
40 | 41 | }
|
41 | 42 |
|
42 |
| - @RequestMapping( |
43 |
| - params = {"name"}, |
44 |
| - method = RequestMethod.GET) |
45 |
| - public ResponseEntity<Collection<Book>> findBookWithName(@RequestParam(value = "name") String name) { |
46 |
| - return new ResponseEntity<>(repository.findByName(name), HttpStatus.OK); |
| 43 | + @PutMapping("/books/{id}") |
| 44 | + @ResponseStatus(HttpStatus.OK) |
| 45 | + public Book updateBookFromDB(@PathVariable("id") long id, @RequestBody Book book) { |
| 46 | + return repository.save(putBook(id, book)); |
47 | 47 | }
|
48 | 48 |
|
49 |
| - @RequestMapping( |
50 |
| - value = "/{id}", |
51 |
| - method = RequestMethod.PUT) |
52 |
| - public ResponseEntity<Book> updateBookFromDB(@PathVariable("id") long id, @RequestBody Book book) { |
53 |
| - |
54 |
| - Book currentBook = repository.findOne(id); |
55 |
| - currentBook.setName(book.getName()); |
56 |
| - currentBook.setDescription(book.getDescription()); |
57 |
| - currentBook.setTags(book.getTags()); |
58 |
| - |
59 |
| - return new ResponseEntity<>(repository.save(currentBook), HttpStatus.OK); |
60 |
| - } |
61 |
| - |
62 |
| - @RequestMapping( |
63 |
| - value = "/{id}", |
64 |
| - method = RequestMethod.DELETE) |
| 49 | + @DeleteMapping("/books/{id}") |
| 50 | + @ResponseStatus(HttpStatus.NO_CONTENT) |
65 | 51 | public void deleteBookWithId(@PathVariable Long id) {
|
66 | 52 | repository.delete(id);
|
67 | 53 | }
|
68 | 54 |
|
69 |
| - @RequestMapping( |
70 |
| - method = RequestMethod.DELETE) |
| 55 | + @DeleteMapping("/books") |
| 56 | + @ResponseStatus(HttpStatus.NO_CONTENT) |
71 | 57 | public void deleteAllBooks() {
|
72 | 58 | repository.deleteAll();
|
73 | 59 | }
|
| 60 | + |
| 61 | + private Book putBook(long id, Book existingBook) { |
| 62 | + Book putBook = repository.findOne(id); |
| 63 | + putBook.setName(existingBook.getName()); |
| 64 | + putBook.setDescription(existingBook.getDescription()); |
| 65 | + putBook.setTags(existingBook.getTags()); |
| 66 | + return putBook; |
| 67 | + } |
74 | 68 | }
|
0 commit comments