Fix authors and categories not getting set properly in the viewer.

This commit is contained in:
aditya.chandel
2025-01-16 20:50:00 -07:00
parent 2c9b943538
commit 6ee5236920
8 changed files with 47 additions and 53 deletions
@@ -34,14 +34,8 @@ public class MetadataController {
} }
@PutMapping("/{bookId}") @PutMapping("/{bookId}")
public ResponseEntity<BookMetadata> setBookMetadata(@RequestBody FetchedBookMetadata setMetadataRequest, @PathVariable long bookId) { public ResponseEntity<BookMetadata> updateMetadata(@RequestBody FetchedBookMetadata setMetadataRequest, @PathVariable long bookId) {
BookMetadata bookMetadata = bookMetadataMapper.toBookMetadata(bookMetadataUpdater.setBookMetadata(bookId, setMetadataRequest, true), false); BookMetadata bookMetadata = bookMetadataMapper.toBookMetadata(bookMetadataUpdater.setBookMetadata(bookId, setMetadataRequest, true), true);
return ResponseEntity.ok(bookMetadata);
}
@PutMapping("/{bookId}/source/{source}")
public ResponseEntity<BookMetadata> setBookMetadata(@RequestBody FetchedBookMetadata setMetadataRequest, @PathVariable long bookId, @PathVariable MetadataProvider source) {
BookMetadata bookMetadata = bookMetadataMapper.toBookMetadata(bookMetadataUpdater.setBookMetadata(bookId, setMetadataRequest, true), false);
return ResponseEntity.ok(bookMetadata); return ResponseEntity.ok(bookMetadata);
} }
@@ -8,7 +8,7 @@ import {Book, BookMetadata} from '../../book/model/book.model';
export class BookMetadataCenterService { export class BookMetadataCenterService {
private bookMetadataSubject = new BehaviorSubject<BookMetadata | null>(null); private bookMetadataSubject = new BehaviorSubject<BookMetadata | null>(null);
bookMetadata$ = this.bookMetadataSubject.asObservable(); currentMetadata$ = this.bookMetadataSubject.asObservable();
private dialogCloseSubject = new BehaviorSubject<boolean | null>(null); private dialogCloseSubject = new BehaviorSubject<boolean | null>(null);
dialogClose$ = this.dialogCloseSubject.asObservable(); dialogClose$ = this.dialogCloseSubject.asObservable();
@@ -37,7 +37,7 @@ export class MetadataEditorComponent implements OnInit {
private messageService = inject(MessageService); private messageService = inject(MessageService);
private metadataService = inject(MetadataService); private metadataService = inject(MetadataService);
bookMetadata$: Observable<BookMetadata | null> = this.metadataCenterService.bookMetadata$; bookMetadata$: Observable<BookMetadata | null> = this.metadataCenterService.currentMetadata$;
bookMetadataForm: FormGroup; bookMetadataForm: FormGroup;
currentBookId!: number; currentBookId!: number;
@@ -1,4 +1,4 @@
<form [formGroup]="bookMetadataForm" (ngSubmit)="onSave()" class="h-full flex pt-1 flex-col"> <form [formGroup]="metadataForm" (ngSubmit)="onSave()" class="h-full flex pt-1 flex-col">
<div class="overflow-y-scroll flex-1"> <div class="overflow-y-scroll flex-1">
<div class="flex justify-center items-center" [ngStyle]="{'padding-bottom': '0.75rem', 'padding-left': '4.95rem'}"> <div class="flex justify-center items-center" [ngStyle]="{'padding-bottom': '0.75rem', 'padding-left': '4.95rem'}">
@@ -34,7 +34,7 @@ export class MetadataPickerComponent implements OnInit {
@Input() fetchedMetadata!: FetchedMetadata; @Input() fetchedMetadata!: FetchedMetadata;
@Output() goBack = new EventEmitter<boolean>(); @Output() goBack = new EventEmitter<boolean>();
bookMetadataForm: FormGroup; metadataForm: FormGroup;
currentBookId!: number; currentBookId!: number;
updateThumbnailUrl: boolean = false; updateThumbnailUrl: boolean = false;
thumbnailSaved: boolean = false; thumbnailSaved: boolean = false;
@@ -46,10 +46,10 @@ export class MetadataPickerComponent implements OnInit {
private messageService = inject(MessageService); private messageService = inject(MessageService);
private metadataService = inject(MetadataService); private metadataService = inject(MetadataService);
metadata$: Observable<BookMetadata | null> = this.metadataCenterService.bookMetadata$; currentMetadata$: Observable<BookMetadata | null> = this.metadataCenterService.currentMetadata$;
constructor() { constructor() {
this.bookMetadataForm = new FormGroup({ this.metadataForm = new FormGroup({
title: new FormControl(''), title: new FormControl(''),
subtitle: new FormControl(''), subtitle: new FormControl(''),
authors: new FormControl(''), authors: new FormControl(''),
@@ -67,10 +67,10 @@ export class MetadataPickerComponent implements OnInit {
} }
ngOnInit(): void { ngOnInit(): void {
this.metadata$.subscribe((metadata) => { this.currentMetadata$.subscribe((metadata) => {
if (metadata) { if (metadata) {
this.currentBookId = metadata.bookId; this.currentBookId = metadata.bookId;
this.bookMetadataForm.setValue({ this.metadataForm.setValue({
title: metadata.title, title: metadata.title,
subtitle: metadata.subtitle, subtitle: metadata.subtitle,
authors: metadata.authors.map((author) => author.name).join(', '), authors: metadata.authors.map((author) => author.name).join(', '),
@@ -104,24 +104,24 @@ export class MetadataPickerComponent implements OnInit {
onSave(): void { onSave(): void {
const updatedBookMetadata: BookMetadata = { const updatedBookMetadata: BookMetadata = {
bookId: this.currentBookId, bookId: this.currentBookId,
title: this.bookMetadataForm.get('title')?.value || this.copiedFields['title'] ? this.getValueOrCopied('title') : '', title: this.metadataForm.get('title')?.value || this.copiedFields['title'] ? this.getValueOrCopied('title') : '',
subtitle: this.bookMetadataForm.get('subtitle')?.value || this.copiedFields['subtitle'] ? this.getValueOrCopied('subtitle') : '', subtitle: this.metadataForm.get('subtitle')?.value || this.copiedFields['subtitle'] ? this.getValueOrCopied('subtitle') : '',
authors: this.bookMetadataForm.get('authors')?.value || this.copiedFields['authors'] ? this.getArrayFromFormField('authors', this.fetchedMetadata.authors) : [], authors: this.metadataForm.get('authors')?.value || this.copiedFields['authors'] ? this.getArrayFromFormField('authors', this.fetchedMetadata.authors) : [],
categories: this.bookMetadataForm.get('categories')?.value || this.copiedFields['categories'] ? this.getArrayFromFormField('categories', this.fetchedMetadata.categories) : [], categories: this.metadataForm.get('categories')?.value || this.copiedFields['categories'] ? this.getArrayFromFormField('categories', this.fetchedMetadata.categories) : [],
publisher: this.bookMetadataForm.get('publisher')?.value || this.copiedFields['publisher'] ? this.getValueOrCopied('publisher') : '', publisher: this.metadataForm.get('publisher')?.value || this.copiedFields['publisher'] ? this.getValueOrCopied('publisher') : '',
publishedDate: this.bookMetadataForm.get('publishedDate')?.value || this.copiedFields['publishedDate'] ? this.getValueOrCopied('publishedDate') : '', publishedDate: this.metadataForm.get('publishedDate')?.value || this.copiedFields['publishedDate'] ? this.getValueOrCopied('publishedDate') : '',
isbn10: this.bookMetadataForm.get('isbn10')?.value || this.copiedFields['isbn10'] ? this.getValueOrCopied('isbn10') : '', isbn10: this.metadataForm.get('isbn10')?.value || this.copiedFields['isbn10'] ? this.getValueOrCopied('isbn10') : '',
isbn13: this.bookMetadataForm.get('isbn13')?.value || this.copiedFields['isbn13'] ? this.getValueOrCopied('isbn13') : '', isbn13: this.metadataForm.get('isbn13')?.value || this.copiedFields['isbn13'] ? this.getValueOrCopied('isbn13') : '',
description: this.bookMetadataForm.get('description')?.value || this.copiedFields['description'] ? this.getValueOrCopied('description') : '', description: this.metadataForm.get('description')?.value || this.copiedFields['description'] ? this.getValueOrCopied('description') : '',
pageCount: this.bookMetadataForm.get('pageCount')?.value || this.copiedFields['pageCount'] ? this.getPageCountOrCopied() : null, pageCount: this.metadataForm.get('pageCount')?.value || this.copiedFields['pageCount'] ? this.getPageCountOrCopied() : null,
language: this.bookMetadataForm.get('language')?.value || this.copiedFields['language'] ? this.getValueOrCopied('language') : '', language: this.metadataForm.get('language')?.value || this.copiedFields['language'] ? this.getValueOrCopied('language') : '',
rating: this.bookMetadataForm.get('rating')?.value || this.copiedFields['rating'] ? this.getNumberOrCopied('rating') : null, rating: this.metadataForm.get('rating')?.value || this.copiedFields['rating'] ? this.getNumberOrCopied('rating') : null,
reviewCount: this.bookMetadataForm.get('reviewCount')?.value || this.copiedFields['reviewCount'] ? this.getNumberOrCopied('reviewCount') : null, reviewCount: this.metadataForm.get('reviewCount')?.value || this.copiedFields['reviewCount'] ? this.getNumberOrCopied('reviewCount') : null,
thumbnailUrl: this.updateThumbnailUrl ? this.fetchedMetadata.thumbnailUrl : '', thumbnailUrl: this.updateThumbnailUrl ? this.fetchedMetadata.thumbnailUrl : '',
}; };
this.metadataService.updateBookMetadata(this.currentBookId, updatedBookMetadata).subscribe({ this.metadataService.updateBookMetadata(this.currentBookId, updatedBookMetadata).subscribe({
next: () => { next: (bookMetadata) => {
Object.keys(this.copiedFields).forEach((field) => { Object.keys(this.copiedFields).forEach((field) => {
if (this.copiedFields[field]) { if (this.copiedFields[field]) {
this.savedFields[field] = true; this.savedFields[field] = true;
@@ -131,7 +131,7 @@ export class MetadataPickerComponent implements OnInit {
this.thumbnailSaved = true; this.thumbnailSaved = true;
} }
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Book metadata updated'}); this.messageService.add({severity: 'info', summary: 'Success', detail: 'Book metadata updated'});
this.metadataCenterService.emit(updatedBookMetadata); this.metadataCenterService.emit(bookMetadata);
}, },
error: () => { error: () => {
this.messageService.add({severity: 'error', summary: 'Error', detail: 'Failed to update book metadata'}); this.messageService.add({severity: 'error', summary: 'Error', detail: 'Failed to update book metadata'});
@@ -141,14 +141,14 @@ export class MetadataPickerComponent implements OnInit {
copyMissing() { copyMissing() {
Object.keys(this.fetchedMetadata).forEach((field) => { Object.keys(this.fetchedMetadata).forEach((field) => {
if (!this.bookMetadataForm.get(field)?.value && this.fetchedMetadata[field]) { if (!this.metadataForm.get(field)?.value && this.fetchedMetadata[field]) {
this.copyFetchedToCurrent(field); this.copyFetchedToCurrent(field);
} }
}); });
} }
private getNumberOrCopied(field: string): number | null { private getNumberOrCopied(field: string): number | null {
const formValue = this.bookMetadataForm.get(field)?.value; const formValue = this.metadataForm.get(field)?.value;
if (formValue === '' || formValue === null || isNaN(formValue)) { if (formValue === '' || formValue === null || isNaN(formValue)) {
this.copiedFields[field] = true; this.copiedFields[field] = true;
return this.fetchedMetadata[field] || null; return this.fetchedMetadata[field] || null;
@@ -157,7 +157,7 @@ export class MetadataPickerComponent implements OnInit {
} }
private getPageCountOrCopied(): number | null { private getPageCountOrCopied(): number | null {
const formValue = this.bookMetadataForm.get('pageCount')?.value; const formValue = this.metadataForm.get('pageCount')?.value;
if (formValue === '' || formValue === null || isNaN(formValue)) { if (formValue === '' || formValue === null || isNaN(formValue)) {
this.copiedFields['pageCount'] = true; this.copiedFields['pageCount'] = true;
return this.fetchedMetadata.pageCount || null; return this.fetchedMetadata.pageCount || null;
@@ -166,7 +166,7 @@ export class MetadataPickerComponent implements OnInit {
} }
private getValueOrCopied(field: string): string { private getValueOrCopied(field: string): string {
const formValue = this.bookMetadataForm.get(field)?.value; const formValue = this.metadataForm.get(field)?.value;
if (!formValue || formValue === '') { if (!formValue || formValue === '') {
this.copiedFields[field] = true; this.copiedFields[field] = true;
return this.fetchedMetadata[field] || ''; return this.fetchedMetadata[field] || '';
@@ -175,7 +175,7 @@ export class MetadataPickerComponent implements OnInit {
} }
getArrayFromFormField(field: string, fallbackValue: any): any[] { getArrayFromFormField(field: string, fallbackValue: any): any[] {
const fieldValue = this.bookMetadataForm.get(field)?.value; const fieldValue = this.metadataForm.get(field)?.value;
if (!fieldValue) { if (!fieldValue) {
return fallbackValue ? (Array.isArray(fallbackValue) ? fallbackValue : [fallbackValue]) : []; return fallbackValue ? (Array.isArray(fallbackValue) ? fallbackValue : [fallbackValue]) : [];
} }
@@ -192,7 +192,7 @@ export class MetadataPickerComponent implements OnInit {
copyFetchedToCurrent(field: string): void { copyFetchedToCurrent(field: string): void {
const value = this.fetchedMetadata[field]; const value = this.fetchedMetadata[field];
if (value) { if (value) {
this.bookMetadataForm.get(field)?.setValue(value); this.metadataForm.get(field)?.setValue(value);
this.copiedFields[field] = true; this.copiedFields[field] = true;
this.highlightCopiedInput(field); this.highlightCopiedInput(field);
} }
@@ -54,7 +54,7 @@ export class MetadataSearcherComponent implements OnInit {
} }
ngOnInit() { ngOnInit() {
this.metadataCenterService.bookMetadata$.subscribe((metadata => { this.metadataCenterService.currentMetadata$.subscribe((metadata => {
if (metadata) { if (metadata) {
this.bookId = metadata.bookId; this.bookId = metadata.bookId;
this.form.setValue(({ this.form.setValue(({
@@ -62,9 +62,9 @@ export class MetadataSearcherComponent implements OnInit {
isbn: metadata.isbn10, isbn: metadata.isbn10,
title: metadata.title, title: metadata.title,
author: metadata.authors?.length! > 0 ? metadata.authors[0].name : '' author: metadata.authors?.length! > 0 ? metadata.authors[0].name : ''
})) }));
} }
})) }));
} }
get isSearchEnabled(): boolean { get isSearchEnabled(): boolean {
@@ -94,9 +94,9 @@ export class MetadataSearcherComponent implements OnInit {
.subscribe({ .subscribe({
next: (fetchedMetadata) => { next: (fetchedMetadata) => {
this.loading = false; this.loading = false;
this.allFetchedMetadata = fetchedMetadata.map((book) => ({ this.allFetchedMetadata = fetchedMetadata.map((fetchedMetadata) => ({
...book, ...fetchedMetadata,
thumbnailUrl: book.thumbnailUrl thumbnailUrl: fetchedMetadata.thumbnailUrl
})); }));
}, },
error: () => { error: () => {
@@ -37,27 +37,27 @@
</div> </div>
<div class="flex flex-wrap gap-2"> <div class="flex flex-wrap gap-2">
<p-tag *ngFor="let category of metadata.categories" value="{{category}}"></p-tag> <p-tag *ngFor="let category of metadata.categories" [value]="category.name"></p-tag>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
<!--<p-divider></p-divider>--> <p-divider></p-divider>
<div *ngIf="metadata$ | async as metadata" class="book-description space-y-4 overflow-y-auto flex-1"> <div *ngIf="metadata$ | async as metadata" class="book-description space-y-4 overflow-y-auto flex-1">
<!--<p class="pt-2 px-2" [innerHTML]="bookMetadata.description || 'No description available.'"></p>--> <p class="pt-2 px-2" [innerHTML]="metadata.description || 'No description available.'"></p>
<div class="pt-6"> <!--<div class="pt-6">
<p-editor [(ngModel)]="metadata.description" [style]="{ height: '350px' }" [readonly]="true"> <p-editor [(ngModel)]="metadata.description" [style]="{ height: '350px' }" [readonly]="true">
<ng-template #header> <ng-template #header>
</ng-template> </ng-template>
</p-editor> </p-editor>
</div> </div>-->
</div> </div>
<div class="flex flex-col gap-x-4 mt-auto"> <div class="flex flex-col gap-x-4 mt-auto">
<!--<p-divider></p-divider>--> <p-divider></p-divider>
<div class="flex gap-x-4 justify-end"> <div class="flex gap-x-4 justify-end">
<p-button label="Close" icon="pi pi-times" severity="secondary" (onClick)="closeDialog()"></p-button> <p-button label="Close" icon="pi pi-times" severity="secondary" (onClick)="closeDialog()"></p-button>
</div> </div>
@@ -7,16 +7,16 @@ import {BookMetadataCenterService} from '../book-metadata-center.service';
import {Rating} from 'primeng/rating'; import {Rating} from 'primeng/rating';
import {FormsModule} from '@angular/forms'; import {FormsModule} from '@angular/forms';
import {Tag} from 'primeng/tag'; import {Tag} from 'primeng/tag';
import {Editor} from 'primeng/editor';
import {Author, Book} from '../../../book/model/book.model'; import {Author, Book} from '../../../book/model/book.model';
import {FileService} from '../../../book/service/file.service'; import {FileService} from '../../../book/service/file.service';
import {Divider} from 'primeng/divider';
@Component({ @Component({
selector: 'app-metadata-viewer', selector: 'app-metadata-viewer',
standalone: true, standalone: true,
templateUrl: './metadata-viewer.component.html', templateUrl: './metadata-viewer.component.html',
styleUrl: './metadata-viewer.component.scss', styleUrl: './metadata-viewer.component.scss',
imports: [Button, NgForOf, NgIf, AsyncPipe, Rating, FormsModule, Tag, Editor] imports: [Button, NgForOf, NgIf, AsyncPipe, Rating, FormsModule, Tag, Divider]
}) })
export class MetadataViewerComponent { export class MetadataViewerComponent {
@@ -24,7 +24,7 @@ export class MetadataViewerComponent {
private fileService = inject(FileService); private fileService = inject(FileService);
private metadataCenterService = inject(BookMetadataCenterService); private metadataCenterService = inject(BookMetadataCenterService);
metadata$: Observable<Book['metadata'] | null> = this.metadataCenterService.bookMetadata$; metadata$: Observable<Book['metadata'] | null> = this.metadataCenterService.currentMetadata$;
coverImageSrc(bookId: number | undefined): string { coverImageSrc(bookId: number | undefined): string {
return bookId ? this.bookService.getBookCoverUrl(bookId) : 'assets/book-cover-metadata.png'; return bookId ? this.bookService.getBookCoverUrl(bookId) : 'assets/book-cover-metadata.png';