Added shelf functionality
This commit is contained in:
+5
-3
@@ -1,6 +1,8 @@
|
||||
package com.adityachandel.booklore.controller;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.BookDTO;
|
||||
import com.adityachandel.booklore.model.dto.ShelfDTO;
|
||||
import com.adityachandel.booklore.model.dto.request.AssignShelvesRequest;
|
||||
import com.adityachandel.booklore.model.dto.request.ShelfCreateRequest;
|
||||
import com.adityachandel.booklore.service.ShelfService;
|
||||
import jakarta.validation.Valid;
|
||||
@@ -38,9 +40,9 @@ public class ShelfController {
|
||||
return ResponseEntity.ok(shelfService.updateShelf(id, request));
|
||||
}
|
||||
|
||||
@PostMapping("/{shelfId}/book/{bookId}")
|
||||
public ResponseEntity<ShelfDTO> addBookToShelf(@PathVariable Long shelfId, @PathVariable Long bookId) {
|
||||
return ResponseEntity.ok(shelfService.addBookToShelf(shelfId, bookId));
|
||||
@PostMapping("/assign-shelves")
|
||||
public ResponseEntity<BookDTO> addBookToShelf(@RequestBody @Valid AssignShelvesRequest request) {
|
||||
return ResponseEntity.ok(shelfService.addBookToShelf(request.getBookId(), request.getShelfIds()));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{shelfId}")
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.adityachandel.booklore.model.dto.request;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class AssignShelvesRequest {
|
||||
private Long bookId;
|
||||
private List<Long> shelfIds;
|
||||
}
|
||||
@@ -22,11 +22,6 @@ public class Shelf {
|
||||
@Column(name = "name", nullable = false, unique = true)
|
||||
private String name;
|
||||
|
||||
@ManyToMany(fetch = FetchType.LAZY)
|
||||
@JoinTable(
|
||||
name = "book_shelf_mapping",
|
||||
joinColumns = @JoinColumn(name = "book_id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "shelf_id")
|
||||
)
|
||||
@ManyToMany(mappedBy = "shelves", fetch = FetchType.LAZY)
|
||||
private Set<Book> books = new HashSet<>();
|
||||
}
|
||||
|
||||
@@ -1,24 +1,28 @@
|
||||
package com.adityachandel.booklore.service;
|
||||
|
||||
import com.adityachandel.booklore.exception.ApiError;
|
||||
import com.adityachandel.booklore.model.dto.BookDTO;
|
||||
import com.adityachandel.booklore.model.dto.ShelfDTO;
|
||||
import com.adityachandel.booklore.model.dto.request.ShelfCreateRequest;
|
||||
import com.adityachandel.booklore.model.entity.Book;
|
||||
import com.adityachandel.booklore.model.entity.Shelf;
|
||||
import com.adityachandel.booklore.repository.BookRepository;
|
||||
import com.adityachandel.booklore.repository.ShelfRepository;
|
||||
import com.adityachandel.booklore.transformer.BookTransformer;
|
||||
import com.adityachandel.booklore.transformer.ShelfTransformer;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Service
|
||||
public class ShelfService {
|
||||
|
||||
private final ShelfRepository shelfRepository;
|
||||
private final BookRepository bookRepository;
|
||||
private final BookRepository bookRepository;
|
||||
|
||||
public ShelfDTO createShelf(ShelfCreateRequest request) {
|
||||
boolean exists = shelfRepository.existsByName(request.getName());
|
||||
@@ -36,14 +40,26 @@ public class ShelfService {
|
||||
return ShelfTransformer.convertToShelfDTO(shelfRepository.save(shelf));
|
||||
}
|
||||
|
||||
public ShelfDTO addBookToShelf(Long shelfId, Long bookId) {
|
||||
Shelf shelf = shelfRepository.findById(shelfId)
|
||||
.orElseThrow(() -> ApiError.SHELF_NOT_FOUND.createException(shelfId));
|
||||
@Transactional
|
||||
public BookDTO addBookToShelf(Long bookId, List<Long> shelfIds) {
|
||||
Book book = bookRepository.findById(bookId)
|
||||
.orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||
shelf.getBooks().add(book);
|
||||
shelfRepository.save(shelf);
|
||||
return ShelfTransformer.convertToShelfDTO(shelf);
|
||||
|
||||
List<Shelf> currentShelves = book.getShelves();
|
||||
List<Shelf> shelvesToAdd = shelfRepository.findAllById(shelfIds);
|
||||
|
||||
currentShelves.stream()
|
||||
.filter(shelf -> !shelfIds.contains(shelf.getId()))
|
||||
.forEach(shelf -> shelf.getBooks().remove(book));
|
||||
|
||||
book.getShelves().clear();
|
||||
book.getShelves().addAll(shelvesToAdd);
|
||||
|
||||
shelvesToAdd.forEach(shelf -> shelf.getBooks().add(book));
|
||||
|
||||
bookRepository.save(book);
|
||||
shelfRepository.saveAll(shelvesToAdd);
|
||||
return BookTransformer.convertToBookDTO(book);
|
||||
}
|
||||
|
||||
public List<ShelfDTO> getShelves() {
|
||||
|
||||
Reference in New Issue
Block a user