fix: stop sending shelves not associated with the user (#1201)
* stop sending shelves not associated with the user * correctly map shelves to books. filter shelves not belonging to user
This commit is contained in:
@@ -3,9 +3,11 @@ package com.adityachandel.booklore.mapper;
|
|||||||
import com.adityachandel.booklore.model.dto.Shelf;
|
import com.adityachandel.booklore.model.dto.Shelf;
|
||||||
import com.adityachandel.booklore.model.entity.ShelfEntity;
|
import com.adityachandel.booklore.model.entity.ShelfEntity;
|
||||||
import org.mapstruct.Mapper;
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
|
||||||
@Mapper(componentModel = "spring")
|
@Mapper(componentModel = "spring")
|
||||||
public interface ShelfMapper {
|
public interface ShelfMapper {
|
||||||
|
|
||||||
|
@Mapping(source = "user.id", target = "userId")
|
||||||
Shelf toShelf(ShelfEntity shelfEntity);
|
Shelf toShelf(ShelfEntity shelfEntity);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.adityachandel.booklore.mapper.v2;
|
package com.adityachandel.booklore.mapper.v2;
|
||||||
|
|
||||||
|
import com.adityachandel.booklore.mapper.ShelfMapper;
|
||||||
import com.adityachandel.booklore.model.dto.Book;
|
import com.adityachandel.booklore.model.dto.Book;
|
||||||
import com.adityachandel.booklore.model.dto.BookMetadata;
|
import com.adityachandel.booklore.model.dto.BookMetadata;
|
||||||
import com.adityachandel.booklore.model.dto.LibraryPath;
|
import com.adityachandel.booklore.model.dto.LibraryPath;
|
||||||
@@ -11,7 +12,7 @@ import org.mapstruct.Named;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Mapper(componentModel = "spring")
|
@Mapper(componentModel = "spring", uses = ShelfMapper.class)
|
||||||
public interface BookMapperV2 {
|
public interface BookMapperV2 {
|
||||||
|
|
||||||
@Mapping(source = "library.id", target = "libraryId")
|
@Mapping(source = "library.id", target = "libraryId")
|
||||||
|
|||||||
@@ -142,6 +142,7 @@ public class BookService {
|
|||||||
UserBookProgressEntity userProgress = userBookProgressRepository.findByUserIdAndBookId(user.getId(), bookId).orElse(new UserBookProgressEntity());
|
UserBookProgressEntity userProgress = userBookProgressRepository.findByUserIdAndBookId(user.getId(), bookId).orElse(new UserBookProgressEntity());
|
||||||
|
|
||||||
Book book = bookMapper.toBook(bookEntity);
|
Book book = bookMapper.toBook(bookEntity);
|
||||||
|
book.setShelves(filterShelvesByUserId(book.getShelves(), user.getId()));
|
||||||
book.setLastReadTime(userProgress.getLastReadTime());
|
book.setLastReadTime(userProgress.getLastReadTime());
|
||||||
|
|
||||||
if (bookEntity.getBookType() == BookFileType.PDF) {
|
if (bookEntity.getBookType() == BookFileType.PDF) {
|
||||||
@@ -482,6 +483,7 @@ public class BookService {
|
|||||||
|
|
||||||
return bookEntities.stream().map(bookEntity -> {
|
return bookEntities.stream().map(bookEntity -> {
|
||||||
Book book = bookMapper.toBook(bookEntity);
|
Book book = bookMapper.toBook(bookEntity);
|
||||||
|
book.setShelves(filterShelvesByUserId(book.getShelves(), user.getId()));
|
||||||
book.setFilePath(FileUtils.getBookFullPath(bookEntity));
|
book.setFilePath(FileUtils.getBookFullPath(bookEntity));
|
||||||
enrichBookWithProgress(book, progressMap.get(bookEntity.getId()));
|
enrichBookWithProgress(book, progressMap.get(bookEntity.getId()));
|
||||||
return book;
|
return book;
|
||||||
@@ -637,4 +639,11 @@ public class BookService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Set<Shelf> filterShelvesByUserId(Set<Shelf> shelves, Long userId) {
|
||||||
|
if (shelves == null) return Collections.emptySet();
|
||||||
|
return shelves.stream()
|
||||||
|
.filter(shelf -> userId.equals(shelf.getUserId()))
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user