feat: add endpoint to fetch media by slug

This commit is contained in:
2026-04-01 23:28:22 +07:00
parent 697374d6cd
commit 17eb272b1d
9 changed files with 66 additions and 31 deletions

View File

@ -0,0 +1,18 @@
import { Context, Static } from "elysia";
import { mainErrorHandler } from "../../../helpers/error/handler";
import { getMediaBySlugSchema } from "../schemas/getMediaBySlug.schema";
export const getMediaBySlugController = async (ctx: {
set: Context["set"];
params: Static<typeof getMediaBySlugSchema.params>;
}) => {
try {
return {
success: true,
status: 200,
message: `Media with slug '${ctx.params.slug}' fetched successfully`,
};
} catch (error) {
return mainErrorHandler(ctx.set, error);
}
};

View File

@ -1,7 +1,8 @@
import Elysia from "elysia";
import { getAllMediaController } from "./controllers/getAllMedia.controller";
import { getMediaBySlugController } from "./controllers/getMediaBySlug.controller";
import { getMediaBySlugSchema } from "./schemas/getMediaBySlug.schema";
export const mediaModule = new Elysia({ prefix: "/media" }).get(
"/",
getAllMediaController,
);
export const mediaModule = new Elysia({ prefix: "/media" })
.get("/", getAllMediaController)
.get("/:slug", getMediaBySlugController, getMediaBySlugSchema);

View File

@ -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({

View File

@ -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 },

View File

@ -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 },

View 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;

View File

@ -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);
}