- Merge book metadata service to book service
- Standardize API endpoints
This commit is contained in:
@@ -5,6 +5,9 @@ import {catchError, map, tap} from 'rxjs/operators';
|
||||
import {Book, BookMetadata, BookSetting} from '../model/book.model';
|
||||
import {BookState} from '../model/state/book-state.model';
|
||||
import {API_CONFIG} from '../../config/api-config';
|
||||
import {FetchMetadataRequest} from '../../metadata/model/request/fetch-metadata-request.model';
|
||||
import {MetadataRefreshRequest} from '../../metadata/model/request/metadata-refresh-request.model';
|
||||
import {MessageService} from 'primeng/api';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
@@ -22,6 +25,7 @@ export class BookService {
|
||||
|
||||
|
||||
private http = inject(HttpClient);
|
||||
private messageService = inject(MessageService);
|
||||
|
||||
loadBooks(): void {
|
||||
const currentState = this.bookStateSubject.value;
|
||||
@@ -139,7 +143,7 @@ export class BookService {
|
||||
);
|
||||
}
|
||||
|
||||
getPdfData(bookId: number): Observable<Blob> {
|
||||
getFileContent(bookId: number): Observable<Blob> {
|
||||
return this.http.get<Blob>(`${this.url}/${bookId}/content`, {responseType: 'blob' as 'json'});
|
||||
}
|
||||
|
||||
@@ -151,6 +155,32 @@ export class BookService {
|
||||
});
|
||||
}
|
||||
|
||||
downloadFile(bookId: number): void {
|
||||
const downloadUrl = `${this.url}/${bookId}/download`;
|
||||
this.http.get(downloadUrl, {responseType: 'blob', observe: 'response'})
|
||||
.subscribe({
|
||||
next: (response) => {
|
||||
const contentDisposition = response.headers.get('Content-Disposition');
|
||||
const filename = contentDisposition
|
||||
? contentDisposition.match(/filename="(.+?)"/)?.[1] || `book_${bookId}.pdf`
|
||||
: `book_${bookId}.pdf`;
|
||||
this.saveFile(response.body as Blob, filename);
|
||||
},
|
||||
error: (err) => console.error('Error downloading file:', err),
|
||||
});
|
||||
}
|
||||
|
||||
private saveFile(blob: Blob, filename: string): void {
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = filename;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
window.URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
savePdfProgress(bookId: number, progress: number): Observable<void> {
|
||||
return this.http.post<void>(`${this.url}/progress`, {bookId: bookId, pdfProgress: progress});
|
||||
}
|
||||
@@ -159,6 +189,58 @@ export class BookService {
|
||||
return this.http.post<void>(`${this.url}/progress`, {bookId: bookId, epubProgress: progress});
|
||||
}
|
||||
|
||||
|
||||
/*------------------ All the metadata related calls go here ------------------*/
|
||||
|
||||
fetchBookMetadata(bookId: number, request: FetchMetadataRequest): Observable<BookMetadata[]> {
|
||||
return this.http.post<BookMetadata[]>(`${this.url}/${bookId}/metadata/prospective`, request);
|
||||
}
|
||||
|
||||
updateBookMetadata(bookId: number, bookMetadata: BookMetadata): Observable<BookMetadata> {
|
||||
return this.http.put<BookMetadata>(`${this.url}/${bookId}/metadata`, bookMetadata).pipe(
|
||||
map(updatedMetadata => {
|
||||
this.handleBookMetadataUpdate(bookId, updatedMetadata);
|
||||
return updatedMetadata;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
autoRefreshMetadata(metadataRefreshRequest: MetadataRefreshRequest): Observable<any> {
|
||||
return this.http.put<void>(`${this.url}/metadata/refresh`, metadataRefreshRequest).pipe(
|
||||
map(() => {
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: 'Metadata Update Scheduled',
|
||||
detail: 'The metadata update for the selected books has been successfully scheduled.'
|
||||
});
|
||||
return {success: true};
|
||||
}),
|
||||
catchError((e) => {
|
||||
if (e.status === 409) {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Task Already Running',
|
||||
life: 5000,
|
||||
detail: 'A metadata refresh task is already in progress. Please wait for it to complete before starting another one.'
|
||||
});
|
||||
} else {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Metadata Update Failed',
|
||||
life: 5000,
|
||||
detail: 'An unexpected error occurred while scheduling the metadata update. Please try again later or contact support if the issue persists.'
|
||||
});
|
||||
}
|
||||
return of({success: false});
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
getUploadCoverUrl(bookId: number): string {
|
||||
return this.url + '/' + bookId + "/metadata/cover"
|
||||
}
|
||||
|
||||
|
||||
/*------------------ All the websocket handlers go below ------------------*/
|
||||
|
||||
handleNewlyCreatedBook(book: Book): void {
|
||||
@@ -194,9 +276,4 @@ export class BookService {
|
||||
});
|
||||
this.bookStateSubject.next({...currentState, books: updatedBooks})
|
||||
}
|
||||
|
||||
getBookMetadata(bookId: number): BookMetadata | null {
|
||||
const book = this.bookStateSubject.value.books?.find(book => book.id === bookId);
|
||||
return book?.metadata ?? null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
import {Injectable, inject} from '@angular/core';
|
||||
import {HttpClient, HttpHeaders} from '@angular/common/http';
|
||||
import {API_CONFIG} from '../../config/api-config';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class FileService {
|
||||
private readonly url = `${API_CONFIG.BASE_URL}/api/v1/book`;
|
||||
private readonly http = inject(HttpClient);
|
||||
|
||||
downloadFile(bookId: number): void {
|
||||
const downloadUrl = `${this.url}/${bookId}/download`;
|
||||
this.http.get(downloadUrl, {responseType: 'blob', observe: 'response'})
|
||||
.subscribe({
|
||||
next: (response) => {
|
||||
const contentDisposition = response.headers.get('Content-Disposition');
|
||||
const filename = contentDisposition
|
||||
? contentDisposition.match(/filename="(.+?)"/)?.[1] || `book_${bookId}.pdf`
|
||||
: `book_${bookId}.pdf`;
|
||||
this.saveFile(response.body as Blob, filename);
|
||||
},
|
||||
error: (err) => console.error('Error downloading file:', err),
|
||||
});
|
||||
}
|
||||
|
||||
private saveFile(blob: Blob, filename: string): void {
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = filename;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
window.URL.revokeObjectURL(url);
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ import {API_CONFIG} from '../../config/api-config';
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class LibraryService {
|
||||
private readonly url = `${API_CONFIG.BASE_URL}/api/v1/library`;
|
||||
private readonly url = `${API_CONFIG.BASE_URL}/api/v1/libraries`;
|
||||
|
||||
private libraryStateSubject = new BehaviorSubject<LibraryState>({
|
||||
libraries: null,
|
||||
|
||||
@@ -13,7 +13,7 @@ import {Library} from '../model/library.model';
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class ShelfService {
|
||||
private readonly url = `${API_CONFIG.BASE_URL}/api/v1/shelf`;
|
||||
private readonly url = `${API_CONFIG.BASE_URL}/api/v1/shelves`;
|
||||
private shelfStateSubject = new BehaviorSubject<ShelfState>({
|
||||
shelves: null,
|
||||
loaded: false,
|
||||
|
||||
Reference in New Issue
Block a user