diff --git a/src/api/index.ts b/src/api/index.ts index 646d124..2caf790 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -3,7 +3,7 @@ import { projectRoutes } from "./project/project.route"; import { uploadRoutes } from "./upload/upload.route"; import { authRoute } from "./auth/auth.route"; import { downloadRoute } from "./downloadCount/download.count.route"; -// import { photoLibraryRoutes } from "./photoLibrary/photo.library.route"; +import { photoLibraryRoutes } from "./photoLibrary/photo.library.route"; import { designRoute } from "./design/design.route"; export const api = new Elysia({ prefix: "" }) @@ -15,7 +15,7 @@ export const api = new Elysia({ prefix: "" }) .use(projectRoutes) .use(uploadRoutes) .use(downloadRoute) - // .use(photoLibraryRoutes) + .use(photoLibraryRoutes) .use(designRoute) .onError(({ code, error, set }) => { console.error(`API Error: ${code}`, error); diff --git a/src/api/photoLibrary/photo.library.controller.ts b/src/api/photoLibrary/photo.library.controller.ts new file mode 100644 index 0000000..82f9b0c --- /dev/null +++ b/src/api/photoLibrary/photo.library.controller.ts @@ -0,0 +1,21 @@ +import { ENV } from "../../config/env"; + +export const getPhotos = async (keyword: string, pre_page: number, token: string) => { + try { + const url = `${ENV.PEXELS_URL}/search?query=${keyword}&per_page=${pre_page}`; + const response = await fetch(url, { + headers: { + Authorization: process.env.PEXELS_ACCESS_KEY as string, + }, + }) + + if (!response.ok) { + return { status: 500, message: "An error occurred while getting the photos", token } + } + const data = await response.json(); + return { data, token } + } catch (error: any) { + console.log("Error in getting photos:", error.message || error.toString()); + return { status: 500, message: "An error occurred while getting the photos", token }; + } +} \ No newline at end of file diff --git a/src/api/photoLibrary/photo.library.route.ts b/src/api/photoLibrary/photo.library.route.ts new file mode 100644 index 0000000..7381c4c --- /dev/null +++ b/src/api/photoLibrary/photo.library.route.ts @@ -0,0 +1,31 @@ +import Elysia, { t } from "elysia"; +import { getPhotos } from "./photo.library.controller"; +import { verifyAuth } from "../../middlewares/auth.middlewares"; + +export const photoLibraryRoutes = new Elysia({ + prefix: "/photos", + tags: ["Photos"], + detail: { + description: "Routes for managing photo library", + } +}).derive(async ({ cookie }) => { + const authData = await verifyAuth(cookie); + return { authData }; // Inject into context +}); + +photoLibraryRoutes.get("/", async ({ query, authData +}) => { + if (authData.status !== 200) + return authData; + else { + const { keyword, per_page } = query; + const token = authData.token; + const data = await getPhotos(keyword, per_page, token); + return { data }; + } +}, { + query: t.Object({ + keyword: t.String(), + per_page: t.Number(), + }) +}) \ No newline at end of file