Merge pull request 'media-detail' (#31) from media-detail into main
All checks were successful
Sync to GitHub / sync (push) Successful in 9s
All checks were successful
Sync to GitHub / sync (push) Successful in 9s
Reviewed-on: #31
This commit is contained in:
@ -1,17 +1,14 @@
|
||||
import { AppError } from "../../../../helpers/error/instances/app";
|
||||
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
|
||||
import { getMediaIdFromSlugRepository } from "../../../media/repositories/GET/getMediaIdFromSlug.repository";
|
||||
import { selectMediaIdFromSlugRepository } from "../../../media/repositories/SELECT/selectMediaIdFromSlug.repository";
|
||||
import { GetEpisodeDetailsParams } from "../../controllers/getEpisodeDetails.controller";
|
||||
import { getEpisodeDetailsRepository } from "../../repositories/GET/getEpisodeDetails.repository";
|
||||
|
||||
export const getEpisodeDetailsService = async (
|
||||
params: GetEpisodeDetailsParams,
|
||||
) => {
|
||||
export const getEpisodeDetailsService = async (params: GetEpisodeDetailsParams) => {
|
||||
try {
|
||||
if (!params.mediaSlug || !params.episode)
|
||||
throw new AppError(400, "Media slug and episode are required.");
|
||||
if (!params.mediaSlug || !params.episode) throw new AppError(400, "Media slug and episode are required.");
|
||||
|
||||
const mediaId = await getMediaIdFromSlugRepository(params.mediaSlug);
|
||||
const mediaId = await selectMediaIdFromSlugRepository(params.mediaSlug);
|
||||
if (!mediaId?.id) throw new AppError(404, "Media not found.");
|
||||
|
||||
const result = await getEpisodeDetailsRepository({
|
||||
|
||||
@ -1,27 +1,20 @@
|
||||
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
|
||||
import { MediaEpisodeInfoResponse } from "../../types/mediaEpisodeInfo.type";
|
||||
import { getMediaByMalIdRepository } from "../../../media/repositories/GET/getMediaByMalId.repository";
|
||||
import { AppError } from "../../../../helpers/error/instances/app";
|
||||
import { SystemAccountId } from "../../../../config/account/system";
|
||||
import { bulkInsertEpisodesRepository } from "../../repositories/bulkInsertEpisodes.repository";
|
||||
import { getEpisodeReferenceAPI } from "../../../../config/apis/jikan/episode.reference";
|
||||
import { selectMediaByMalIdRepository } from "../../../media/repositories/SELECT/selectMediaByMalId.repository";
|
||||
|
||||
export const bulkInsertEpisodeService = async (
|
||||
mal_id: number,
|
||||
page: number = 1,
|
||||
) => {
|
||||
export const bulkInsertEpisodeService = async (mal_id: number, page: number = 1) => {
|
||||
try {
|
||||
const episodeAPI = getEpisodeReferenceAPI(mal_id);
|
||||
const episodeData: MediaEpisodeInfoResponse = await fetch(
|
||||
`${episodeAPI.baseURL}${episodeAPI.getEpisodeList}?page=${page}`,
|
||||
).then((res) => res.json());
|
||||
|
||||
const mediaData = await getMediaByMalIdRepository(mal_id);
|
||||
if (!mediaData)
|
||||
throw new AppError(
|
||||
404,
|
||||
`Media with Mal ID ${mal_id} not found in database`,
|
||||
);
|
||||
const mediaData = await selectMediaByMalIdRepository(mal_id);
|
||||
if (!mediaData) throw new AppError(404, `Media with Mal ID ${mal_id} not found in database`);
|
||||
|
||||
const insertedEpisodeData = [];
|
||||
episodeData.data.forEach(async (episode) => {
|
||||
|
||||
@ -1,19 +1,16 @@
|
||||
import { Context } from "elysia";
|
||||
import { Context, Static } from "elysia";
|
||||
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||
import { getAllMediaService } from "../services/http/getAllMedia.service";
|
||||
import { returnReadResponse } from "../../../helpers/callback/httpResponse";
|
||||
import { getAllMediaSchema } from "../schemas/getAllMedia.schema";
|
||||
|
||||
export const getAllMediaController = async (
|
||||
ctx: Context & { query: { page: string } },
|
||||
) => {
|
||||
export const getAllMediaController = async (ctx: {
|
||||
set: Context["set"];
|
||||
query: Static<typeof getAllMediaSchema.query>;
|
||||
}) => {
|
||||
try {
|
||||
const mediaData = await getAllMediaService(ctx.query.page);
|
||||
return returnReadResponse(
|
||||
ctx.set,
|
||||
200,
|
||||
"Media fetched successfully",
|
||||
mediaData,
|
||||
);
|
||||
return returnReadResponse(ctx.set, 200, "Media fetched successfully", mediaData);
|
||||
} catch (error) {
|
||||
return mainErrorHandler(ctx.set, error);
|
||||
}
|
||||
|
||||
17
src/modules/media/controllers/getMediaBySlug.controller.ts
Normal file
17
src/modules/media/controllers/getMediaBySlug.controller.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { Context, Static } from "elysia";
|
||||
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||
import { getMediaBySlugSchema } from "../schemas/getMediaBySlug.schema";
|
||||
import { getMediaBySlugService } from "../services/http/getMediaBySlug.service";
|
||||
import { returnReadResponse } from "../../../helpers/callback/httpResponse";
|
||||
|
||||
export const getMediaBySlugController = async (ctx: {
|
||||
set: Context["set"];
|
||||
params: Static<typeof getMediaBySlugSchema.params>;
|
||||
}) => {
|
||||
try {
|
||||
const mediaData = await getMediaBySlugService(ctx.params.slug);
|
||||
return returnReadResponse(ctx.set, 200, "Media fetched successfully", mediaData);
|
||||
} catch (error) {
|
||||
return mainErrorHandler(ctx.set, error);
|
||||
}
|
||||
};
|
||||
@ -1,7 +1,9 @@
|
||||
import Elysia from "elysia";
|
||||
import { getAllMediaController } from "./controllers/getAllMedia.controller";
|
||||
import { getMediaBySlugController } from "./controllers/getMediaBySlug.controller";
|
||||
import { getMediaBySlugSchema } from "./schemas/getMediaBySlug.schema";
|
||||
import { getAllMediaSchema } from "./schemas/getAllMedia.schema";
|
||||
|
||||
export const mediaModule = new Elysia({ prefix: "/media" }).get(
|
||||
"/",
|
||||
getAllMediaController,
|
||||
);
|
||||
export const mediaModule = new Elysia({ prefix: "/media", tags: ["Media"] })
|
||||
.get("/", getAllMediaController, getAllMediaSchema)
|
||||
.get("/:slug", getMediaBySlugController, getMediaBySlugSchema);
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import { AppError } from "../../../../helpers/error/instances/app";
|
||||
import { mediaModel } from "../../model";
|
||||
|
||||
export const getAllMediaRepository = async (page: number) => {
|
||||
export const selectAllMediaRepository = async (page: number) => {
|
||||
try {
|
||||
const limit = 10;
|
||||
return await mediaModel.findMany({
|
||||
@ -1,7 +1,7 @@
|
||||
import { AppError } from "../../../../helpers/error/instances/app";
|
||||
import { mediaModel } from "../../model";
|
||||
|
||||
export const getMediaByMalIdRepository = async (mal_id: number) => {
|
||||
export const selectMediaByMalIdRepository = async (mal_id: number) => {
|
||||
try {
|
||||
return await mediaModel.findUnique({
|
||||
where: { malId: mal_id },
|
||||
@ -0,0 +1,12 @@
|
||||
import { AppError } from "../../../../helpers/error/instances/app";
|
||||
import { prisma } from "../../../../utils/databases/prisma/connection";
|
||||
|
||||
export const selectMediaBySlugRepository = async (slug: string) => {
|
||||
try {
|
||||
return await prisma.media.findUnique({
|
||||
where: { slug },
|
||||
});
|
||||
} catch (error) {
|
||||
throw new AppError(500, "Failed to fetch media by slug", error);
|
||||
}
|
||||
};
|
||||
@ -1,7 +1,7 @@
|
||||
import { AppError } from "../../../../helpers/error/instances/app";
|
||||
import { mediaModel } from "../../model";
|
||||
|
||||
export const getMediaIdFromSlugRepository = async (slug: string) => {
|
||||
export const selectMediaIdFromSlugRepository = async (slug: string) => {
|
||||
try {
|
||||
return await mediaModel.findUnique({
|
||||
where: { slug },
|
||||
58
src/modules/media/schemas/getAllMedia.schema.ts
Normal file
58
src/modules/media/schemas/getAllMedia.schema.ts
Normal file
@ -0,0 +1,58 @@
|
||||
import { t } from "elysia";
|
||||
import { AppRouteSchema } from "../../../helpers/types/AppRouteSchema";
|
||||
|
||||
export const getAllMediaSchema = {
|
||||
query: t.Object({
|
||||
page: t.String({ description: "The page number for pagination", default: "1" }),
|
||||
}),
|
||||
detail: {
|
||||
summary: "Fetch all media items with pagination",
|
||||
description:
|
||||
"Fetch a paginated list of all media items. The 'page' query parameter can be used to specify the page number for pagination.",
|
||||
responses: {
|
||||
200: {
|
||||
description: "Media items fetched successfully",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
success: { type: "boolean", example: true },
|
||||
status: { type: "number", example: 200 },
|
||||
message: { type: "string", example: "Media fetched successfully" },
|
||||
data: {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "object",
|
||||
properties: {
|
||||
status: { type: "string", example: "Finished Airing" },
|
||||
id: { type: "string", example: "12345" },
|
||||
title: { type: "string", example: "Example Media Title" },
|
||||
slug: { type: "string", example: "example-media-title" },
|
||||
malId: { type: "number", example: 67890 },
|
||||
pictureMedium: { type: "string", example: "https://example.com/medium.jpg" },
|
||||
pictureLarge: { type: "string", example: "https://example.com/large.jpg" },
|
||||
country: { type: "string", example: "JP" },
|
||||
score: { type: "number", example: 8.5 },
|
||||
startAiring: { type: "string", format: "date-time", example: "2023-01-01T00:00:00Z" },
|
||||
endAiring: { type: "string", format: "date-time", example: "2023-12-31T23:59:59Z" },
|
||||
synopsis: { type: "string", example: "This is an example synopsis of the media item." },
|
||||
ageRating: { type: "string", example: "PG-13" },
|
||||
mediaType: { type: "string", example: "Anime" },
|
||||
source: { type: "string", example: "Manga" },
|
||||
onDraft: { type: "boolean", example: false },
|
||||
uploadedBy: { type: "string", example: "admin" },
|
||||
deletedAt: { type: "string", format: "date-time", nullable: true, example: null },
|
||||
createdAt: { type: "string", format: "date-time", example: "2023-01-01T00:00:00Z" },
|
||||
updatedAt: { type: "string", format: "date-time", example: "2023-01-02T00:00:00Z" },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} satisfies AppRouteSchema;
|
||||
29
src/modules/media/schemas/getMediaBySlug.schema.ts
Normal file
29
src/modules/media/schemas/getMediaBySlug.schema.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import { t } from "elysia";
|
||||
import { AppRouteSchema } from "../../../helpers/types/AppRouteSchema";
|
||||
|
||||
export const getMediaBySlugSchema = {
|
||||
params: t.Object({
|
||||
slug: t.String({ description: "The slug of the media to fetch" }),
|
||||
}),
|
||||
detail: {
|
||||
summary: "Fetch a media item by its slug",
|
||||
description: "Fetch the specified media item using its slug. This endpoint returns the media details if found.",
|
||||
responses: {
|
||||
200: {
|
||||
description: "Media item fetched successfully",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
success: { type: "boolean", example: true },
|
||||
status: { type: "number", example: 200 },
|
||||
message: { type: "string", example: "Media fetched successfully" },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} satisfies AppRouteSchema;
|
||||
@ -1,14 +1,11 @@
|
||||
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
|
||||
import { getAllMediaRepository } from "../../repositories/GET/getAllMedia.repository";
|
||||
import { selectAllMediaRepository } from "../../repositories/SELECT/selectAllMedia.repository";
|
||||
|
||||
export const getAllMediaService = async (pagination: string) => {
|
||||
try {
|
||||
const page =
|
||||
/^\d+$/.test(pagination) && Number(pagination) > 0
|
||||
? Number(pagination)
|
||||
: 1;
|
||||
const page = /^\d+$/.test(pagination) && Number(pagination) > 0 ? Number(pagination) : 1;
|
||||
|
||||
return getAllMediaRepository(page);
|
||||
return selectAllMediaRepository(page);
|
||||
} catch (error) {
|
||||
ErrorForwarder(error);
|
||||
}
|
||||
|
||||
14
src/modules/media/services/http/getMediaBySlug.service.ts
Normal file
14
src/modules/media/services/http/getMediaBySlug.service.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import { AppError } from "../../../../helpers/error/instances/app";
|
||||
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
|
||||
import { selectMediaBySlugRepository } from "../../repositories/SELECT/selectMediaBySlug.repository";
|
||||
|
||||
export const getMediaBySlugService = async (slug: string) => {
|
||||
try {
|
||||
const mediaData = await selectMediaBySlugRepository(slug);
|
||||
if (!mediaData) throw new AppError(404, "Media not found with the provided slug.");
|
||||
|
||||
return mediaData;
|
||||
} catch (error) {
|
||||
ErrorForwarder(error);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user