WIP: Multi select
This commit is contained in:
+4
-9
@@ -2,22 +2,17 @@ package com.adityachandel.booklore.controller;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.BookDTO;
|
||||
import com.adityachandel.booklore.model.dto.BookViewerSettingDTO;
|
||||
import com.adityachandel.booklore.model.dto.BookWithNeighborsDTO;
|
||||
import com.adityachandel.booklore.model.dto.request.AssignShelvesRequest;
|
||||
import com.adityachandel.booklore.model.dto.response.GoogleBooksMetadata;
|
||||
import com.adityachandel.booklore.model.dto.request.ShelvesAssignmentRequest;
|
||||
import com.adityachandel.booklore.model.dto.request.SetMetadataRequest;
|
||||
import com.adityachandel.booklore.model.dto.response.GoogleBooksMetadata;
|
||||
import com.adityachandel.booklore.service.BooksService;
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.Max;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
@RequestMapping("/v1/book")
|
||||
@@ -86,7 +81,7 @@ public class BookController {
|
||||
}
|
||||
|
||||
@PostMapping("/assign-shelves")
|
||||
public ResponseEntity<BookDTO> addBookToShelf(@RequestBody @Valid AssignShelvesRequest request) {
|
||||
return ResponseEntity.ok(booksService.addBookToShelf(request.getBookId(), request.getShelfIds()));
|
||||
public ResponseEntity<List<BookDTO>> addBookToShelf(@RequestBody @Valid ShelvesAssignmentRequest request) {
|
||||
return ResponseEntity.ok(booksService.assignShelvesToBooks(request.getBookIds(), request.getShelvesToAssign(), request.getShelvesToUnassign()));
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,6 @@ 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;
|
||||
|
||||
-11
@@ -1,11 +0,0 @@
|
||||
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;
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.adityachandel.booklore.model.dto.request;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
@Data
|
||||
public class ShelvesAssignmentRequest {
|
||||
private Set<Long> bookIds;
|
||||
private Set<Long> shelvesToAssign;
|
||||
private Set<Long> shelvesToUnassign;
|
||||
}
|
||||
@@ -15,10 +15,6 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -33,8 +29,7 @@ import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
@@ -220,25 +215,32 @@ public class BooksService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public BookDTO addBookToShelf(Long bookId, List<Long> shelfIds) {
|
||||
Book book = bookRepository.findById(bookId)
|
||||
.orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||
public List<BookDTO> assignShelvesToBooks(Set<Long> bookIds, Set<Long> shelfIdsToAssign, Set<Long> shelfIdsToUnassign) {
|
||||
List<Book> books = bookRepository.findAllById(bookIds);
|
||||
List<Shelf> shelvesToAssign = shelfRepository.findAllById(shelfIdsToAssign);
|
||||
List<Shelf> shelvesToUnassign = shelfRepository.findAllById(shelfIdsToUnassign);
|
||||
|
||||
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);
|
||||
for (Book book : books) {
|
||||
for (Shelf shelf : shelvesToUnassign) {
|
||||
Optional<Shelf> shelfOpt = book.getShelves().stream().filter(s -> s.getId().equals(shelf.getId())).findAny();
|
||||
shelfOpt.ifPresent(value -> book.getShelves().remove(value));
|
||||
shelf.getBooks().remove(book);
|
||||
shelfRepository.save(shelf);
|
||||
}
|
||||
for (Shelf shelf : shelvesToAssign) {
|
||||
Optional<Shelf> shelfOpt = book.getShelves().stream().filter(s -> s.getId().equals(shelf.getId())).findAny();
|
||||
if (shelfOpt.isEmpty()) {
|
||||
book.getShelves().add(shelf);
|
||||
}
|
||||
Optional<Book> bookOpt = shelf.getBooks().stream().filter(b -> b.getId().equals(book.getId())).findAny();
|
||||
if (bookOpt.isEmpty()) {
|
||||
shelf.getBooks().add(book);
|
||||
}
|
||||
shelfRepository.save(shelf);
|
||||
}
|
||||
bookRepository.save(book);
|
||||
}
|
||||
return books.stream().map(BookTransformer::convertToBookDTO).toList();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user