From 28b3de29d8976e1f691e6cf4a6fad78997370f56 Mon Sep 17 00:00:00 2001 From: Rafi Arrafif Date: Mon, 29 Jun 2026 12:52:52 +0700 Subject: [PATCH] wip: create new req endpoint to get characters data --- src/config/apis/jikan/media.reference.ts | 12 +-- .../bulkInsertMediaCharacter.repository.ts | 9 ++ .../bulkinsertMedia.repository.ts | 6 +- .../services/http/bulkInsertAnime.service.ts | 25 +++-- .../internal/bulkInsertCharWithVA.service.ts | 100 +++++++++--------- .../bulkInsertStaffOrPeople.service.ts | 28 ++--- .../internal/types/mediaCharWithVAInfo.ts | 59 ----------- src/modules/internal/types/mediaCharacters.ts | 48 +++++++++ 8 files changed, 148 insertions(+), 139 deletions(-) create mode 100644 src/modules/internal/repositories/bulkInsertMediaCharacter.repository.ts delete mode 100644 src/modules/internal/types/mediaCharWithVAInfo.ts create mode 100644 src/modules/internal/types/mediaCharacters.ts diff --git a/src/config/apis/jikan/media.reference.ts b/src/config/apis/jikan/media.reference.ts index 4d7d2ac..55b5339 100644 --- a/src/config/apis/jikan/media.reference.ts +++ b/src/config/apis/jikan/media.reference.ts @@ -1,9 +1,9 @@ -import { baseURL } from "./baseUrl"; +import {baseURL} from "./baseUrl"; export const getContentReferenceAPI = (malId: number) => { - return { - baseURL, - getMediaFullInfo: `/anime/${malId}/full`, - getMediaCharactersWithVA: `/anime/${malId}/characters`, - }; + return { + baseURL, + getMediaFullInfo: `/anime/${malId}/full`, + getMediaCharacters: `/anime/${malId}/characters`, + }; }; diff --git a/src/modules/internal/repositories/bulkInsertMediaCharacter.repository.ts b/src/modules/internal/repositories/bulkInsertMediaCharacter.repository.ts new file mode 100644 index 0000000..eedcf4a --- /dev/null +++ b/src/modules/internal/repositories/bulkInsertMediaCharacter.repository.ts @@ -0,0 +1,9 @@ +import {AppError} from "../../../helpers/error/instances/app"; + +export const bulkInsertMediaCharacterRepository = async (animeMalId: number) => { + try { + return animeMalId + } catch (error) { + throw new AppError(500, "Failed to bulk insert media characters", error); + } +} \ No newline at end of file diff --git a/src/modules/internal/repositories/bulkinsertMedia.repository.ts b/src/modules/internal/repositories/bulkinsertMedia.repository.ts index 21b8e83..93ff995 100644 --- a/src/modules/internal/repositories/bulkinsertMedia.repository.ts +++ b/src/modules/internal/repositories/bulkinsertMedia.repository.ts @@ -143,7 +143,11 @@ export const InsertMediaRepository = async ({ payload }: { payload: MediaFullInf }); await bulkInsertMediaProducerStudioLicensorRepository(tx, media.id, producerPayload); - return media.id; + return { + id: media.id, + mal_id: payload.mal_id, + name: payload.title, + }; }); } catch (error) { diff --git a/src/modules/internal/services/http/bulkInsertAnime.service.ts b/src/modules/internal/services/http/bulkInsertAnime.service.ts index b841346..b009a57 100644 --- a/src/modules/internal/services/http/bulkInsertAnime.service.ts +++ b/src/modules/internal/services/http/bulkInsertAnime.service.ts @@ -1,17 +1,24 @@ import {ErrorForwarder} from "../../../../helpers/error/instances/forwarder"; import {InsertMediaRepository} from "../../repositories/bulkinsertMedia.repository"; import {getContentReferenceAPI} from "../../../../config/apis/jikan/media.reference"; +import {bulkInsertMediaCharacterRepository} from "../../repositories/bulkInsertMediaCharacter.repository"; import {MediaFullInfoResponse} from "../../types/mediaFullInfo.type"; +import {MediaCharacters} from "../../types/mediaCharacters"; export const bulkInsertAnimeService = async (malId: number) => { - try { - const { baseURL, getMediaFullInfo } = getContentReferenceAPI(malId); - const mediaFullInfo = (await fetch(baseURL + getMediaFullInfo).then((res) => res.json())) as MediaFullInfoResponse; + try { + const {baseURL, getMediaFullInfo, getMediaCharacters} = getContentReferenceAPI(malId); + const mediaFullInfo = (await fetch(baseURL + getMediaFullInfo).then((res) => res.json())) as MediaFullInfoResponse; - return await InsertMediaRepository({ - payload: mediaFullInfo.data, - }); - } catch (error) { - ErrorForwarder(error); - } + const insertedMedia = await InsertMediaRepository({ + payload: mediaFullInfo.data, + }); + + + // await bulkInsertMediaCharacterRepository(insertedMedia.mal_id) + const mediaChar = await fetch(baseURL + getMediaCharacters).then((res) => res.json()) as MediaCharacters; + return mediaChar; + } catch (error) { + ErrorForwarder(error); + } }; diff --git a/src/modules/internal/services/internal/bulkInsertCharWithVA.service.ts b/src/modules/internal/services/internal/bulkInsertCharWithVA.service.ts index ba3100e..a783d79 100644 --- a/src/modules/internal/services/internal/bulkInsertCharWithVA.service.ts +++ b/src/modules/internal/services/internal/bulkInsertCharWithVA.service.ts @@ -1,58 +1,58 @@ -import { SystemAccountId } from "../../../../config/account/system"; -import { getContentReferenceAPI } from "../../../../config/apis/jikan/media.reference"; -import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder"; -import { bulkInsertCharactersRepository } from "../../repositories/bulkInsertCharacters.repository"; -import { bulkInsertLangVARepository } from "../../repositories/bulkInsertLangVA.repository"; -import { MediaCharWithVAInfo } from "../../types/mediaCharWithVAInfo"; -import { bulkInsertStaffOrPeopleService } from "./bulkInsertStaffOrPeople.service"; +import {SystemAccountId} from "../../../../config/account/system"; +import {getContentReferenceAPI} from "../../../../config/apis/jikan/media.reference"; +import {ErrorForwarder} from "../../../../helpers/error/instances/forwarder"; +import {bulkInsertCharactersRepository} from "../../repositories/bulkInsertCharacters.repository"; +import {bulkInsertLangVARepository} from "../../repositories/bulkInsertLangVA.repository"; +import {MediaCharWithVAInfo} from "../../types/mediaCharacters"; +import {bulkInsertStaffOrPeopleService} from "./bulkInsertStaffOrPeople.service"; export const bulkInsertCharWithVAService = async (malId: number) => { - try { - const { baseURL, getMediaCharactersWithVA } = getContentReferenceAPI(malId); - const charactersWithVAData = (await fetch( - `${baseURL}${getMediaCharactersWithVA}`, - ).then((res) => res.json())) as MediaCharWithVAInfo; + try { + const {baseURL, getMediaCharactersWithVA} = getContentReferenceAPI(malId); + const charactersWithVAData = (await fetch( + `${baseURL}${getMediaCharactersWithVA}`, + ).then((res) => res.json())) as MediaCharWithVAInfo; - const insertedCharacters = []; - for (const charEntry of charactersWithVAData.data) { - // Insert character if not exists - const characterInsertedId = await bulkInsertCharactersRepository({ - malId: charEntry.character.mal_id, - name: charEntry.character.name, - role: charEntry.role, - favorites: charEntry.favorites, - imageUrl: charEntry.character.images.webp.image_url, - smallImageUrl: charEntry.character.images.webp.small_image_url, - creatorId: SystemAccountId, - }); + const insertedCharacters = []; + for (const charEntry of charactersWithVAData.data) { + // Insert character if not exists + const characterInsertedId = await bulkInsertCharactersRepository({ + malId: charEntry.character.mal_id, + name: charEntry.character.name, + role: charEntry.role, + favorites: charEntry.favorites, + imageUrl: charEntry.character.images.webp.image_url, + smallImageUrl: charEntry.character.images.webp.small_image_url, + creatorId: SystemAccountId, + }); - // Insert character voice actors if not exists - const insertedVAs: { staffId: string; lang: string }[] = []; - for (const VAEntries of charEntry.voice_actors) { - const insertedVAId = await bulkInsertStaffOrPeopleService( - VAEntries.person, - ); - insertedVAs.push({ - staffId: insertedVAId.id, - lang: VAEntries.language, - }); - } + // Insert character voice actors if not exists + const insertedVAs: { staffId: string; lang: string }[] = []; + for (const VAEntries of charEntry.voice_actors) { + const insertedVAId = await bulkInsertStaffOrPeopleService( + VAEntries.person, + ); + insertedVAs.push({ + staffId: insertedVAId.id, + lang: VAEntries.language, + }); + } - // Link character with inserted VAs - for (const langVA of insertedVAs) { - await bulkInsertLangVARepository({ - language: langVA.lang, - vaId: langVA.staffId, - charId: characterInsertedId.id, - creatorId: SystemAccountId, - }); - } + // Link character with inserted VAs + for (const langVA of insertedVAs) { + await bulkInsertLangVARepository({ + language: langVA.lang, + vaId: langVA.staffId, + charId: characterInsertedId.id, + creatorId: SystemAccountId, + }); + } - insertedCharacters.push(characterInsertedId); + insertedCharacters.push(characterInsertedId); + } + + return insertedCharacters; + } catch (error) { + ErrorForwarder(error); } - - return insertedCharacters; - } catch (error) { - ErrorForwarder(error); - } }; diff --git a/src/modules/internal/services/internal/bulkInsertStaffOrPeople.service.ts b/src/modules/internal/services/internal/bulkInsertStaffOrPeople.service.ts index 0f6ef4c..d731c02 100644 --- a/src/modules/internal/services/internal/bulkInsertStaffOrPeople.service.ts +++ b/src/modules/internal/services/internal/bulkInsertStaffOrPeople.service.ts @@ -1,17 +1,17 @@ -import { SystemAccountId } from "../../../../config/account/system"; -import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder"; -import { bulkInsertVoiceActorRepository } from "../../repositories/bulkInsertVoiceActor.repository"; -import { Person } from "../../types/mediaCharWithVAInfo"; +import {SystemAccountId} from "../../../../config/account/system"; +import {ErrorForwarder} from "../../../../helpers/error/instances/forwarder"; +import {bulkInsertVoiceActorRepository} from "../../repositories/bulkInsertVoiceActor.repository"; +import {Person} from "../../types/mediaCharacters"; export const bulkInsertStaffOrPeopleService = async (peopleData: Person) => { - try { - return await bulkInsertVoiceActorRepository({ - malId: peopleData.mal_id, - name: peopleData.name, - imageUrl: peopleData.images.jpg.image_url, - creatorId: SystemAccountId, - }); - } catch (error) { - ErrorForwarder(error); - } + try { + return await bulkInsertVoiceActorRepository({ + malId: peopleData.mal_id, + name: peopleData.name, + imageUrl: peopleData.images.jpg.image_url, + creatorId: SystemAccountId, + }); + } catch (error) { + ErrorForwarder(error); + } }; diff --git a/src/modules/internal/types/mediaCharWithVAInfo.ts b/src/modules/internal/types/mediaCharWithVAInfo.ts deleted file mode 100644 index b1f3091..0000000 --- a/src/modules/internal/types/mediaCharWithVAInfo.ts +++ /dev/null @@ -1,59 +0,0 @@ -export interface MediaCharWithVAInfo { - data: Datum[]; -} - -interface Datum { - character: Character; - role: Role; - favorites: number; - voice_actors: VoiceActor[]; -} - -interface Character { - mal_id: number; - url: string; - images: CharacterImages; - name: string; -} - -interface CharacterImages { - jpg: Jpg; - webp: Webp; -} - -interface Jpg { - image_url: string; -} - -interface Webp { - image_url: string; - small_image_url: string; -} - -enum Role { - Main = "Main", - Supporting = "Supporting", -} - -export interface VoiceActor { - person: Person; - language: Language; -} - -enum Language { - English = "English", - Japanese = "Japanese", - PortugueseBR = "Portuguese (BR)", - Spanish = "Spanish", -} - -export interface Person { - mal_id: number; - url: string; - images: PersonImages; - name: string; -} - -interface PersonImages { - jpg: Jpg; -} diff --git a/src/modules/internal/types/mediaCharacters.ts b/src/modules/internal/types/mediaCharacters.ts new file mode 100644 index 0000000..5222ab4 --- /dev/null +++ b/src/modules/internal/types/mediaCharacters.ts @@ -0,0 +1,48 @@ +interface StaffVA { + mal_id: number; + url: string; + images: { + jpg: { + image_url: string; + }, + webp: { + image_url: string; + small_image_url: string; + } + } +} + +interface voiceActor { + person: StaffVA; + language: string; +} + +interface Character { + mal_id: number; + name: string; + url: string; + images: { + jpg: { + image_url: string; + }, + webp: { + image_url: string; + small_image_url: string; + } + } +} + +enum Role { + Main = "Main", + Supporting = "Supporting", + Background = "Background", +} + +interface MediaChar { + character: Character; + role: Role; + favorites: number; + voice_actors: voiceActor[]; +} + +export type MediaCharacters = Array; \ No newline at end of file