Skip to content

Commit

Permalink
Minor tweaks to support new React client
Browse files Browse the repository at this point in the history
  • Loading branch information
Aidan Whiteley committed Oct 24, 2024
1 parent 4513077 commit 6c6da73
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>com.aidanwhiteley</groupId>
<artifactId>books</artifactId>
<version>0.42.1-RELEASE</version>
<version>0.50.0-RELEASE</version>
<packaging>jar</packaging>

<name>Books Microservice</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,14 @@ public ResponseEntity<Book> updateBook(@Valid @RequestBody Book book, Principal
) {
// Retrieve and update Google Book details synchronously
Item item = googleBooksDaoSync.searchGoogleBooksByGoogleBookId(book.getGoogleBookId());
book.setGoogleBookDetails(item);
currentBookState.setGoogleBookDetails(item);
} else if (book.getGoogleBookId() == null || book.getGoogleBookId().isEmpty()) {
book.setGoogleBookDetails(null);
currentBookState.setGoogleBookDetails(null);
}

bookRepository.save(book);
Book mergedBook = mergeUpdatesOntoExistingBook(currentBookState, book);

bookRepository.save(mergedBook);
return ResponseEntity.noContent().build();
} else {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
Expand All @@ -138,6 +140,20 @@ public ResponseEntity<Book> updateBook(@Valid @RequestBody Book book, Principal
}
}

private Book mergeUpdatesOntoExistingBook(Book currentBookState, Book book) {

// Set the fields the owner / admin is allowed to manually update
currentBookState.setSummary(book.getSummary());
currentBookState.setGenre(book.getGenre());
currentBookState.setTitle(book.getTitle());
currentBookState.setSummary(book.getSummary());
currentBookState.setGoogleBookId(book.getGoogleBookId());
currentBookState.setRating(book.getRating());
currentBookState.setAuthor(book.getAuthor());

return currentBookState;
}

@DeleteMapping(value = "/books/{id}")
public ResponseEntity<Book> deleteBookById(@PathVariable String id, Principal principal) {

Expand Down Expand Up @@ -216,9 +232,9 @@ public Page<Book> findByReader(@RequestParam String reader, @RequestParam(defaul
return bookRepository.findByReaderOrderByCreatedDateTimeDesc(pageObj, reader);
}

@GetMapping(value = {"/googlebooks", "googlebooks/"}, params = "title")
public BookSearchResult findGoogleBooksByTitle(@RequestParam String title) {
return googleBooksDaoSync.searchGoogBooksByTitle(title);
@GetMapping(value = {"/googlebooks", "googlebooks/"}, params = {"title", "author"})
public BookSearchResult findGoogleBooksByTitleAndAuthor(@RequestParam String title, @RequestParam String author) {
return googleBooksDaoSync.searchGoogBooksByTitleAndAuthor(title, author);
}

@GetMapping(value = "/books/readers")
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/aidanwhiteley/books/domain/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class Book extends Auditable {

@NotNull
@Length(min = 1, max = 75)
@Setter
private String author;

@NotNull
Expand All @@ -51,11 +52,14 @@ public class Book extends Auditable {

@NotNull
@Length(min = 1, max = 20000)
@Setter
private String summary;

@NotNull
@Setter
private Rating rating;

@Setter
private String googleBookId;

@Setter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,17 @@ public void init() {
this.googleBooksRestTemplate = buildRestTemplate(restTemplateBuilder);
}

public BookSearchResult searchGoogBooksByTitle(String title) {
public BookSearchResult searchGoogBooksByTitleAndAuthor(String title, String author) {

String encodedTitle;
encodedTitle = URLEncoder.encode(title, StandardCharsets.UTF_8);
String encodedTitle = URLEncoder.encode(title, StandardCharsets.UTF_8);
String encodedAuthor = URLEncoder.encode(author, StandardCharsets.UTF_8);

googleBooksRestTemplate.getMessageConverters().add(0,
new StringHttpMessageConverter(StandardCharsets.UTF_8));

final String searchString = googleBooksApiConfig.getSearchUrl() + encodedTitle + "&" +
googleBooksApiConfig.getCountryCode() + "&" + googleBooksApiConfig.getMaxResults();
final String searchString = googleBooksApiConfig.getSearchUrl() + "+intitle:" + encodedTitle +
"+inauthor:" + encodedAuthor + "&" + googleBooksApiConfig.getCountryCode() +
"&" + googleBooksApiConfig.getMaxResults();

if (LOGGER.isInfoEnabled()) {
LOGGER.info("Google Books API called with API called: {}", searchString);
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/application-dev-mongo-java-server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ books:
getByIdUrl: http://localhost:${wiremock.server.port}/books/v1/volumes/
countryCode: country=GB
maxResults: maxResults=30
connectTimeout: 500
readTimeout: 750
connectTimeout: 2500
readTimeout: 2000

reload:
development:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class GoogleBooksDaoSyncTest extends IntegrationTest {
private GoogleBooksDaoSync theDao;

@Test
void findByTitle() {
BookSearchResult result = theDao.searchGoogBooksByTitle("Design Patterns");
void findByTitleAndAuthor() {
BookSearchResult result = theDao.searchGoogBooksByTitleAndAuthor("Design Patterns", "Gamma");
assertNotNull(result);
assertEquals(NUMBER_OF_BOOKS_IN_SEARCH_RESULTS, result.getItems().size());
assertTrue(result.getItems().get(0).getId().length() > 0, "Should have found a book id");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"request" : {
"url" : "/books/v1/volumes?q=Design+Patterns&country=GB&maxResults=30",
"url" : "/books/v1/volumes?q=+intitle:Design+Patterns+inauthor:Gamma&country=GB&maxResults=30",
"method" : "GET"
},
"response" : {
Expand Down

0 comments on commit 6c6da73

Please sign in to comment.