Add bulk lock/unlock metadata functionality for books

This commit is contained in:
adityachandelgit
2025-04-18 14:28:19 -06:00
committed by Aditya Chandel
parent 5c50a157cd
commit 941206bdf3
21 changed files with 332 additions and 46 deletions
@@ -4,6 +4,7 @@ import com.adityachandel.booklore.mapper.BookMetadataMapper;
import com.adityachandel.booklore.model.dto.BookMetadata;
import com.adityachandel.booklore.model.dto.request.FieldLockRequest;
import com.adityachandel.booklore.model.dto.request.MetadataRefreshRequest;
import com.adityachandel.booklore.model.dto.request.ToggleAllLockRequest;
import com.adityachandel.booklore.quartz.JobSchedulerService;
import com.adityachandel.booklore.service.BookMetadataService;
import com.adityachandel.booklore.service.BookMetadataUpdater;
@@ -57,7 +58,7 @@ public class MetadataController {
return ResponseEntity.ok(updatedMetadata);
}
@PutMapping("/{bookId}/metadata/lock")
@PutMapping("/{bookId}/metadata/toggle-field-lock")
@PreAuthorize("@securityUtil.canEditMetadata() or @securityUtil.isAdmin()")
public ResponseEntity<BookMetadata> updateFieldLockState(@RequestBody FieldLockRequest request) {
long bookId = request.getBookId();
@@ -66,6 +67,12 @@ public class MetadataController {
return ResponseEntity.ok(bookMetadataService.updateFieldLockState(bookId, field, isLocked));
}
@PutMapping("/metadata/toggle-all-lock")
@PreAuthorize("@securityUtil.canEditMetadata() or @securityUtil.isAdmin()")
public ResponseEntity<List<BookMetadata>> toggleAllMetadata(@RequestBody ToggleAllLockRequest request) {
return ResponseEntity.ok(bookMetadataService.toggleAllLock(request));
}
@PostMapping("/regenerate-covers")
@PreAuthorize("@securityUtil.canEditMetadata() or @securityUtil.isAdmin()")
public void regenerateCovers() {
@@ -0,0 +1,12 @@
package com.adityachandel.booklore.model.dto.request;
import com.adityachandel.booklore.model.enums.Lock;
import lombok.Data;
import java.util.Set;
@Data
public class ToggleAllLockRequest {
private Set<Long> bookIds;
private Lock lock;
}
@@ -150,4 +150,27 @@ public class BookMetadataEntity {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "book")
private List<BookAwardEntity> awards;
public void applyLockToAllFields(boolean lock) {
this.allFieldsLocked = lock;
this.titleLocked = lock;
this.subtitleLocked = lock;
this.publisherLocked = lock;
this.publishedDateLocked = lock;
this.descriptionLocked = lock;
this.isbn13Locked = lock;
this.isbn10Locked = lock;
this.pageCountLocked = lock;
this.thumbnailLocked = lock;
this.languageLocked = lock;
this.ratingLocked = lock;
this.reviewCountLocked = lock;
this.coverLocked = lock;
this.seriesNameLocked = lock;
this.seriesNumberLocked = lock;
this.seriesTotalLocked = lock;
this.authorsLocked = lock;
this.categoriesLocked = lock;
}
}
@@ -0,0 +1,5 @@
package com.adityachandel.booklore.model.enums;
public enum Lock {
LOCK, UNLOCK
}
@@ -7,10 +7,12 @@ import com.adityachandel.booklore.model.dto.Book;
import com.adityachandel.booklore.model.dto.BookMetadata;
import com.adityachandel.booklore.model.dto.request.MetadataRefreshOptions;
import com.adityachandel.booklore.model.dto.request.MetadataRefreshRequest;
import com.adityachandel.booklore.model.dto.request.ToggleAllLockRequest;
import com.adityachandel.booklore.model.dto.settings.AppSettings;
import com.adityachandel.booklore.model.entity.BookEntity;
import com.adityachandel.booklore.model.entity.BookMetadataEntity;
import com.adityachandel.booklore.model.entity.LibraryEntity;
import com.adityachandel.booklore.model.enums.Lock;
import com.adityachandel.booklore.model.websocket.Topic;
import com.adityachandel.booklore.repository.BookMetadataRepository;
import com.adityachandel.booklore.repository.BookRepository;
@@ -441,4 +443,15 @@ public class BookMetadataService {
}
log.info("{} Successfully regenerated cover for book ID {} ({})", progress, book.getId(), title);
}
@Transactional
public List<BookMetadata> toggleAllLock(ToggleAllLockRequest request) {
boolean lock = request.getLock() == Lock.LOCK;
List<BookEntity> books = bookRepository.findAllByIdIn(request.getBookIds())
.stream()
.peek(book -> book.getMetadata().applyLockToAllFields(lock))
.toList();
bookRepository.saveAll(books);
return books.stream().map(b ->bookMetadataMapper.toBookMetadata(b.getMetadata(), false)).collect(Collectors.toList());
}
}