refactor: bulk insert media module
This commit is contained in:
@ -1,8 +1,14 @@
|
|||||||
import { Prisma } from "@prisma/client";
|
import { Prisma } from "@prisma/client";
|
||||||
import { GenreOrProducer, MediaFullInfoResponse } from "../types/mediaFullInfo.type";
|
import { GenreOrProducer } from "../types/mediaFullInfo.type";
|
||||||
import { SystemAccountId } from "../../../config/account/system";
|
import { SystemAccountId } from "../../../config/account/system";
|
||||||
import { status } from "elysia";
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bulk Insert Producer, Studio, Licensor
|
||||||
|
*
|
||||||
|
* This function handles the bulk insertion of producers, studios, and licensors.
|
||||||
|
* It takes a transaction client, the ID of the media, and an array of producer, studio, and licensor objects.
|
||||||
|
* */
|
||||||
export const bulkInsertMediaProducerStudioLicensorRepository = async (
|
export const bulkInsertMediaProducerStudioLicensorRepository = async (
|
||||||
tx: Prisma.TransactionClient,
|
tx: Prisma.TransactionClient,
|
||||||
media_id: string,
|
media_id: string,
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import { Prisma } from "@prisma/client";
|
import { Prisma } from "@prisma/client";
|
||||||
import { AppError } from "../../../helpers/error/instances/app";
|
import { AppError } from "../../../helpers/error/instances/app";
|
||||||
import { prisma } from "../../../utils/databases/prisma/connection";
|
|
||||||
import { MediaFullInfoResponse } from "../types/mediaFullInfo.type";
|
import { MediaFullInfoResponse } from "../types/mediaFullInfo.type";
|
||||||
|
import { prisma } from "../../../utils/databases/prisma/connection";
|
||||||
import { bulkInsertMediaProducerStudioLicensorRepository } from "./bulkInsertMediaProducerStudioLicensor.repository";
|
import { bulkInsertMediaProducerStudioLicensorRepository } from "./bulkInsertMediaProducerStudioLicensor.repository";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -132,7 +132,7 @@ export const InsertMediaRepository = async ({ payload }: { payload: MediaFullInf
|
|||||||
})),
|
})),
|
||||||
];
|
];
|
||||||
|
|
||||||
await prisma.$transaction(async (tx) => {
|
return await prisma.$transaction(async (tx) => {
|
||||||
const media = await tx.media.upsert({
|
const media = await tx.media.upsert({
|
||||||
where: { mal_id: payload.mal_id },
|
where: { mal_id: payload.mal_id },
|
||||||
create: constructMediaPayload,
|
create: constructMediaPayload,
|
||||||
@ -143,7 +143,9 @@ export const InsertMediaRepository = async ({ payload }: { payload: MediaFullInf
|
|||||||
});
|
});
|
||||||
|
|
||||||
await bulkInsertMediaProducerStudioLicensorRepository(tx, media.id, producerPayload);
|
await bulkInsertMediaProducerStudioLicensorRepository(tx, media.id, producerPayload);
|
||||||
|
return media.id;
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new AppError(500, "Failed to insert media", error);
|
throw new AppError(500, "Failed to insert media", error);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,18 +1,16 @@
|
|||||||
import {ErrorForwarder} from "../../../../helpers/error/instances/forwarder";
|
import {ErrorForwarder} from "../../../../helpers/error/instances/forwarder";
|
||||||
import {InsertMediaRepository} from "../../repositories/bulkinsertMedia.repository";
|
import {InsertMediaRepository} from "../../repositories/bulkinsertMedia.repository";
|
||||||
import { MediaFullInfoResponse } from "../../types/mediaFullInfo.type";
|
|
||||||
import {getContentReferenceAPI} from "../../../../config/apis/jikan/media.reference";
|
import {getContentReferenceAPI} from "../../../../config/apis/jikan/media.reference";
|
||||||
|
import {MediaFullInfoResponse} from "../../types/mediaFullInfo.type";
|
||||||
|
|
||||||
export const bulkInsertAnimeService = async (malId: number) => {
|
export const bulkInsertAnimeService = async (malId: number) => {
|
||||||
try {
|
try {
|
||||||
const { baseURL, getMediaFullInfo } = getContentReferenceAPI(malId);
|
const { baseURL, getMediaFullInfo } = getContentReferenceAPI(malId);
|
||||||
const mediaFullInfo = (await fetch(baseURL + getMediaFullInfo).then((res) => res.json())) as MediaFullInfoResponse;
|
const mediaFullInfo = (await fetch(baseURL + getMediaFullInfo).then((res) => res.json())) as MediaFullInfoResponse;
|
||||||
|
|
||||||
const insertedMedia = await InsertMediaRepository({
|
return await InsertMediaRepository({
|
||||||
payload: mediaFullInfo.data,
|
payload: mediaFullInfo.data,
|
||||||
});
|
});
|
||||||
|
|
||||||
return insertedMedia;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
ErrorForwarder(error);
|
ErrorForwarder(error);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user