add update to rest api (#1401)
* upgrade to spring boot 1.5.2 * add full update to REST API
This commit is contained in:
+19
-6
@@ -1,11 +1,19 @@
|
||||
package com.baeldung.spring.cloud.bootstrap.svcbook.book;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PatchMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/books")
|
||||
public class BookController {
|
||||
@@ -13,7 +21,7 @@ public class BookController {
|
||||
@Autowired
|
||||
private BookService bookService;
|
||||
|
||||
@GetMapping("")
|
||||
@GetMapping
|
||||
public List<Book> findAllBooks() {
|
||||
return bookService.findAllBooks();
|
||||
}
|
||||
@@ -23,7 +31,7 @@ public class BookController {
|
||||
return bookService.findBookById(bookId);
|
||||
}
|
||||
|
||||
@PostMapping("")
|
||||
@PostMapping
|
||||
public Book createBook(@RequestBody Book book) {
|
||||
return bookService.createBook(book);
|
||||
}
|
||||
@@ -33,7 +41,12 @@ public class BookController {
|
||||
bookService.deleteBook(bookId);
|
||||
}
|
||||
|
||||
@PatchMapping("/{bookId")
|
||||
@PutMapping("/{bookId}")
|
||||
public Book updateBook(@RequestBody Book book, @PathVariable Long bookId) {
|
||||
return bookService.updateBook(book, bookId);
|
||||
}
|
||||
|
||||
@PatchMapping("/{bookId}")
|
||||
public Book updateBook(@RequestBody Map<String, String> updates, @PathVariable Long bookId) {
|
||||
return bookService.updateBook(updates, bookId);
|
||||
}
|
||||
|
||||
+25
-14
@@ -1,13 +1,15 @@
|
||||
package com.baeldung.spring.cloud.bootstrap.svcbook.book;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
@Service
|
||||
@Transactional(readOnly = true)
|
||||
@@ -27,7 +29,7 @@ public class BookService {
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRED)
|
||||
public Book createBook(Book book) {
|
||||
Book newBook = new Book();
|
||||
final Book newBook = new Book();
|
||||
newBook.setTitle(book.getTitle());
|
||||
newBook.setAuthor(book.getAuthor());
|
||||
return bookRepository.save(newBook);
|
||||
@@ -40,16 +42,25 @@ public class BookService {
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRED)
|
||||
public Book updateBook(Map<String, String> updates, Long bookId) {
|
||||
Book book = findBookById(bookId);
|
||||
updates.keySet().forEach(key -> {
|
||||
switch (key) {
|
||||
case "author":
|
||||
book.setAuthor(updates.get(key));
|
||||
break;
|
||||
case "title":
|
||||
book.setTitle(updates.get(key));
|
||||
}
|
||||
});
|
||||
final Book book = findBookById(bookId);
|
||||
updates.keySet()
|
||||
.forEach(key -> {
|
||||
switch (key) {
|
||||
case "author":
|
||||
book.setAuthor(updates.get(key));
|
||||
break;
|
||||
case "title":
|
||||
book.setTitle(updates.get(key));
|
||||
}
|
||||
});
|
||||
return bookRepository.save(book);
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRED)
|
||||
public Book updateBook(Book book, Long bookId) {
|
||||
Preconditions.checkNotNull(book);
|
||||
Preconditions.checkState(book.getId() == bookId);
|
||||
Preconditions.checkNotNull(bookRepository.findOne(bookId));
|
||||
return bookRepository.save(book);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user