media-detail #31
@ -9,7 +9,7 @@ export const getMediaBySlugController = async (ctx: {
|
||||
params: Static<typeof getMediaBySlugSchema.params>;
|
||||
}) => {
|
||||
try {
|
||||
const mediaData = getMediaBySlugService(ctx.params.slug);
|
||||
const mediaData = await getMediaBySlugService(ctx.params.slug);
|
||||
return returnReadResponse(ctx.set, 200, "Media fetched successfully", mediaData);
|
||||
} catch (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);
|
||||
}
|
||||
};
|
||||
@ -1,8 +1,13 @@
|
||||
import { AppError } from "../../../../helpers/error/instances/app";
|
||||
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
|
||||
import { selectMediaBySlugRepository } from "../../repositories/SELECT/selectMediaBySlug.repository";
|
||||
|
||||
export const getMediaBySlugService = (slug: string) => {
|
||||
export const getMediaBySlugService = async (slug: string) => {
|
||||
try {
|
||||
return `Mengambil media dengan slug '${slug}'`;
|
||||
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