104 lines
2.7 KiB
JavaScript
104 lines
2.7 KiB
JavaScript
import axios from 'axios';
|
|
import { Manga, MangaCollection, MangaDetail } from '../../domain/manga';
|
|
import { Chapter } from '../../domain/chapter';
|
|
|
|
export class ApiMangaRepository {
|
|
constructor() {
|
|
this.api = axios.create({
|
|
baseURL: '/api'
|
|
});
|
|
}
|
|
|
|
async getMangaCollection(page = 1) {
|
|
try {
|
|
const response = await this.api.get(`/mangas?page=${page}`);
|
|
const data = response.data;
|
|
|
|
const mangas = data.items.map(item => new Manga(
|
|
item.id,
|
|
item.title,
|
|
item.slug,
|
|
item.imageUrl,
|
|
item.author,
|
|
item.publicationYear,
|
|
item.genres,
|
|
item.status,
|
|
item.rating,
|
|
item.description,
|
|
item.createdAt
|
|
));
|
|
|
|
return new MangaCollection(
|
|
mangas,
|
|
data.total,
|
|
data.page,
|
|
data.limit,
|
|
data.hasNextPage,
|
|
data.hasPreviousPage
|
|
);
|
|
} catch (error) {
|
|
console.error('Error fetching manga collection:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async searchMangas(query) {
|
|
try {
|
|
const response = await this.api.get(`/mangas-search?title=${encodeURIComponent(query)}`);
|
|
const data = response.data;
|
|
|
|
return data.items.map(item => new Manga(
|
|
item.externalId,
|
|
item.title,
|
|
item.slug,
|
|
item.imageUrl,
|
|
item.author,
|
|
item.publicationYear,
|
|
item.genres,
|
|
item.status,
|
|
item.rating,
|
|
item.description,
|
|
item.createdAt
|
|
));
|
|
} catch (error) {
|
|
console.error('Error searching mangas:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async getMangaBySlug(slug) {
|
|
try {
|
|
const mangaResponse = await this.api.get(`/mangas/by-slug/${slug}`);
|
|
const mangaData = mangaResponse.data;
|
|
|
|
const chaptersResponse = await this.api.get(`/mangas/${mangaData.id}/chapters?page=1&limit=1000&sortOrder=desc`);
|
|
const chaptersData = chaptersResponse.data;
|
|
|
|
const chapters = chaptersData.items.map(item => new Chapter(
|
|
item.id,
|
|
parseFloat(item.number),
|
|
item.title,
|
|
item.volume,
|
|
item.isVisible,
|
|
item.createdAt
|
|
));
|
|
|
|
return new MangaDetail({
|
|
id: mangaData.id,
|
|
title: mangaData.title,
|
|
slug: mangaData.slug,
|
|
imageUrl: mangaData.imageUrl,
|
|
author: mangaData.author,
|
|
publicationYear: mangaData.publicationYear,
|
|
genres: mangaData.genres,
|
|
status: mangaData.status,
|
|
rating: mangaData.rating,
|
|
description: mangaData.description,
|
|
createdAt: mangaData.createdAt
|
|
}, chapters);
|
|
} catch (error) {
|
|
console.error('Error fetching manga details:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
} |