From f91ff472d05f88e3bce6bf4c64b71f8b30a9f79f Mon Sep 17 00:00:00 2001 From: Saimon8420 Date: Thu, 20 Feb 2025 12:51:21 +0600 Subject: [PATCH] minor changes added --- .env | 3 ++ .gitignore | 2 +- package.json | 2 +- src/api/index.ts | 8 ++++- .../photoLibrary/photo.library.controller.ts | 21 +++++++++++++ src/api/photoLibrary/photo.library.route.ts | 31 +++++++++++++++++++ src/app.ts | 10 ++---- src/config/env.ts | 2 ++ 8 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 src/api/photoLibrary/photo.library.controller.ts create mode 100644 src/api/photoLibrary/photo.library.route.ts diff --git a/.env b/.env index 3d99658..0bee116 100644 --- a/.env +++ b/.env @@ -14,3 +14,6 @@ JWT_ACCESS_TOKEN_SECRET=planpostai%^$_%43%65576canvas%%$$ JWT_REFRESH_TOKEN_SECRET=planpostai!@43223_canvas$%^$349332$$ +PEXELS_URL=https://api.pexels.com/v1 +PEXELS_ACCESS_KEY=6PK7hLvOuG6nFsmC8c9EV0P8hGkyHeIhYpiRxhkEfh2ePK0GhiQypBhI + diff --git a/.gitignore b/.gitignore index 90edc0c..996fc0e 100644 --- a/.gitignore +++ b/.gitignore @@ -25,7 +25,7 @@ yarn-debug.log* yarn-error.log* # local env files -.env +.env .env.local .env.development.local .env.test.local diff --git a/package.json b/package.json index abb47f0..e4b3062 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "1.0.50", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "db:studio": "drizzle-kit studio", + "db:studio": "drizzle-kit studio --port=3000", "db:generate": "drizzle-kit generate", "db:migrate": "drizzle-kit migrate", "db:push": "drizzle-kit push:pg", diff --git a/src/api/index.ts b/src/api/index.ts index 95b3a11..5a7444f 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -3,12 +3,18 @@ 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"; export const api = new Elysia({ prefix: "/api", }); +api.get("/", () => { + return "Hello from PlanPostAI Canvas API"; +}); + api.use(authRoute); api.use(projectRoutes); api.use(uploadRoutes); -api.use(downloadRoute); \ No newline at end of file +api.use(downloadRoute); +api.use(photoLibraryRoutes); \ No newline at end of file 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 diff --git a/src/app.ts b/src/app.ts index 86f25ab..fda8f87 100644 --- a/src/app.ts +++ b/src/app.ts @@ -8,7 +8,8 @@ import { api } from "./api"; const allowedOrigins = [ "http://localhost:5175", "http://localhost:5173", - "https://your-production-site.com", + "https://dashboard.planpostai.com", + "https://canvas.planpostai.com", ]; const app = new Elysia({ @@ -22,7 +23,7 @@ const app = new Elysia({ credentials: true, })) .use(swagger({ - path: "/docs", + path: "/api/docs", documentation: { info: { title: "Canvas API", @@ -48,11 +49,6 @@ const app = new Elysia({ console.error(error) }); - -app.get("/", () => { - return "Hello from PlanPostAI Canvas API"; -}); - // all routes here app.use(api); diff --git a/src/config/env.ts b/src/config/env.ts index 8bd4ea4..a1888e1 100644 --- a/src/config/env.ts +++ b/src/config/env.ts @@ -11,4 +11,6 @@ export const ENV = { CLERK_SECRET_KEY: process.env.CLERK_SECRET_KEY, JWT_ACCESS_TOKEN_SECRET: process.env.JWT_ACCESS_TOKEN_SECRET, JWT_REFRESH_TOKEN_SECRET: process.env.JWT_REFRESH_TOKEN_SECRET, + PEXELS_URL: process.env.PEXELS_URL, + PEXELS_ACCESS_KEY: process.env.PEXELS_ACCESS_KEY, } \ No newline at end of file