- Refactor to remove BookMetadataBI

- Use inject everywhere instead of constructor injection
This commit is contained in:
aditya.chandel
2025-01-16 00:10:42 -07:00
parent edbb71e8a6
commit 532246b18b
36 changed files with 301 additions and 338 deletions
@@ -1,4 +1,4 @@
import {Component, OnInit, ViewChild} from '@angular/core';
import {Component, inject, OnInit, ViewChild} from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {MenuItem, MessageService} from 'primeng/api';
import {LibraryService} from '../../service/library.service';
@@ -81,16 +81,14 @@ export class BookBrowserComponent implements OnInit {
@ViewChild(BookTableComponent) bookTableComponent!: BookTableComponent;
constructor(
private activatedRoute: ActivatedRoute,
private messageService: MessageService,
private libraryService: LibraryService,
private bookService: BookService,
private shelfService: ShelfService,
private dialogService: DialogService,
private sortService: SortService,
private libraryShelfMenuService: LibraryShelfMenuService) {
}
private activatedRoute = inject(ActivatedRoute);
private messageService = inject(MessageService);
private libraryService = inject(LibraryService);
private bookService = inject(BookService);
private shelfService = inject(ShelfService);
private dialogService = inject(DialogService);
private sortService = inject(SortService);
private libraryShelfMenuService = inject(LibraryShelfMenuService);
ngOnInit(): void {
this.sortOptions = SortService.generateSortOptions();
@@ -262,7 +260,9 @@ export class BookBrowserComponent implements OnInit {
deselectAllBooks(): void {
this.selectedBooks.clear();
this.isDrawerVisible = false;
this.bookTableComponent.clearSelectedBooks();
if(this.bookTableComponent) {
this.bookTableComponent.clearSelectedBooks();
}
}
viewChanged() {
@@ -1,4 +1,4 @@
import {Component, Input, OnInit} from '@angular/core';
import {Component, inject, Input, OnInit} from '@angular/core';
import {Book} from '../../../model/book.model';
import {Button} from 'primeng/button';
import {MenuModule} from 'primeng/menu';
@@ -26,9 +26,9 @@ export class BookCardComponent implements OnInit {
items: MenuItem[] | undefined;
isHovered: boolean = false;
constructor(private bookService: BookService, private dialogService: DialogService,
private metadataDialogService: MetadataDialogService) {
}
private bookService = inject(BookService);
private dialogService = inject(DialogService);
private metadataDialogService = inject(MetadataDialogService);
ngOnInit(): void {
this.initMenu();
@@ -39,7 +39,7 @@ export class BookCardComponent implements OnInit {
}
readBook(book: Book): void {
this.bookService.readBook(book);
this.bookService.readBook(book.id);
}
toggleSelection(selected: boolean): void {
@@ -1,4 +1,4 @@
import {Component, EventEmitter, Input, OnChanges, Output} from '@angular/core';
import {Component, EventEmitter, inject, Input, OnChanges, Output} from '@angular/core';
import {TableModule} from 'primeng/table';
import {NgIf} from '@angular/common';
import {BookService} from '../../../service/book.service';
@@ -30,8 +30,9 @@ export class BookTableComponent implements OnChanges {
@Input() books: Book[] = [];
@Input() sortOption: SortOption | null = null;
constructor(private bookService: BookService, private metadataDialogService: MetadataDialogService) {
}
private bookService = inject(BookService);
private metadataDialogService = inject(MetadataDialogService);
// Hack to set virtual-scroller height
ngOnChanges() {
@@ -1,4 +1,4 @@
import {Component, OnDestroy, OnInit} from '@angular/core';
import {Component, inject, OnDestroy, OnInit} from '@angular/core';
import {Subject, Subscription} from 'rxjs';
import {catchError, debounceTime, distinctUntilChanged, switchMap} from 'rxjs/operators';
import {Book} from '../../model/book.model';
@@ -11,6 +11,7 @@ import {DialogService} from 'primeng/dynamicdialog';
import {Button} from 'primeng/button';
import {NgForOf, NgIf} from '@angular/common';
import {MetadataDialogService} from '../../../metadata/service/metadata-dialog.service';
import {LibraryShelfMenuService} from '../../service/library-shelf-menu.service';
@Component({
selector: 'app-book-searcher',
@@ -31,8 +32,9 @@ export class BookSearcherComponent implements OnInit, OnDestroy {
#searchSubject = new Subject<string>();
#subscription!: Subscription;
constructor(private bookService: BookService, private metadataDialogService: MetadataDialogService) {
}
private bookService = inject(BookService);
private metadataDialogService = inject(MetadataDialogService);
ngOnInit(): void {
this.initializeSearch();
@@ -1,4 +1,4 @@
import {Component, ViewChild} from '@angular/core';
import {Component, inject, ViewChild} from '@angular/core';
import {DialogService, DynamicDialogRef} from 'primeng/dynamicdialog';
import {DirectoryPickerComponent} from '../../../utilities/component/directory-picker/directory-picker.component';
import {MessageService} from 'primeng/api';
@@ -12,6 +12,7 @@ import {Step, StepList, StepPanel, StepPanels, Stepper} from 'primeng/stepper';
import {NgIf} from '@angular/common';
import {FormsModule} from '@angular/forms';
import {InputText} from 'primeng/inputtext';
import {BookService} from '../../service/book.service';
@Component({
selector: 'app-library-creator',
@@ -41,13 +42,12 @@ export class LibraryCreatorComponent {
ref: DynamicDialogRef | undefined;
selectedIcon: string | null = null;
constructor(
private dialogService: DialogService,
private dynamicDialogRef: DynamicDialogRef,
private libraryService: LibraryService,
private messageService: MessageService,
private router: Router) {
}
private dialogService = inject(DialogService);
private dynamicDialogRef = inject(DynamicDialogRef);
private libraryService = inject(LibraryService);
private messageService = inject(MessageService);
private router = inject(Router);
show() {
this.ref = this.dialogService.open(DirectoryPickerComponent, {
@@ -1,4 +1,4 @@
import {Component, OnInit, ViewChild} from '@angular/core';
import {Component, inject, OnInit, ViewChild} from '@angular/core';
import {DynamicDialogConfig, DynamicDialogRef} from 'primeng/dynamicdialog';
import {Book} from '../../model/book.model';
import {MessageService, PrimeTemplate} from 'primeng/api';
@@ -16,6 +16,7 @@ import {Checkbox} from 'primeng/checkbox';
import {FormsModule} from '@angular/forms';
import {Dialog} from 'primeng/dialog';
import {InputText} from 'primeng/inputtext';
import {Router} from '@angular/router';
@Component({
selector: 'app-shelf-assigner',
@@ -37,30 +38,24 @@ import {InputText} from 'primeng/inputtext';
styleUrls: ['./shelf-assigner.component.scss']
})
export class ShelfAssignerComponent implements OnInit {
shelfState$: Observable<ShelfState>;
book: Book;
private shelfService = inject(ShelfService);
private dynamicDialogConfig = inject(DynamicDialogConfig);
private dynamicDialogRef = inject(DynamicDialogRef);
private messageService = inject(MessageService);
private bookService = inject(BookService);
shelfState$: Observable<ShelfState> = this.shelfService.shelfState$;
book: Book = this.dynamicDialogConfig.data.book;
selectedShelves: Shelf[] = [];
displayShelfDialog: boolean = false;
shelfName: string = '';
bookIds = new Set<number>();
isMultiBooks: boolean = false;
bookIds: Set<number> = this.dynamicDialogConfig.data.bookIds;
isMultiBooks: boolean = this.dynamicDialogConfig.data.isMultiBooks;
selectedIcon: string | null = null;
@ViewChild(IconPickerComponent) iconPicker: IconPickerComponent | undefined;
constructor(
private shelfService: ShelfService,
private dynamicDialogConfig: DynamicDialogConfig,
private dynamicDialogRef: DynamicDialogRef,
private messageService: MessageService,
private bookService: BookService
) {
this.shelfState$ = this.shelfService.shelfState$;
this.book = this.dynamicDialogConfig.data.book;
this.bookIds = this.dynamicDialogConfig.data.bookIds;
this.isMultiBooks = this.dynamicDialogConfig.data.isMultiBooks;
}
ngOnInit(): void {
if (!this.isMultiBooks && this.book.shelves) {
this.shelfState$.pipe(
+12 -9
View File
@@ -4,6 +4,7 @@ export type BookType = "PDF" | "EPUB";
export interface Book {
id: number;
bookType: BookType;
libraryId: number;
metadata?: BookMetadata;
shelves?: Shelf[];
@@ -11,24 +12,25 @@ export interface Book {
addedOn?: string;
epubProgress?: string;
pdfProgress?: number;
bookType: BookType;
}
export interface BookMetadata {
bookId: number;
title: string;
subtitle?: string;
publisher: string;
publishedDate: string;
publisher?: string;
publishedDate?: string;
description?: string;
isbn13: string;
isbn10: string;
isbn13?: string;
isbn10?: string;
asin?: string;
pageCount: number;
language: string;
rating: number;
reviewCount: number;
pageCount?: number | null;
language?: string;
rating?: number | null;
reviewCount?: number | null;
authors: Author[];
categories: Category[];
[key: string]: any;
}
@@ -51,6 +53,7 @@ export interface FetchedMetadata {
reviewCount: number | null;
authors: string[];
categories: string[];
[key: string]: any;
}
@@ -1,4 +1,4 @@
import {Injectable} from '@angular/core';
import {inject, Injectable} from '@angular/core';
import {BehaviorSubject, Observable, of} from 'rxjs';
import {HttpClient} from '@angular/common/http';
import {catchError, map, switchMap, tap} from 'rxjs/operators';
@@ -18,7 +18,9 @@ export class BookService {
});
bookState$ = this.bookStateSubject.asObservable();
constructor(private http: HttpClient) {
private http = inject(HttpClient);
constructor() {
this.loadBooks();
}
@@ -113,21 +115,26 @@ export class BookService {
);
}
readBook(book: Book): void {
if (book.bookType === "PDF") {
const url = `/pdf-viewer/book/${book.id}`;
window.open(url, '_blank');
this.updateLastReadTime(book.id).subscribe({
next: () => console.log('Last read time updated successfully'),
error: err => console.error('Failed to update last read time', err),
});
} else if (book.bookType === "EPUB") {
const url = `/epub-viewer/book/${book.id}`;
window.open(url, '_blank');
} else {
console.error('Unsupported book type:', book.bookType);
return;
}
readBook(bookId: number): void {
this.getBookById(bookId).subscribe((book) => {
if (book) {
if (book.bookType === "PDF") {
const url = `/pdf-viewer/book/${book.id}`;
window.open(url, '_blank');
this.updateLastReadTime(book.id).subscribe({
next: () => console.log('Last read time updated successfully'),
error: err => console.error('Failed to update last read time', err),
});
} else if (book.bookType === "EPUB") {
const url = `/epub-viewer/book/${book.id}`;
window.open(url, '_blank');
} else {
console.error('Unsupported book type:', book.bookType);
}
} else {
console.error('Book not found');
}
});
}
searchBooks(query: string): Observable<Book[]> {
@@ -145,7 +152,7 @@ export class BookService {
getBookData(bookId: number): Observable<Blob> {
console.log('Fetching book data...');
return this.http.get<Blob>(`${this.url}/${bookId}/data`, { responseType: 'blob' as 'json' });
return this.http.get<Blob>(`${this.url}/${bookId}/data`, {responseType: 'blob' as 'json'});
}
getBookCoverUrl(bookId: number): string {
@@ -161,7 +168,7 @@ export class BookService {
}
saveEpubProgress(bookId: number, progress: string): Observable<void> {
return this.http.post<void>(`${this.url}/progress`, { bookId: bookId, epubProgress: progress });
return this.http.post<void>(`${this.url}/progress`, {bookId: bookId, epubProgress: progress});
}
/*------------------ All the websocket handlers go below ------------------*/
@@ -1,4 +1,4 @@
import {Injectable} from '@angular/core';
import {inject, Injectable} from '@angular/core';
import {ConfirmationService, MenuItem, MessageService} from 'primeng/api';
import {Router} from '@angular/router';
import {LibraryService} from './library.service';
@@ -13,15 +13,14 @@ import {MetadataRefreshType} from '../../metadata/model/request/metadata-refresh
providedIn: 'root',
})
export class LibraryShelfMenuService {
constructor(
private confirmationService: ConfirmationService,
private messageService: MessageService,
private libraryService: LibraryService,
private shelfService: ShelfService,
private router: Router,
private dialogService: DialogService
) {
}
private confirmationService = inject(ConfirmationService);
private messageService = inject(MessageService);
private libraryService = inject(LibraryService);
private shelfService = inject(ShelfService);
private router = inject(Router);
private dialogService = inject(DialogService);
initializeLibraryMenuItems(entity: Library | Shelf | null): MenuItem[] {
return [
@@ -1,4 +1,4 @@
import {Injectable} from '@angular/core';
import {inject, Injectable} from '@angular/core';
import {BehaviorSubject, Observable, of, tap} from 'rxjs';
import {HttpClient} from '@angular/common/http';
import {catchError, map} from 'rxjs/operators';
@@ -20,7 +20,10 @@ export class LibraryService {
});
libraryState$ = this.libraryStateSubject.asObservable();
constructor(private http: HttpClient, private bookService: BookService) {
private http = inject(HttpClient);
private bookService = inject(BookService);
constructor() {
this.loadLibraries();
}
@@ -48,12 +51,12 @@ export class LibraryService {
map(createdLibrary => {
const currentState = this.libraryStateSubject.value;
const updatedLibraries = currentState.libraries ? [...currentState.libraries, createdLibrary] : [createdLibrary];
this.libraryStateSubject.next({ ...currentState, libraries: updatedLibraries });
this.libraryStateSubject.next({...currentState, libraries: updatedLibraries});
return createdLibrary;
}),
catchError(error => {
const currentState = this.libraryStateSubject.value;
this.libraryStateSubject.next({ ...currentState, error: error.message });
this.libraryStateSubject.next({...currentState, error: error.message});
throw error;
})
);
@@ -65,11 +68,11 @@ export class LibraryService {
this.bookService.removeBooksByLibraryId(libraryId);
const currentState = this.libraryStateSubject.value;
const updatedLibraries = currentState.libraries?.filter(library => library.id !== libraryId) || [];
this.libraryStateSubject.next({ ...currentState, libraries: updatedLibraries });
this.libraryStateSubject.next({...currentState, libraries: updatedLibraries});
}),
catchError(error => {
const currentState = this.libraryStateSubject.value;
this.libraryStateSubject.next({ ...currentState, error: error.message });
this.libraryStateSubject.next({...currentState, error: error.message});
return of();
})
);
@@ -79,7 +82,7 @@ export class LibraryService {
return this.http.put<void>(`${this.url}/${libraryId}/refresh`, {}).pipe(
catchError(error => {
const currentState = this.libraryStateSubject.value;
this.libraryStateSubject.next({ ...currentState, error: error.message });
this.libraryStateSubject.next({...currentState, error: error.message});
throw error;
})
);
@@ -92,12 +95,12 @@ export class LibraryService {
const updatedLibraries = currentState.libraries?.map(library =>
library.id === libraryId ? updatedLibrary : library
) || [];
this.libraryStateSubject.next({ ...currentState, libraries: updatedLibraries });
this.libraryStateSubject.next({...currentState, libraries: updatedLibraries});
return updatedLibrary;
}),
catchError(error => {
const currentState = this.libraryStateSubject.value;
this.libraryStateSubject.next({ ...currentState, error: error.message });
this.libraryStateSubject.next({...currentState, error: error.message});
throw error;
})
);
@@ -1,4 +1,4 @@
import { Injectable } from '@angular/core';
import {inject, Injectable} from '@angular/core';
import { BehaviorSubject, Observable, of, tap } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';
@@ -19,9 +19,8 @@ export class ShelfService {
});
shelfState$ = this.shelfStateSubject.asObservable();
constructor(private http: HttpClient, private bookService: BookService) {
this.loadShelves();
}
private http = inject(HttpClient);
private bookService = inject(BookService);
private loadShelves(): void {
this.http.get<Shelf[]>(this.url).pipe(