refactor(api): always map the all-metadata-locked field (#2739)
This commit is contained in:
@@ -35,8 +35,14 @@ public interface BookMapperV2 {
|
||||
@Mapping(target = "categories", source = "categories", qualifiedByName = "mapCategories")
|
||||
@Mapping(target = "moods", source = "moods", qualifiedByName = "mapMoods")
|
||||
@Mapping(target = "tags", source = "tags", qualifiedByName = "mapTags")
|
||||
@Mapping(target = "allMetadataLocked", source = ".", qualifiedByName = "mapAllMetadataLocked")
|
||||
BookMetadata mapMetadata(BookMetadataEntity metadataEntity);
|
||||
|
||||
@Named("mapAllMetadataLocked")
|
||||
default boolean mapAllMetadataLocked(BookMetadataEntity metadataEntity) {
|
||||
return metadataEntity.areAllFieldsLocked();
|
||||
}
|
||||
|
||||
@AfterMapping
|
||||
default void mapAudiobookMetadata(BookEntity bookEntity, @MappingTarget Book book) {
|
||||
if (book.getMetadata() == null) {
|
||||
|
||||
@@ -177,21 +177,17 @@ public class BookQueryService {
|
||||
}
|
||||
|
||||
if (stripForListView) {
|
||||
stripFieldsForListView(dto, bookEntity);
|
||||
stripFieldsForListView(dto);
|
||||
}
|
||||
|
||||
return dto;
|
||||
}
|
||||
|
||||
private void stripFieldsForListView(Book dto, BookEntity bookEntity) {
|
||||
private void stripFieldsForListView(Book dto) {
|
||||
dto.setLibraryPath(null);
|
||||
|
||||
BookMetadata m = dto.getMetadata();
|
||||
if (m != null) {
|
||||
// Compute allMetadataLocked before stripping lock flags
|
||||
BookMetadataEntity metadataEntity = bookEntity.getMetadata();
|
||||
m.setAllMetadataLocked(metadataEntity != null && metadataEntity.areAllFieldsLocked());
|
||||
|
||||
// Strip lock flags
|
||||
m.setTitleLocked(null);
|
||||
m.setSubtitleLocked(null);
|
||||
|
||||
@@ -1,24 +1,30 @@
|
||||
package org.booklore.mapper.v2;
|
||||
|
||||
import org.booklore.mapper.BookMapper;
|
||||
import org.booklore.model.dto.Book;
|
||||
import org.booklore.model.entity.BookFileEntity;
|
||||
import org.booklore.model.entity.BookEntity;
|
||||
import org.booklore.model.entity.BookMetadataEntity;
|
||||
import org.booklore.model.entity.LibraryEntity;
|
||||
import org.booklore.model.entity.LibraryPathEntity;
|
||||
import org.booklore.model.enums.BookFileType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("test")
|
||||
class BookMapperV2Test {
|
||||
@Autowired
|
||||
private BookMapperV2 mapper;
|
||||
|
||||
private final BookMapperV2 mapper = Mappers.getMapper(BookMapperV2.class);
|
||||
|
||||
@Test
|
||||
void shouldMapFilepathFieldsCorrectly() {
|
||||
private BookEntity createBook() {
|
||||
LibraryEntity library = new LibraryEntity();
|
||||
library.setId(123L);
|
||||
library.setName("Test Library");
|
||||
@@ -50,6 +56,13 @@ class BookMapperV2Test {
|
||||
entity.setLibrary(library);
|
||||
entity.setLibraryPath(libraryPath);
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldMapFilepathFieldsCorrectly() {
|
||||
BookEntity entity = createBook();
|
||||
|
||||
Book dto = mapper.toDTO(entity);
|
||||
|
||||
assertThat(dto).isNotNull();
|
||||
@@ -71,4 +84,54 @@ class BookMapperV2Test {
|
||||
assertThat(dto.getSupplementaryFiles().get(0).getFileName()).isEqualTo("test-book-cover.jpg");
|
||||
assertThat(dto.getSupplementaryFiles().get(0).isBook()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void metadataIsNull() {
|
||||
BookEntity entity = createBook();
|
||||
entity.setMetadata(null);
|
||||
|
||||
Book dto = mapper.toDTO(entity);
|
||||
|
||||
assertThat(dto.getMetadata()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void allMetadataLocked_unlockedMetadata() {
|
||||
BookEntity entity = createBook();
|
||||
|
||||
BookMetadataEntity bookMetadataEntity = new BookMetadataEntity();
|
||||
|
||||
entity.setMetadata(bookMetadataEntity);
|
||||
|
||||
Book dto = mapper.toDTO(entity);
|
||||
|
||||
assertThat(dto.getMetadata().getAllMetadataLocked()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void allMetadataLocked_partiallyLockedMetadata() {
|
||||
BookEntity entity = createBook();
|
||||
|
||||
BookMetadataEntity bookMetadataEntity = new BookMetadataEntity();
|
||||
bookMetadataEntity.setTitleLocked(true);
|
||||
entity.setMetadata(bookMetadataEntity);
|
||||
|
||||
Book dto = mapper.toDTO(entity);
|
||||
|
||||
assertThat(dto.getMetadata().getAllMetadataLocked()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void allMetadataLocked_lockedMetadata() {
|
||||
BookEntity entity = createBook();
|
||||
|
||||
BookMetadataEntity bookMetadataEntity = new BookMetadataEntity();
|
||||
bookMetadataEntity.setDescription("Foo");
|
||||
bookMetadataEntity.applyLockToAllFields(true);
|
||||
entity.setMetadata(bookMetadataEntity);
|
||||
|
||||
Book dto = mapper.toDTO(entity);
|
||||
|
||||
assertThat(dto.getMetadata().getAllMetadataLocked()).isTrue();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user