add update to rest api (#1401)

* upgrade to spring boot 1.5.2

* add full update to REST API
This commit is contained in:
Doha2012 2017-03-15 10:59:55 +02:00 committed by Eugen
parent 52ef5b3803
commit 61660f5d37
4 changed files with 83 additions and 35 deletions

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -1,11 +1,20 @@
package com.baeldung.spring.cloud.bootstrap.svcrating.rating;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/ratings")
public class RatingController {
@ -13,7 +22,7 @@ public class RatingController {
@Autowired
private RatingService ratingService;
@GetMapping("")
@GetMapping
public List<Rating> findRatingsByBookId(@RequestParam(required = false, defaultValue = "0") Long bookId) {
if (bookId.equals(0L)) {
return ratingService.findAllRatings();
@ -21,7 +30,7 @@ public class RatingController {
return ratingService.findRatingsByBookId(bookId);
}
@PostMapping("")
@PostMapping
public Rating createRating(@RequestBody Rating rating) {
return ratingService.createRating(rating);
}
@ -31,7 +40,12 @@ public class RatingController {
ratingService.deleteRating(ratingId);
}
@PatchMapping("/{ratingId")
@PutMapping("/{ratingId}")
public Rating updateRating(@RequestBody Rating rating, @PathVariable Long ratingId) {
return ratingService.updateRating(rating, ratingId);
}
@PatchMapping("/{ratingId}")
public Rating updateRating(@RequestBody Map<String, String> updates, @PathVariable Long ratingId) {
return ratingService.updateRating(updates, ratingId);
}

View File

@ -1,13 +1,15 @@
package com.baeldung.spring.cloud.bootstrap.svcrating.rating;
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)
@ -31,7 +33,7 @@ public class RatingService {
@Transactional(propagation = Propagation.REQUIRED)
public Rating createRating(Rating rating) {
Rating newRating = new Rating();
final Rating newRating = new Rating();
newRating.setBookId(rating.getBookId());
newRating.setStars(rating.getStars());
return ratingRepository.save(newRating);
@ -44,14 +46,22 @@ public class RatingService {
@Transactional(propagation = Propagation.REQUIRED)
public Rating updateRating(Map<String, String> updates, Long ratingId) {
Rating rating = findRatingById(ratingId);
updates.keySet().forEach(key -> {
switch (key) {
final Rating rating = findRatingById(ratingId);
updates.keySet()
.forEach(key -> {
switch (key) {
case "stars":
rating.setStars(Integer.parseInt(updates.get(key)));
break;
}
});
}
});
return ratingRepository.save(rating);
}
public Rating updateRating(Rating rating, Long ratingId) {
Preconditions.checkNotNull(rating);
Preconditions.checkState(rating.getId() == ratingId);
Preconditions.checkNotNull(ratingRepository.findOne(ratingId));
return ratingRepository.save(rating);
}
}