Merge pull request '✨ feat: add get all media endpoint' (#8) from feat/get-all-media into main
All checks were successful
Sync to GitHub / sync (push) Successful in 8s
All checks were successful
Sync to GitHub / sync (push) Successful in 8s
Reviewed-on: #8
This commit is contained in:
20
src/modules/media/controllers/getAllMedia.controller.ts
Normal file
20
src/modules/media/controllers/getAllMedia.controller.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import { Context } from "elysia";
|
||||||
|
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||||
|
import { getAllMediaService } from "../services/http/getAllMedia.service";
|
||||||
|
import { returnReadResponse } from "../../../helpers/callback/httpResponse";
|
||||||
|
|
||||||
|
export const getAllMediaController = async (
|
||||||
|
ctx: Context & { query: { page: string } },
|
||||||
|
) => {
|
||||||
|
try {
|
||||||
|
const mediaData = await getAllMediaService(ctx.query.page);
|
||||||
|
return returnReadResponse(
|
||||||
|
ctx.set,
|
||||||
|
200,
|
||||||
|
"Media fetched successfully",
|
||||||
|
mediaData,
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
return mainErrorHandler(ctx.set, error);
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -1,6 +1,7 @@
|
|||||||
import Elysia from "elysia";
|
import Elysia from "elysia";
|
||||||
|
import { getAllMediaController } from "./controllers/getAllMedia.controller";
|
||||||
|
|
||||||
export const mediaModule = new Elysia({ prefix: "/media" }).get(
|
export const mediaModule = new Elysia({ prefix: "/media" }).get(
|
||||||
"/",
|
"/",
|
||||||
() => "Media Module",
|
getAllMediaController,
|
||||||
);
|
);
|
||||||
|
|||||||
17
src/modules/media/repositories/GET/getAllMedia.repository.ts
Normal file
17
src/modules/media/repositories/GET/getAllMedia.repository.ts
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import { AppError } from "../../../../helpers/error/instances/app";
|
||||||
|
import { mediaModel } from "../../model";
|
||||||
|
|
||||||
|
export const getAllMediaRepository = async (page: number) => {
|
||||||
|
try {
|
||||||
|
const limit = 10;
|
||||||
|
return await mediaModel.findMany({
|
||||||
|
take: limit,
|
||||||
|
skip: (page - 1) * limit,
|
||||||
|
where: {
|
||||||
|
deletedAt: null,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
throw new AppError(500, "Failed to get all media from repository", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
15
src/modules/media/services/http/getAllMedia.service.ts
Normal file
15
src/modules/media/services/http/getAllMedia.service.ts
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
|
||||||
|
import { getAllMediaRepository } from "../../repositories/GET/getAllMedia.repository";
|
||||||
|
|
||||||
|
export const getAllMediaService = async (pagination: string) => {
|
||||||
|
try {
|
||||||
|
const page =
|
||||||
|
/^\d+$/.test(pagination) && Number(pagination) > 0
|
||||||
|
? Number(pagination)
|
||||||
|
: 1;
|
||||||
|
|
||||||
|
return getAllMediaRepository(page);
|
||||||
|
} catch (error) {
|
||||||
|
ErrorForwarder(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user