Compare commits
2 Commits
b27479cd3e
...
72f8e9e4eb
| Author | SHA1 | Date | |
|---|---|---|---|
| 72f8e9e4eb | |||
| 59228f7d1e |
@ -1,17 +1,16 @@
|
|||||||
import { Context, Static } from "elysia";
|
import { Context, Static } from "elysia";
|
||||||
import { mainErrorHandler } from "../../../helpers/error/handler";
|
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||||
import { getMediaBySlugSchema } from "../schemas/getMediaBySlug.schema";
|
import { getMediaBySlugSchema } from "../schemas/getMediaBySlug.schema";
|
||||||
|
import { getMediaBySlugService } from "../services/http/getMediaBySlug.service";
|
||||||
|
import { returnReadResponse } from "../../../helpers/callback/httpResponse";
|
||||||
|
|
||||||
export const getMediaBySlugController = async (ctx: {
|
export const getMediaBySlugController = async (ctx: {
|
||||||
set: Context["set"];
|
set: Context["set"];
|
||||||
params: Static<typeof getMediaBySlugSchema.params>;
|
params: Static<typeof getMediaBySlugSchema.params>;
|
||||||
}) => {
|
}) => {
|
||||||
try {
|
try {
|
||||||
return {
|
const mediaData = await getMediaBySlugService(ctx.params.slug);
|
||||||
success: true,
|
return returnReadResponse(ctx.set, 200, "Media fetched successfully", mediaData);
|
||||||
status: 200,
|
|
||||||
message: `Media with slug '${ctx.params.slug}' fetched successfully`,
|
|
||||||
};
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return mainErrorHandler(ctx.set, error);
|
return mainErrorHandler(ctx.set, error);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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);
|
||||||
|
}
|
||||||
|
};
|
||||||
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