Skip to content

Commit 1938c89

Browse files
committed
Getting all books by author id
Signed-off-by: Kemal Žigović <kvant800@gmail.com>
1 parent 984ca38 commit 1938c89

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/main/java/com/kvark900/api/controller/BookController.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ public ResponseEntity<Book> getBook(@PathVariable Long id){
4646
else return new ResponseEntity<>(book, HttpStatus.OK);
4747
}
4848

49-
@GetMapping ("/by-topics-id/{topicIds}")
49+
@GetMapping ("/by-topic-id/{topicIds}")
5050
public ResponseEntity<List<Book>> getAllBooksByTopicId(@PathVariable Long [] topicIds){
5151
List<Book> allBooksByTopicId = new ArrayList<>();
5252
for (Long topicId : topicIds){
53-
List<Book> booksByTopicId = bookService.findByTopicId(topicId);
53+
List<Book> booksByTopicId = bookService.findByTopicsId(topicId);
5454
if(!booksByTopicId.isEmpty()){
5555
allBooksByTopicId.addAll(booksByTopicId);
5656
}
@@ -61,6 +61,20 @@ public ResponseEntity<List<Book>> getAllBooksByTopicId(@PathVariable Long [] top
6161
else return new ResponseEntity<>(allBooksByTopicId, HttpStatus.OK);
6262
}
6363

64+
@GetMapping ("/by-author-id/{authorIds}")
65+
public ResponseEntity<List<Book>> getAllBooksByAuthorId(@PathVariable Long [] authorIds){
66+
List<Book> allBooksByAuthorId = new ArrayList<>();
67+
for (Long authorId : authorIds){
68+
List<Book> booksByAuthorId = bookService.findByAuthorsId(authorId);
69+
if(!booksByAuthorId.isEmpty()){
70+
allBooksByAuthorId.addAll(booksByAuthorId);
71+
}
72+
}
73+
if(allBooksByAuthorId.isEmpty()){
74+
return new ResponseEntity<>(allBooksByAuthorId, HttpStatus.NO_CONTENT);
75+
}
76+
else return new ResponseEntity<>(allBooksByAuthorId, HttpStatus.OK);
77+
}
6478

6579
@PostMapping ("")
6680
public ResponseEntity<Book> saveBook(@RequestBody @Valid Book book, BindingResult bindingResult,

src/main/java/com/kvark900/api/repository/BookRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
public interface BookRepository extends JpaRepository<Book, Long> {
1313
Book findByTitleAllIgnoreCase(String title);
1414
List<Book> findByTopicsId(Long id);
15+
List<Book> findByAuthorsId(Long id);
1516
}

src/main/java/com/kvark900/api/service/BookService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public Book findById(Long id){
2929

3030
public Book findByTitle (String title){return bookRepository.findByTitleAllIgnoreCase(title);}
3131

32-
public List<Book> findByTopicId (Long id){return bookRepository.findByTopicsId(id);}
32+
public List<Book> findByTopicsId(Long id){return bookRepository.findByTopicsId(id);}
33+
34+
public List<Book> findByAuthorsId(Long id){return bookRepository.findByAuthorsId(id);}
3335

3436

3537
public void save(Book book){

0 commit comments

Comments
 (0)