✨ feat: add endpoint to fetch media by slug
This commit is contained in:
18
src/modules/media/controllers/getMediaBySlug.controller.ts
Normal file
18
src/modules/media/controllers/getMediaBySlug.controller.ts
Normal 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);
|
||||
}
|
||||
};
|
||||
@ -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);
|
||||
|
||||
@ -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 },
|
||||
@ -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 },
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user