Skip to content

Commit 984ca38

Browse files
committed
Getting all books by topic ids; suppressing duplicates warnings; cascadeType changes
Signed-off-by: Kemal Žigović <kvant800@gmail.com>
1 parent 580418a commit 984ca38

File tree

8 files changed

+31
-5
lines changed

8 files changed

+31
-5
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
/**
1717
* Created by Keno&Kemo on 17.12.2017..
1818
*/
19+
@SuppressWarnings("Duplicates")
1920
@RestController
2021
@RequestMapping(value = "/authors", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
2122
consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
import org.springframework.web.util.UriComponentsBuilder;
1212

1313
import javax.validation.Valid;
14+
import java.util.ArrayList;
1415
import java.util.List;
1516

17+
@SuppressWarnings("Duplicates")
1618
@RestController
1719
@RequestMapping(value = "/books", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
1820
consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
@@ -44,6 +46,22 @@ public ResponseEntity<Book> getBook(@PathVariable Long id){
4446
else return new ResponseEntity<>(book, HttpStatus.OK);
4547
}
4648

49+
@GetMapping ("/by-topics-id/{topicIds}")
50+
public ResponseEntity<List<Book>> getAllBooksByTopicId(@PathVariable Long [] topicIds){
51+
List<Book> allBooksByTopicId = new ArrayList<>();
52+
for (Long topicId : topicIds){
53+
List<Book> booksByTopicId = bookService.findByTopicId(topicId);
54+
if(!booksByTopicId.isEmpty()){
55+
allBooksByTopicId.addAll(booksByTopicId);
56+
}
57+
}
58+
if(allBooksByTopicId.isEmpty()){
59+
return new ResponseEntity<>(allBooksByTopicId, HttpStatus.NO_CONTENT);
60+
}
61+
else return new ResponseEntity<>(allBooksByTopicId, HttpStatus.OK);
62+
}
63+
64+
4765
@PostMapping ("")
4866
public ResponseEntity<Book> saveBook(@RequestBody @Valid Book book, BindingResult bindingResult,
4967
UriComponentsBuilder uriComponentsBuilder){

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import javax.validation.Valid;
1414
import java.util.List;
1515

16+
@SuppressWarnings("Duplicates")
1617
@RestController
1718
@RequestMapping (value = "/topics",produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
1819
consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
@@ -56,7 +57,7 @@ public ResponseEntity<Topic> addTopic(@RequestBody @Valid Topic topic, BindingRe
5657
return new ResponseEntity<>(headers, HttpStatus.BAD_REQUEST);
5758
}
5859
else{
59-
topicService.save(topic);
60+
topicService.save(topic);
6061
headers.setLocation(uriComponentsBuilder.path("/books/{id}").
6162
buildAndExpand(topic.getId()).toUri());
6263
return new ResponseEntity<>(topic, headers, HttpStatus.CREATED);

src/main/java/com/kvark900/api/model/Author.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class Author {
2323

2424
// @NotNull
2525
@JsonIgnore
26-
@ManyToMany(cascade = CascadeType.PERSIST, mappedBy = "authors")
26+
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, mappedBy = "authors")
2727
private Set<Book> books = new HashSet<>();
2828

2929

@@ -35,6 +35,7 @@ public Author(String name, String surname, Set<Book> books) {
3535
this.surname = surname;
3636
this.books = books;
3737
}
38+
3839
@Autowired
3940
public Author(String name, String surname) {
4041
this.id = id;

src/main/java/com/kvark900/api/model/Book.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class Book {
1616
private String title;
1717

1818
@NotNull
19-
@ManyToMany(cascade = CascadeType.PERSIST, targetEntity = Author.class)
19+
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, targetEntity = Author.class)
2020
@JoinTable(name = "book_author",
2121
joinColumns = @JoinColumn(name = "book_id"),
2222
inverseJoinColumns = @JoinColumn(name = "author_id")

src/main/java/com/kvark900/api/model/Topic.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class Topic {
2323
private String description;
2424

2525
@JsonIgnore
26-
@ManyToMany(cascade = CascadeType.ALL, targetEntity = Book.class, mappedBy = "topics")
26+
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, targetEntity = Book.class, mappedBy = "topics")
2727
private Set<Book> books = new HashSet<>();
2828

2929
//Constructors

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import org.springframework.data.jpa.repository.JpaRepository;
66
import org.springframework.stereotype.Repository;
77

8+
import java.util.List;
9+
810

911
@Repository
1012
public interface BookRepository extends JpaRepository<Book, Long> {
1113
Book findByTitleAllIgnoreCase(String title);
12-
14+
List<Book> findByTopicsId(Long id);
1315
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +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);}
33+
34+
3235
public void save(Book book){
3336
bookRepository.save(book);
3437
}

0 commit comments

Comments
 (0)