feat(metadata-lock): add UI for toggling individual field locks with save/reset actions
This commit is contained in:
committed by
Aditya Chandel
parent
8518e57d92
commit
128fa1c847
+8
-4
@@ -2,13 +2,10 @@ package com.adityachandel.booklore.controller;
|
||||
|
||||
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.model.dto.request.*;
|
||||
import com.adityachandel.booklore.quartz.JobSchedulerService;
|
||||
import com.adityachandel.booklore.service.BookMetadataService;
|
||||
import com.adityachandel.booklore.service.BookMetadataUpdater;
|
||||
import com.adityachandel.booklore.model.dto.request.FetchMetadataRequest;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
@@ -73,6 +70,13 @@ public class MetadataController {
|
||||
return ResponseEntity.ok(bookMetadataService.toggleAllLock(request));
|
||||
}
|
||||
|
||||
@PutMapping("/metadata/toggle-field-locks")
|
||||
@PreAuthorize("@securityUtil.canEditMetadata() or @securityUtil.isAdmin()")
|
||||
public ResponseEntity<List<BookMetadata>> toggleFieldLocks(@RequestBody ToggleFieldLocksRequest request) {
|
||||
List<BookMetadata> updated = bookMetadataService.toggleFieldLocks(request.getBookIds(), request.getFieldActions());
|
||||
return ResponseEntity.ok(updated);
|
||||
}
|
||||
|
||||
@PostMapping("/regenerate-covers")
|
||||
@PreAuthorize("@securityUtil.canEditMetadata() or @securityUtil.isAdmin()")
|
||||
public void regenerateCovers() {
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.adityachandel.booklore.model.dto.request;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
public class ToggleFieldLocksRequest {
|
||||
private List<Long> bookIds;
|
||||
private Map<String, String> fieldActions;
|
||||
}
|
||||
+23
@@ -31,6 +31,8 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
@@ -417,6 +419,27 @@ public class BookMetadataService {
|
||||
return bookMetadataMapper.toBookMetadata(bookMetadataRepository.save(existingMetadata), true);
|
||||
}
|
||||
|
||||
public List<BookMetadata> toggleFieldLocks(List<Long> bookIds, Map<String, String> fieldActions) {
|
||||
List<BookMetadataEntity> entitiesToUpdate = new ArrayList<>();
|
||||
for (Long bookId : bookIds) {
|
||||
BookMetadataEntity metadataEntity = bookMetadataRepository.findById(bookId).orElseThrow(() -> new IllegalArgumentException("Book metadata not found for bookId: " + bookId));
|
||||
fieldActions.forEach((field, action) -> {
|
||||
try {
|
||||
String setterName = "set" + Character.toUpperCase(field.charAt(0)) + field.substring(1);
|
||||
Method setter = BookMetadataEntity.class.getMethod(setterName, Boolean.class);
|
||||
setter.invoke(metadataEntity, "LOCK".equalsIgnoreCase(action));
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to invoke setter for field: " + field + " on bookId: " + bookId, e);
|
||||
}
|
||||
});
|
||||
entitiesToUpdate.add(metadataEntity);
|
||||
}
|
||||
List<BookMetadataEntity> savedEntities = bookMetadataRepository.saveAll(entitiesToUpdate);
|
||||
return savedEntities.stream()
|
||||
.map(entity -> bookMetadataMapper.toBookMetadata(entity, true))
|
||||
.toList();
|
||||
}
|
||||
|
||||
public BookMetadata handleCoverUpload(Long bookId, MultipartFile file) {
|
||||
fileService.createThumbnailFromFile(bookId, file);
|
||||
BookMetadataEntity metadata = bookMetadataRepository.findById(bookId).orElseThrow(() -> new IllegalArgumentException("Book not found with ID: " + bookId));
|
||||
|
||||
@@ -616,8 +616,9 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
}
|
||||
|
||||
lockUnlockMetadata() {
|
||||
const count = this.selectedBooks.size;
|
||||
this.dynamicDialogRef = this.dialogService.open(LockUnlockMetadataDialogComponent, {
|
||||
header: 'Toggle Metadata Lock',
|
||||
header: `Lock or Unlock Metadata for ${count} Selected Book${count > 1 ? 's' : ''}`,
|
||||
modal: true,
|
||||
closable: true,
|
||||
data: {
|
||||
|
||||
+25
-5
@@ -1,7 +1,27 @@
|
||||
<div class="flex flex-col items-center text-center space-y-6">
|
||||
<p class="pt-6 px-4">Lock or unlock all metadata for all the selected books</p>
|
||||
<div class="flex gap-4 justify-center">
|
||||
<p-button icon="pi pi-lock" label="Lock" outlined="true" severity="danger" (onClick)="toggleLock('LOCK')"></p-button>
|
||||
<p-button icon="pi pi-lock-open" label="Unlock" outlined="true" severity="success" (onClick)="toggleLock('UNLOCK')"></p-button>
|
||||
<div class="p-4">
|
||||
<div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-x-24 gap-y-4">
|
||||
<div *ngFor="let field of lockableFields" class="flex items-center justify-between">
|
||||
<label class="capitalize">{{ fieldLabels[field] || field }}</label>
|
||||
<p-button
|
||||
[outlined]="true"
|
||||
[label]="getLockLabel(field)"
|
||||
[icon]="getLockIcon(field)"
|
||||
[severity]="fieldLocks[field] === true ? 'danger' : fieldLocks[field] === false ? 'success' : 'secondary'"
|
||||
(click)="cycleLockState(field)"
|
||||
></p-button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p-divider></p-divider>
|
||||
|
||||
<div class="flex justify-end px-4">
|
||||
<p-button icon="pi pi-refresh" label="Reset" outlined="true" severity="warn" (onClick)="resetFieldLocks()"></p-button>
|
||||
<p-divider layout="vertical"></p-divider>
|
||||
<div class="flex gap-4 justify-center">
|
||||
<p-button icon="pi pi-lock" label="Lock All" outlined="true" severity="danger" (onClick)="toggleLockAll('LOCK')"></p-button>
|
||||
<p-button icon="pi pi-lock-open" label="Unlock All" outlined="true" severity="success" (onClick)="toggleLockAll('UNLOCK')"></p-button>
|
||||
</div>
|
||||
<p-divider layout="vertical"></p-divider>
|
||||
<p-button [label]="isSaving ? 'Saving...' : 'Save'" [outlined]="true" icon="pi pi-check" severity="info" (onClick)="applyFieldLocks()" [disabled]="bookIds.size === 0 || isSaving"></p-button>
|
||||
</div>
|
||||
|
||||
+108
-11
@@ -1,41 +1,138 @@
|
||||
import {Component, inject} from '@angular/core';
|
||||
import {Component, inject, OnInit} from '@angular/core';
|
||||
import {Button} from 'primeng/button';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {NgForOf} from '@angular/common';
|
||||
import {DynamicDialogConfig, DynamicDialogRef} from 'primeng/dynamicdialog';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {BookService} from '../../../service/book.service';
|
||||
import {Divider} from 'primeng/divider';
|
||||
|
||||
@Component({
|
||||
selector: 'app-lock-unlock-metadata-dialog',
|
||||
standalone: true,
|
||||
imports: [
|
||||
Button
|
||||
Button,
|
||||
FormsModule,
|
||||
NgForOf,
|
||||
Divider
|
||||
],
|
||||
templateUrl: './lock-unlock-metadata-dialog.component.html',
|
||||
styleUrl: './lock-unlock-metadata-dialog.component.scss'
|
||||
})
|
||||
export class LockUnlockMetadataDialogComponent {
|
||||
export class LockUnlockMetadataDialogComponent implements OnInit {
|
||||
private bookService = inject(BookService);
|
||||
private dynamicDialogConfig = inject(DynamicDialogConfig);
|
||||
private dialogRef = inject(DynamicDialogRef);
|
||||
private messageService = inject(MessageService);
|
||||
fieldLocks: Record<string, boolean | undefined> = {};
|
||||
|
||||
bookIds: Set<number> = this.dynamicDialogConfig.data.bookIds;
|
||||
|
||||
toggleLock(action: 'LOCK' | 'UNLOCK'): void {
|
||||
this.bookService.toggleAllLock(this.bookIds, action).subscribe({
|
||||
lockableFields: string[] = [
|
||||
'titleLocked', 'subtitleLocked', 'publisherLocked', 'publishedDateLocked', 'descriptionLocked',
|
||||
'isbn13Locked', 'isbn10Locked', 'asinLocked', 'pageCountLocked', 'thumbnailLocked', 'languageLocked',
|
||||
'ratingLocked', 'reviewCountLocked', 'coverLocked',
|
||||
'seriesNameLocked', 'seriesNumberLocked', 'seriesTotalLocked',
|
||||
'authorsLocked', 'categoriesLocked',
|
||||
'amazonRatingLocked', 'amazonReviewCountLocked',
|
||||
'goodreadsRatingLocked', 'goodreadsReviewCountLocked',
|
||||
'hardcoverRatingLocked', 'hardcoverReviewCountLocked'
|
||||
];
|
||||
|
||||
fieldLabels: Record<string, string> = {
|
||||
titleLocked: 'Title',
|
||||
subtitleLocked: 'Subtitle',
|
||||
publisherLocked: 'Publisher',
|
||||
publishedDateLocked: 'Published Date',
|
||||
descriptionLocked: 'Description',
|
||||
isbn13Locked: 'ISBN-13',
|
||||
isbn10Locked: 'ISBN-10',
|
||||
asinLocked: 'ASIN',
|
||||
pageCountLocked: 'Page Count',
|
||||
thumbnailLocked: 'Thumbnail',
|
||||
languageLocked: 'Language',
|
||||
ratingLocked: 'Rating',
|
||||
reviewCountLocked: 'Review Count',
|
||||
coverLocked: 'Cover',
|
||||
seriesNameLocked: 'Series Name',
|
||||
seriesNumberLocked: 'Series Number',
|
||||
seriesTotalLocked: 'Series Total',
|
||||
authorsLocked: 'Authors',
|
||||
categoriesLocked: 'Categories',
|
||||
amazonRatingLocked: 'Amazon Rating',
|
||||
amazonReviewCountLocked: 'Amazon Rating Count',
|
||||
goodreadsRatingLocked: 'Goodreads Rating',
|
||||
goodreadsReviewCountLocked: 'Goodreads Rating Count',
|
||||
hardcoverRatingLocked: 'Hardcover Rating',
|
||||
hardcoverReviewCountLocked: 'Hardcover Rating Count'
|
||||
};
|
||||
|
||||
isSaving = false;
|
||||
|
||||
ngOnInit(): void {
|
||||
this.lockableFields.forEach(field => this.fieldLocks[field] = undefined);
|
||||
}
|
||||
|
||||
toggleLockAll(action: 'LOCK' | 'UNLOCK'): void {
|
||||
const lockState = action === 'LOCK' ? true : false;
|
||||
this.lockableFields.forEach(field => {
|
||||
this.fieldLocks[field] = lockState;
|
||||
});
|
||||
}
|
||||
|
||||
getLockLabel(field: string): string {
|
||||
const state = this.fieldLocks[field];
|
||||
if (state === undefined) return 'Unselected';
|
||||
return state ? 'Locked' : 'Unlocked';
|
||||
}
|
||||
|
||||
getLockIcon(field: string): string {
|
||||
const state = this.fieldLocks[field];
|
||||
return state === undefined ? '' : state ? 'pi pi-lock' : 'pi pi-lock-open';
|
||||
}
|
||||
|
||||
resetFieldLocks(): void {
|
||||
this.lockableFields.forEach(field => {
|
||||
this.fieldLocks[field] = undefined;
|
||||
});
|
||||
}
|
||||
|
||||
cycleLockState(field: string): void {
|
||||
const current = this.fieldLocks[field];
|
||||
if (current === undefined) {
|
||||
this.fieldLocks[field] = true;
|
||||
} else if (current) {
|
||||
this.fieldLocks[field] = false;
|
||||
} else {
|
||||
this.fieldLocks[field] = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
applyFieldLocks(): void {
|
||||
const fieldActions: Record<string, 'LOCK' | 'UNLOCK'> = {};
|
||||
for (const [field, locked] of Object.entries(this.fieldLocks)) {
|
||||
if (locked !== undefined) {
|
||||
fieldActions[field] = locked ? 'LOCK' : 'UNLOCK';
|
||||
}
|
||||
}
|
||||
|
||||
this.isSaving = true;
|
||||
this.bookService.toggleFieldLocks(this.bookIds, fieldActions).subscribe({
|
||||
next: () => {
|
||||
const isLock = action === 'LOCK';
|
||||
this.isSaving = false;
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: `Metadata ${isLock ? 'Locked' : 'Unlocked'}`,
|
||||
detail: `All selected books have been ${isLock ? 'locked' : 'unlocked'} successfully.`,
|
||||
summary: 'Field Locks Updated',
|
||||
detail: 'Selected metadata fields have been updated successfully.'
|
||||
});
|
||||
this.dialogRef.close(action.toLowerCase());
|
||||
this.dialogRef.close('fields-updated');
|
||||
},
|
||||
error: () => {
|
||||
this.isSaving = false;
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: `Failed to ${action === 'LOCK' ? 'Lock' : 'Unlock'}`,
|
||||
detail: `An error occurred while ${action === 'LOCK' ? 'locking' : 'unlocking'} metadata.`,
|
||||
summary: 'Failed to Update Field Locks',
|
||||
detail: 'An error occurred while updating field lock statuses.'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
-2
@@ -75,8 +75,6 @@ export class MetadataSearcherComponent implements OnInit, OnDestroy {
|
||||
distinctUntilChanged(([prevBook], [currBook]) => prevBook?.id === currBook?.id)
|
||||
).subscribe(([book, settings]) => {
|
||||
|
||||
console.log(settings!.metadataProviderSettings)
|
||||
|
||||
this.providers = Object.entries(settings!.metadataProviderSettings)
|
||||
.filter(([key, value]) => value.enabled)
|
||||
.map(([key]) => key.charAt(0).toUpperCase() + key.slice(1));
|
||||
|
||||
@@ -347,7 +347,7 @@ export class BookService {
|
||||
|
||||
handleRemovedBookIds(removedBookIds: number[]): void {
|
||||
const currentState = this.bookStateSubject.value;
|
||||
const filteredBooks = (currentState.books || []).filter(book => !removedBookIds.includes(book.id)); // Check using includes() method
|
||||
const filteredBooks = (currentState.books || []).filter(book => !removedBookIds.includes(book.id));
|
||||
this.bookStateSubject.next({...currentState, books: filteredBooks});
|
||||
}
|
||||
|
||||
@@ -366,4 +366,30 @@ export class BookService {
|
||||
});
|
||||
this.bookStateSubject.next({...currentState, books: updatedBooks})
|
||||
}
|
||||
|
||||
toggleFieldLocks(bookIds: Set<number>, fieldActions: Record<string, 'LOCK' | 'UNLOCK'>): Observable<void> {
|
||||
const requestBody = {
|
||||
bookIds: Array.from(bookIds),
|
||||
fieldActions: fieldActions
|
||||
};
|
||||
return this.http.put<BookMetadata[]>(`${this.url}/metadata/toggle-field-locks`, requestBody).pipe(
|
||||
tap((updatedMetadataList) => {
|
||||
const currentState = this.bookStateSubject.value;
|
||||
const updatedBooks = (currentState.books || []).map(book => {
|
||||
const updatedMetadata = updatedMetadataList.find(meta => meta.bookId === book.id);
|
||||
return updatedMetadata ? { ...book, metadata: updatedMetadata } : book;
|
||||
});
|
||||
this.bookStateSubject.next({ ...currentState, books: updatedBooks });
|
||||
}),
|
||||
map(() => void 0),
|
||||
catchError((error) => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Field Lock Update Failed',
|
||||
detail: 'Failed to update metadata field locks. Please try again.',
|
||||
});
|
||||
throw error;
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user