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:
jduar
2025-09-25 17:07:13 +01:00
committed by GitHub
parent 7ca27452ca
commit b510f4cf3f
3 changed files with 13 additions and 1 deletions

View File

@@ -3,9 +3,11 @@ package com.adityachandel.booklore.mapper;
import com.adityachandel.booklore.model.dto.Shelf;
import com.adityachandel.booklore.model.entity.ShelfEntity;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
@Mapper(componentModel = "spring")
public interface ShelfMapper {
@Mapping(source = "user.id", target = "userId")
Shelf toShelf(ShelfEntity shelfEntity);
}

View File

@@ -1,5 +1,6 @@
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.BookMetadata;
import com.adityachandel.booklore.model.dto.LibraryPath;
@@ -11,7 +12,7 @@ import org.mapstruct.Named;
import java.util.Set;
import java.util.stream.Collectors;
@Mapper(componentModel = "spring")
@Mapper(componentModel = "spring", uses = ShelfMapper.class)
public interface BookMapperV2 {
@Mapping(source = "library.id", target = "libraryId")

View File

@@ -142,6 +142,7 @@ public class BookService {
UserBookProgressEntity userProgress = userBookProgressRepository.findByUserIdAndBookId(user.getId(), bookId).orElse(new UserBookProgressEntity());
Book book = bookMapper.toBook(bookEntity);
book.setShelves(filterShelvesByUserId(book.getShelves(), user.getId()));
book.setLastReadTime(userProgress.getLastReadTime());
if (bookEntity.getBookType() == BookFileType.PDF) {
@@ -482,6 +483,7 @@ public class BookService {
return bookEntities.stream().map(bookEntity -> {
Book book = bookMapper.toBook(bookEntity);
book.setShelves(filterShelvesByUserId(book.getShelves(), user.getId()));
book.setFilePath(FileUtils.getBookFullPath(bookEntity));
enrichBookWithProgress(book, progressMap.get(bookEntity.getId()));
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());
}
}