WIP: New metadata engine

This commit is contained in:
aditya.chandel
2025-01-01 01:12:27 -07:00
parent 1d06f83de8
commit ea2ac58f03
30 changed files with 1145 additions and 83 deletions
+1
View File
@@ -1,3 +1,4 @@
<p-confirmDialog />
<p-toast></p-toast>
<app-loading-overlay></app-loading-overlay>
<router-outlet></router-outlet>
+7 -1
View File
@@ -41,6 +41,9 @@ import {ProgressSpinnerModule} from 'primeng/progressspinner';
import {provideAnimationsAsync} from '@angular/platform-browser/animations/async';
import {providePrimeNG} from 'primeng/config';
import Aura from '@primeng/themes/aura';
import { MetadataSearcherComponent } from './metadata-searcher/metadata-searcher.component';
import {IftaLabel} from 'primeng/iftalabel';
import { LoadingOverlayComponent } from './loading-overlay/loading-overlay.component';
@NgModule({
declarations: [
@@ -48,7 +51,8 @@ import Aura from '@primeng/themes/aura';
DirectoryPickerComponent,
LibraryCreatorComponent,
ShelfAssignerComponent,
BookBrowserComponent
BookBrowserComponent,
MetadataSearcherComponent
],
imports: [
BrowserModule,
@@ -78,6 +82,8 @@ import Aura from '@primeng/themes/aura';
MenuModule,
IconPickerComponent,
ProgressSpinnerModule,
IftaLabel,
LoadingOverlayComponent,
],
providers: [
DialogService,
@@ -74,7 +74,9 @@ export class BookCardComponent implements OnInit {
{
label: 'View metadata',
icon: 'pi pi-info-circle',
command: () => this.openBookInfo(this.book),
command: () => {
this.openBookInfo(this.book)
},
},
],
},
@@ -20,7 +20,7 @@
<div *ngFor="let metadata of bookMetadataList" class="book-metadata">
<div class="metadata-header">
<div class="book-image-container">
<img class="book-image" [src]="metadata.thumbnail || 'assets/book-cover-metadata.png'" alt="Book Cover"/>
<!--<img class="book-image" [src]="metadata.thumbnail || 'assets/book-cover-metadata.png'" alt="Book Cover"/>-->
<p-button class="select-button" (click)="selectMetadata(metadata)">Select</p-button>
</div>
<div>
@@ -37,7 +37,7 @@
{{ book?.metadata?.title }}<span *ngIf="book?.metadata?.subtitle">: {{ book?.metadata?.subtitle }}</span>
</h1>
<div class="button-group">
<p-button [rounded]="true" [raised]="true" [outlined]="true" (onClick)="openEditDialog(book?.id, book?.libraryId)" icon="pi pi-pencil"></p-button>
<p-button [rounded]="true" [raised]="true" [outlined]="true" (onClick)="openEditDialog(book?.id!, book?.libraryId!)" icon="pi pi-pencil"></p-button>
<p-button [rounded]="true" [raised]="true" [outlined]="true" (onClick)="readBook(book!)" icon="pi pi-eye"></p-button>
<p-button [rounded]="true" [raised]="true" [outlined]="true" icon="pi pi-download"></p-button>
</div>
@@ -9,6 +9,9 @@ import {Subscription} from 'rxjs';
import {TagModule} from 'primeng/tag';
import {BookService} from '../../service/book.service';
import {LibraryService} from '../../service/library.service';
import {MetadataSearcherComponent} from '../../../metadata-searcher/metadata-searcher.component';
import {ProgressSpinner} from 'primeng/progressspinner';
import {LoadingService} from '../../../loading.service';
@Component({
selector: 'app-book-metadata',
@@ -27,18 +30,18 @@ export class BookMetadataComponent implements OnInit, OnDestroy {
previousBookId: number | null = null;
private routeSubscription!: Subscription;
private dialogRef: DynamicDialogRef | undefined;
private dialogRef!: DynamicDialogRef;
private dialogSubscription?: Subscription;
constructor(
private bookService: BookService, private activatedRoute: ActivatedRoute,
private dialogService: DialogService, private router: Router,
private libraryService: LibraryService) {
private libraryService: LibraryService, private loadingService: LoadingService,) {
}
ngOnInit(): void {
this.routeSubscription = this.activatedRoute.paramMap.subscribe((paramMap) => {
const bookId = +paramMap.get('bookId')!;
const bookId = +paramMap.get('bookId')!;
const libraryId = +paramMap.get('libraryId')!;
if (bookId && libraryId) {
this.loadBookWithNeighbors(bookId, libraryId);
@@ -75,7 +78,47 @@ export class BookMetadataComponent implements OnInit, OnDestroy {
return 'No authors available';
}
openEditDialog(bookId: number | undefined, libraryId: number | undefined) {
openEditDialog(bookId: number, libraryId: number) {
this.loadingService.show();
this.bookService.getFetchBookMetadata(bookId).subscribe({
next: (fetchedMetadata) => {
this.loadingService.hide();
this.dialogRef = this.dialogService.open(MetadataSearcherComponent, {
header: 'Update Book Metadata',
modal: true,
closable: true,
width: '1350px',
height: '1000px',
contentStyle: {
'overflow-y': 'auto',
'max-height': 'calc(100vh - 150px)',
'padding': '1.25rem 1.25rem 0',
},
data: {
currentMetadata: this.book?.metadata,
fetchedMetadata: fetchedMetadata,
book: this.book
}
});
if (this.dialogRef) {
this.dialogSubscription = this.dialogRef.onClose.subscribe(() => {
if (this.book?.id && this.book?.libraryId) {
this.loadBookWithNeighbors(this.book.id, this.book.libraryId);
}
});
} else {
console.error('DialogRef is undefined or null');
}
},
error: (error) => {
this.loadingService.hide();
console.error('Error fetching metadata:', error);
}
});
}
/*openEditDialog1(bookId: number | undefined, libraryId: number | undefined) {
this.dialogRef = this.dialogService.open(BookMetadataDialogComponent, {
header: 'Metadata: Google Books',
modal: true,
@@ -94,7 +137,7 @@ export class BookMetadataComponent implements OnInit, OnDestroy {
this.loadBookWithNeighbors(this.book.id, this.book.libraryId);
}
});
}
}*/
coverImageSrc(bookId: number | undefined): string {
if (bookId === null) {
+33 -1
View File
@@ -10,7 +10,6 @@ export interface Book {
}
export interface BookMetadata {
thumbnail: string;
title: string;
subtitle?: string;
authors: Author[];
@@ -22,6 +21,39 @@ export interface BookMetadata {
pageCount: number;
language: string;
googleBookId: string;
[key: string]: any;
}
export interface FetchedMetadata {
title: string;
subtitle?: string;
authors: Author[];
categories: Category[];
publisher: string;
publishedDate: string;
isbn10: string;
description: string;
pageCount: number;
language: string;
googleBookId: string;
thumbnail: string;
[key: string]: any;
}
export interface UpdateMedata {
title?: string;
subtitle?: string;
authors?: Author[];
categories?: Category[];
publisher?: string;
publishedDate?: string;
isbn10?: string;
description?: string;
pageCount?: number;
language?: string;
googleBookId?: string;
thumbnail?: string;
[key: string]: any;
}
export interface Author {
@@ -2,7 +2,7 @@ import {Injectable} from '@angular/core';
import {BehaviorSubject, Observable, of} from 'rxjs';
import {HttpClient} from '@angular/common/http';
import {catchError, map, switchMap, tap} from 'rxjs/operators';
import {Book, BookMetadata, BookSetting} from '../model/book.model';
import {Book, BookMetadata, BookSetting, FetchedMetadata, UpdateMedata} from '../model/book.model';
import {BookState} from '../model/state/book-state.model';
@Injectable({
@@ -186,4 +186,29 @@ export class BookService {
const filteredBooks = (currentState.books || []).filter(book => !removedBookIds.has(book.id));
this.bookStateSubject.next({...currentState, books: filteredBooks});
}
getFetchBookMetadata(bookId: number): Observable<FetchedMetadata> {
return this.http.post<FetchedMetadata>(`${this.url}/${bookId}/query-for-books`, null);
}
updateMetadata(bookId: number, updateMedata: UpdateMedata): Observable<BookMetadata> {
return this.http.put<BookMetadata>(`${this.url}/${bookId}/metadata`, updateMedata).pipe(
map((updatedMetadata) => {
const currentState = this.bookStateSubject.value;
const updatedBooks = currentState.books?.map((book) => {
if (book.id === bookId) {
return { ...book, metadata: updatedMetadata };
}
return book;
}) || [];
this.bookStateSubject.next({ ...currentState, books: updatedBooks });
return updatedMetadata;
}),
catchError((error) => {
const currentState = this.bookStateSubject.value;
this.bookStateSubject.next({ ...currentState, error: error.message });
throw error;
})
);
}
}
@@ -4,7 +4,7 @@ import { HttpClient } from '@angular/common/http';
import { catchError, map } from 'rxjs/operators';
import { Library } from '../model/library.model';
import { BookService } from './book.service';
import { BookWithNeighborsDTO } from '../model/book.model';
import {BookMetadata, BookWithNeighborsDTO} from '../model/book.model';
import { SortOption } from '../model/sort.model';
import { LibraryState } from '../model/state/library-state.model';
@@ -113,4 +113,5 @@ export class LibraryService {
map(state => (state.books || []).filter(book => book.libraryId === libraryId).length)
);
}
}
@@ -0,0 +1,3 @@
<div *ngIf="loading" class="loading-overlay">
<p-progressSpinner></p-progressSpinner>
</div>
@@ -0,0 +1,12 @@
.loading-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5); /* Dimmed background */
display: flex;
justify-content: center;
align-items: center;
z-index: 9999; /* Ensure it stays on top of all content */
}
@@ -0,0 +1,33 @@
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs';
import {LoadingService} from '../loading.service';
import {ProgressSpinner} from 'primeng/progressspinner';
import {NgIf} from '@angular/common';
@Component({
selector: 'app-loading-overlay',
templateUrl: './loading-overlay.component.html',
imports: [
ProgressSpinner,
NgIf
],
styleUrls: ['./loading-overlay.component.scss']
})
export class LoadingOverlayComponent implements OnInit, OnDestroy {
loading: boolean = false;
private loadingSubscription: Subscription | undefined;
constructor(private loadingService: LoadingService) {}
ngOnInit(): void {
this.loadingSubscription = this.loadingService.loading$.subscribe(
(loading) => {
this.loading = loading;
}
);
}
ngOnDestroy(): void {
this.loadingSubscription?.unsubscribe();
}
}
+19
View File
@@ -0,0 +1,19 @@
// loading.service.ts
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class LoadingService {
private loadingSubject = new BehaviorSubject<boolean>(false);
loading$ = this.loadingSubject.asObservable();
show(): void {
this.loadingSubject.next(true);
}
hide(): void {
this.loadingSubject.next(false);
}
}
@@ -0,0 +1,95 @@
<div class="form-row">
<label for="title" class="label">Title</label>
<div class="input-container">
<input pInputText id="title" [(ngModel)]="toUpdateMetadata.title" class="input"/>
<p-button icon="pi pi-arrow-left" [outlined]="true" class="arrow-button" (click)="copyFetchedToCurrent('title')"/>
<input pInputText id="titleFetched" [(ngModel)]="fetchedMetadata.title" class="input"/>
</div>
</div>
<div class="form-row">
<label for="authors" class="label">Authors</label>
<div class="input-container">
<input pInputText id="authors" [(ngModel)]="currentAuthorsString" class="input"/>
<p-button icon="pi pi-arrow-left" [outlined]="true" class="arrow-button" (click)="copyFetchedToCurrent('authors')"/>
<input pInputText id="authorsFetched" [(ngModel)]="fetchedAuthorsString" class="input"/>
</div>
</div>
<div class="form-row">
<label for="categories" class="label">Categories</label>
<div class="input-container">
<input pInputText id="categories" [(ngModel)]="currentCategoriesString" class="input"/>
<p-button icon="pi pi-arrow-left" [outlined]="true" class="arrow-button" (click)="copyFetchedToCurrent('categories')"/>
<input pInputText id="categoriesFetched" [(ngModel)]="fetchedCategoriesString" class="input"/>
</div>
</div>
<div class="form-row">
<label for="publisher" class="label">Publisher</label>
<div class="input-container">
<input pInputText id="publisher" [(ngModel)]="toUpdateMetadata.publisher" class="input"/>
<p-button icon="pi pi-arrow-left" [outlined]="true" class="arrow-button" (click)="copyFetchedToCurrent('publisher')"/>
<input pInputText id="publisherFetched" [(ngModel)]="fetchedMetadata.publisher" class="input"/>
</div>
</div>
<div class="form-row">
<label for="publishedDate" class="label">Published Date</label>
<div class="input-container">
<input pInputText id="publishedDate" [(ngModel)]="toUpdateMetadata.publishedDate" class="input"/>
<p-button icon="pi pi-arrow-left" [outlined]="true" class="arrow-button" (click)="copyFetchedToCurrent('publishedDate')"/>
<input pInputText id="publishedDateFetched" [(ngModel)]="fetchedMetadata.publishedDate" class="input"/>
</div>
</div>
<div class="form-row">
<label for="isbn10" class="label">ISBN-10</label>
<div class="input-container">
<input pInputText id="isbn10" [(ngModel)]="toUpdateMetadata.isbn10" class="input"/>
<p-button icon="pi pi-arrow-left" [outlined]="true" class="arrow-button" (click)="copyFetchedToCurrent('isbn10')"/>
<input pInputText id="isbn10Fetched" [(ngModel)]="fetchedMetadata.isbn10" class="input"/>
</div>
</div>
<div class="form-row">
<label for="language" class="label">Language</label>
<div class="input-container">
<input pInputText id="language" [(ngModel)]="toUpdateMetadata.language" class="input"/>
<p-button icon="pi pi-arrow-left" [outlined]="true" class="arrow-button" (click)="copyFetchedToCurrent('language')"/>
<input pInputText id="languageFetched" [(ngModel)]="fetchedMetadata.language" class="input"/>
</div>
</div>
<div class="form-row">
<label for="language" class="label">Page Count</label>
<div class="input-container">
<input pInputText id="pageCount" [(ngModel)]="toUpdateMetadata.pageCount" class="input"/>
<p-button icon="pi pi-arrow-left" [outlined]="true" class="arrow-button" (click)="copyFetchedToCurrent('pageCount')"/>
<input pInputText id="pageCountFetched" [(ngModel)]="fetchedMetadata.pageCount" class="input"/>
</div>
</div>
<div class="form-row">
<label for="description" class="label">Description</label>
<div class="input-container">
<textarea id="description" [(ngModel)]="toUpdateMetadata.description" class="input"></textarea>
<p-button icon="pi pi-arrow-left" [outlined]="true" class="arrow-button" (click)="copyFetchedToCurrent('description')"/>
<textarea id="descriptionFetched" [(ngModel)]="fetchedMetadata.description" class="input"></textarea>
</div>
</div>
<div class="form-row">
<label class="label"></label>
<div class="input-container">
<img *ngIf="!updateThumbnail" [src]="coverImageSrc(book)" alt="Book Thumbnail" class="thumbnail"/>
<img *ngIf="updateThumbnail" [src]="fetchedMetadata.thumbnail" alt="Fetched Thumbnail" class="thumbnail"/>
<p-button icon="pi pi-arrow-left" [outlined]="true" class="arrow-button" (click)="shouldUpdateThumbnail()"/>
<img [src]="fetchedMetadata.thumbnail" alt="Fetched Thumbnail" class="thumbnail"/>
</div>
</div>
<div class="button-container">
<p-button label="Cancel" icon="pi pi-times" severity="secondary" class="cancel-button" (onClick)="closeDialog()"></p-button>
<p-button label="Save" icon="pi pi-check" severity="primary" class="save-button" [disabled]="loading" (onClick)="saveMetadata()"></p-button>
</div>
@@ -0,0 +1,56 @@
.button-container {
display: flex;
justify-content: flex-end;
width: 100%;
padding: 1.25rem;
position: absolute;
bottom: 0;
left: 0;
right: 0;
z-index: 10;
}
.cancel-button {
margin-right: 1rem;
}
.form-row {
display: flex;
align-items: center;
margin-bottom: 1rem;
width: 100%;
justify-content: flex-start;
}
.label {
width: 8rem;
text-align: left;
}
.input-container {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
}
.input {
width: 50%;
padding: 0.5rem;
border-radius: 0.25rem;
margin: 0 0.5rem;
}
.thumbnail {
width: 15.625rem;
height: 21.875rem;
border-radius: 0.5rem;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
margin-left: 2.5rem;
margin-right: 2.5rem;
}
.arrow-button {
padding-left: 1rem;
padding-right: 1rem;
}
@@ -0,0 +1,153 @@
import {Component} from '@angular/core';
import {Book, FetchedMetadata, UpdateMedata} from '../book/model/book.model';
import {DynamicDialogConfig, DynamicDialogRef} from 'primeng/dynamicdialog';
import {BookService} from '../book/service/book.service';
import {MessageService} from 'primeng/api';
@Component({
selector: 'app-metadata-searcher',
standalone: false,
templateUrl: './metadata-searcher.component.html',
styleUrls: ['./metadata-searcher.component.scss']
})
export class MetadataSearcherComponent {
fetchedMetadata: FetchedMetadata;
toUpdateMetadata: UpdateMedata;
book: Book;
loading: boolean = false;
updateThumbnail: boolean = false;
constructor(public dynamicDialogConfig: DynamicDialogConfig, private dynamicDialogRef: DynamicDialogRef,
private bookService: BookService, private messageService: MessageService) {
this.fetchedMetadata = this.dynamicDialogConfig.data.fetchedMetadata;
this.toUpdateMetadata = JSON.parse(JSON.stringify(this.dynamicDialogConfig.data.currentMetadata));
this.book = this.dynamicDialogConfig.data.book;
}
copyFetchedToCurrent(field: keyof UpdateMedata) {
if (this.fetchedMetadata && this.toUpdateMetadata) {
this.toUpdateMetadata[field] = this.fetchedMetadata[field];
}
}
get currentAuthorsString(): string {
return this.toUpdateMetadata.authors ? this.toUpdateMetadata.authors.map(author => author.name).join(', ') : '';
}
set currentAuthorsString(value: string) {
this.toUpdateMetadata.authors = value.split(',').map(name => ({
id: this.toUpdateMetadata.authors?.find(author => author.name.trim() === name.trim())?.id || 0,
name: name.trim()
}));
}
get fetchedAuthorsString(): string {
return this.fetchedMetadata.authors ? this.fetchedMetadata.authors.map(author => author.name).join(', ') : '';
}
set fetchedAuthorsString(value: string) {
this.fetchedMetadata.authors = value.split(',').map(name => ({
id: this.fetchedMetadata.authors?.find(author => author.name.trim() === name.trim())?.id || 0,
name: name.trim()
}));
}
get currentCategoriesString(): string {
return this.toUpdateMetadata.categories ? this.toUpdateMetadata.categories.map(category => category.name).join(', ') : '';
}
set currentCategoriesString(value: string) {
this.toUpdateMetadata.categories = value.split(',').map(name => ({
id: this.toUpdateMetadata.categories?.find(category => category.name.trim() === name.trim())?.id || 0,
name: name.trim()
}));
}
get fetchedCategoriesString(): string {
return this.fetchedMetadata.categories ? this.fetchedMetadata.categories.map(category => category.name).join(', ') : '';
}
set fetchedCategoriesString(value: string) {
this.fetchedMetadata.categories = value.split(',').map(name => ({
id: this.fetchedMetadata.categories?.find(category => category.name.trim() === name.trim())?.id || 0,
name: name.trim()
}));
}
closeDialog() {
this.dynamicDialogRef.close();
}
coverImageSrc(book: Book): string {
return this.bookService.getBookCoverUrl(book.id);
}
saveMetadata() {
this.loading = true;
this.updateFields();
this.bookService.updateMetadata(this.book.id, this.toUpdateMetadata).subscribe({
next: () => {
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Book metadata updated'});
this.loading = false;
this.dynamicDialogRef.close();
},
error: (error) => {
this.loading = false;
this.messageService.add({severity: 'error', summary: 'Error', detail: 'Failed to update book metadata'});
}
});
}
updateFields() {
if (!this.toUpdateMetadata.title && this.fetchedMetadata.title) {
this.toUpdateMetadata.title = this.fetchedMetadata.title;
}
if (!this.toUpdateMetadata.subtitle && this.fetchedMetadata.subtitle) {
this.toUpdateMetadata.subtitle = this.fetchedMetadata.subtitle;
}
if (!this.toUpdateMetadata.publisher && this.fetchedMetadata.publisher) {
this.toUpdateMetadata.publisher = this.fetchedMetadata.publisher;
}
if (!this.toUpdateMetadata.publishedDate && this.fetchedMetadata.publishedDate) {
this.toUpdateMetadata.publishedDate = this.fetchedMetadata.publishedDate;
}
if (!this.toUpdateMetadata.description && this.fetchedMetadata.description) {
this.toUpdateMetadata.description = this.fetchedMetadata.description;
}
if (!this.toUpdateMetadata.isbn10 && this.fetchedMetadata.isbn10) {
this.toUpdateMetadata.isbn10 = this.fetchedMetadata.isbn10;
}
if (!this.toUpdateMetadata.language && this.fetchedMetadata.language) {
this.toUpdateMetadata.language = this.fetchedMetadata.language;
}
if (!this.toUpdateMetadata.pageCount && this.fetchedMetadata.pageCount) {
this.toUpdateMetadata.pageCount = this.fetchedMetadata.pageCount;
}
if (!this.toUpdateMetadata.authors || this.toUpdateMetadata.authors.length === 0) {
this.toUpdateMetadata.authors = this.fetchedMetadata.authors;
}
if (!this.toUpdateMetadata.categories || this.toUpdateMetadata.categories.length === 0) {
this.toUpdateMetadata.categories = this.fetchedMetadata.categories;
}
if(this.updateThumbnail) {
this.toUpdateMetadata.thumbnail = this.fetchedMetadata.thumbnail;
}
}
shouldUpdateThumbnail() {
this.updateThumbnail = true;
}
}