Could you please help me and explain how should I pass an author value?
@GetMapping(value = "/search")
public ResponseEntity<List<Book>> searchBooksByTitleAndAuthor(
@RequestParam(value = "title", required = false) final String title,
@RequestParam(value = "author", required = false) final Author author) {
HttpStatus httpStatus = HttpStatus.OK;
List<Book> books = null;
if (title == null && author == null) {
log.info("Empty request");
httpStatus = HttpStatus.BAD_REQUEST;
} else if (title == null || author == null) {
books = bookService.getBooksByTitleOrAuthor(title, author);
} else {
Optional<Book> book = bookService.getBookByTitleAndAuthor(title, author);
if (book.isPresent()) {
books = Arrays.asList(book.get());
}
}
if (books == null) {
return new ResponseEntity<>(httpStatus);
} else {
return new ResponseEntity<>(books, httpStatus);
}
}
And Author
class that looks like:
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Getter
@EqualsAndHashCode
@ToString
public final class Author {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String name;
private LocalDate dateOfBirth;
private String bio;
}
Is it a good approach to use author or @RequestParam instead of request body in this situation? I've thought about requesting only String that is authors' name but it will affect service's methods.