chore(library): review deprecated get, now paging and all are local and not by API
This commit is contained in:
@@ -5,6 +5,10 @@ import { ConfigDBService } from "./config-db.service";
|
|||||||
// Types de cache disponibles
|
// Types de cache disponibles
|
||||||
export type CacheType = "DEFAULT" | "HOME" | "LIBRARIES" | "SERIES" | "BOOKS" | "IMAGES";
|
export type CacheType = "DEFAULT" | "HOME" | "LIBRARIES" | "SERIES" | "BOOKS" | "IMAGES";
|
||||||
|
|
||||||
|
interface KomgaRequestInit extends RequestInit {
|
||||||
|
isImage?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
export abstract class BaseApiService {
|
export abstract class BaseApiService {
|
||||||
protected static async getKomgaConfig(): Promise<AuthConfig> {
|
protected static async getKomgaConfig(): Promise<AuthConfig> {
|
||||||
try {
|
try {
|
||||||
@@ -76,26 +80,14 @@ export abstract class BaseApiService {
|
|||||||
protected static async fetchFromApi<T>(
|
protected static async fetchFromApi<T>(
|
||||||
url: string,
|
url: string,
|
||||||
headers: Headers,
|
headers: Headers,
|
||||||
isImage: boolean = false
|
options: KomgaRequestInit = {}
|
||||||
): Promise<T> {
|
): Promise<T> {
|
||||||
// const startTime = Date.now(); // Capture le temps de début
|
const response = await fetch(url, { headers, ...options });
|
||||||
|
|
||||||
const response = await fetch(url, { headers });
|
|
||||||
|
|
||||||
// const endTime = Date.now(); // Capture le temps de fin
|
|
||||||
// const responseTime = endTime - startTime; // Calcule le temps de réponse
|
|
||||||
|
|
||||||
// // Log le temps de réponse en ms ou en s
|
|
||||||
// if (responseTime >= 1000) {
|
|
||||||
// console.log(`Temps de réponse pour ${url}: ${(responseTime / 1000).toFixed(2)}s`);
|
|
||||||
// } else {
|
|
||||||
// console.log(`Temps de réponse pour ${url}: ${responseTime}ms`);
|
|
||||||
// }
|
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`Erreur HTTP: ${response.status} ${response.statusText}`);
|
throw new Error(`Erreur HTTP: ${response.status} ${response.statusText}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return isImage ? response : response.json();
|
return options.isImage ? response : response.json();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ export class ImageService extends BaseApiService {
|
|||||||
return this.fetchWithCache<ImageResponse>(
|
return this.fetchWithCache<ImageResponse>(
|
||||||
`image-${path}`,
|
`image-${path}`,
|
||||||
async () => {
|
async () => {
|
||||||
const response = await this.fetchFromApi<Response>(url, headers, true);
|
const response = await this.fetchFromApi<Response>(url, headers, { isImage: true });
|
||||||
const contentType = response.headers.get("content-type");
|
const contentType = response.headers.get("content-type");
|
||||||
const arrayBuffer = await response.arrayBuffer();
|
const arrayBuffer = await response.arrayBuffer();
|
||||||
const buffer = Buffer.from(arrayBuffer);
|
const buffer = Buffer.from(arrayBuffer);
|
||||||
|
|||||||
@@ -29,6 +29,41 @@ export class LibraryService extends BaseApiService {
|
|||||||
return library;
|
return library;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static async getAllLibrarySeries(libraryId: string): Promise<Series[]> {
|
||||||
|
try {
|
||||||
|
const config = await this.getKomgaConfig();
|
||||||
|
const url = this.buildUrl(config, "series/list", {
|
||||||
|
size: "1000", // On récupère un maximum de séries
|
||||||
|
});
|
||||||
|
const headers = this.getAuthHeaders(config);
|
||||||
|
headers.set("Content-Type", "application/json");
|
||||||
|
|
||||||
|
const searchBody = {
|
||||||
|
condition: {
|
||||||
|
libraryId: {
|
||||||
|
operator: "is",
|
||||||
|
value: libraryId,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const cacheKey = `library-${libraryId}-all-series`;
|
||||||
|
const response = await this.fetchWithCache<LibraryResponse<Series>>(
|
||||||
|
cacheKey,
|
||||||
|
async () =>
|
||||||
|
this.fetchFromApi<LibraryResponse<Series>>(url, headers, {
|
||||||
|
method: "POST",
|
||||||
|
body: JSON.stringify(searchBody),
|
||||||
|
}),
|
||||||
|
"SERIES"
|
||||||
|
);
|
||||||
|
|
||||||
|
return response.content;
|
||||||
|
} catch (error) {
|
||||||
|
return this.handleError(error, "Impossible de récupérer toutes les séries");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static async getLibrarySeries(
|
static async getLibrarySeries(
|
||||||
libraryId: string,
|
libraryId: string,
|
||||||
page: number = 0,
|
page: number = 0,
|
||||||
@@ -37,21 +72,64 @@ export class LibraryService extends BaseApiService {
|
|||||||
search?: string
|
search?: string
|
||||||
): Promise<LibraryResponse<Series>> {
|
): Promise<LibraryResponse<Series>> {
|
||||||
try {
|
try {
|
||||||
const config = await this.getKomgaConfig();
|
// Récupérer toutes les séries depuis le cache
|
||||||
const url = this.buildUrl(config, "series", {
|
const allSeries = await this.getAllLibrarySeries(libraryId);
|
||||||
library_id: libraryId,
|
|
||||||
page: page.toString(),
|
|
||||||
size: size.toString(),
|
|
||||||
...(unreadOnly && { read_status: "UNREAD,IN_PROGRESS" }),
|
|
||||||
...(search && { search }),
|
|
||||||
});
|
|
||||||
const headers = this.getAuthHeaders(config);
|
|
||||||
|
|
||||||
return this.fetchWithCache<LibraryResponse<Series>>(
|
// Filtrer les séries
|
||||||
`library-${libraryId}-series-${page}-${size}-${unreadOnly}-${search}`,
|
let filteredSeries = allSeries;
|
||||||
async () => this.fetchFromApi<LibraryResponse<Series>>(url, headers),
|
|
||||||
"SERIES"
|
if (unreadOnly) {
|
||||||
);
|
filteredSeries = filteredSeries.filter(
|
||||||
|
(series) => series.booksReadCount < series.booksCount
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (search) {
|
||||||
|
const searchLower = search.toLowerCase();
|
||||||
|
filteredSeries = filteredSeries.filter((series) =>
|
||||||
|
series.metadata.title.toLowerCase().includes(searchLower)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Trier les séries
|
||||||
|
filteredSeries.sort((a, b) => a.metadata.titleSort.localeCompare(b.metadata.titleSort));
|
||||||
|
|
||||||
|
// Calculer la pagination
|
||||||
|
const totalElements = filteredSeries.length;
|
||||||
|
const totalPages = Math.ceil(totalElements / size);
|
||||||
|
const startIndex = page * size;
|
||||||
|
const endIndex = Math.min(startIndex + size, totalElements);
|
||||||
|
const paginatedSeries = filteredSeries.slice(startIndex, endIndex);
|
||||||
|
|
||||||
|
// Construire la réponse
|
||||||
|
return {
|
||||||
|
content: paginatedSeries,
|
||||||
|
empty: paginatedSeries.length === 0,
|
||||||
|
first: page === 0,
|
||||||
|
last: page >= totalPages - 1,
|
||||||
|
number: page,
|
||||||
|
numberOfElements: paginatedSeries.length,
|
||||||
|
pageable: {
|
||||||
|
offset: startIndex,
|
||||||
|
pageNumber: page,
|
||||||
|
pageSize: size,
|
||||||
|
paged: true,
|
||||||
|
sort: {
|
||||||
|
empty: false,
|
||||||
|
sorted: true,
|
||||||
|
unsorted: false,
|
||||||
|
},
|
||||||
|
unpaged: false,
|
||||||
|
},
|
||||||
|
size,
|
||||||
|
sort: {
|
||||||
|
empty: false,
|
||||||
|
sorted: true,
|
||||||
|
unsorted: false,
|
||||||
|
},
|
||||||
|
totalElements,
|
||||||
|
totalPages,
|
||||||
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return this.handleError(error, "Impossible de récupérer les séries");
|
return this.handleError(error, "Impossible de récupérer les séries");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user