diff --git a/env.example b/env.example new file mode 100644 index 0000000..ae2e459 --- /dev/null +++ b/env.example @@ -0,0 +1,21 @@ +SERVER_URL= +SERVER_PORT= + +DATABASE_URL= + +MINIO_ACCESS_KEY= +MINIO_SECRET_KEY= +MINIO_ENDPOINT= +MINIO_PORT= + +CLERK_SECRET_KEY= + +JWT_ACCESS_TOKEN_SECRET= + +JWT_REFRESH_TOKEN_SECRET= + +# developer canvas server url +CANVAS_SERVER_URL_DEV= + +PEXELS_URL= +PEXELS_ACCESS_KEY= \ No newline at end of file diff --git a/src/api/design/design.controller.ts b/src/api/design/design.controller.ts new file mode 100644 index 0000000..5ae6a76 --- /dev/null +++ b/src/api/design/design.controller.ts @@ -0,0 +1,18 @@ +import { ENV } from "../../config/env"; + +export const getAllDesign = async (token: string) => { + try { + const response = await fetch(`${ENV.CANVAS_SERVER_URL_DEV}/design`, { + method: "GET", + headers: { + Authorization: `Bearer ${token}`, + "Content-Type": "application/json", + } + }); + const data = await response.json(); + return data; + } catch (error: any) { + console.log(error); + return { status: 500, message: error.message, token }; + } +} \ No newline at end of file diff --git a/src/api/design/design.route.ts b/src/api/design/design.route.ts new file mode 100644 index 0000000..5db638c --- /dev/null +++ b/src/api/design/design.route.ts @@ -0,0 +1,24 @@ +import Elysia from "elysia"; +import { verifyAuth } from "../../middlewares/auth.middlewares"; +import { getAllDesign } from "./design.controller"; + +export const designRoute = new Elysia({ + prefix: "/design", + tags: ["Design"], + detail: { + description: "Routes for managing designs", + } +}).derive(async ({ cookie }) => { + const authData = await verifyAuth(cookie); + return { authData }; // Inject into context +}) + +designRoute.get("/", async ({ authData }) => { + if (authData.status !== 200) + return authData; + else { + const token = authData.token; + const response = await getAllDesign(token); + return response; + } +}) \ No newline at end of file diff --git a/src/api/index.ts b/src/api/index.ts index 6973475..646d124 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -3,6 +3,8 @@ 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 { designRoute } from "./design/design.route"; export const api = new Elysia({ prefix: "" }) .get("/", () => { @@ -13,6 +15,8 @@ export const api = new Elysia({ prefix: "" }) .use(projectRoutes) .use(uploadRoutes) .use(downloadRoute) + // .use(photoLibraryRoutes) + .use(designRoute) .onError(({ code, error, set }) => { console.error(`API Error: ${code}`, error); if (code === "NOT_FOUND") { diff --git a/src/api/project/project.controller.ts b/src/api/project/project.controller.ts index 8b3cd6c..838471b 100644 --- a/src/api/project/project.controller.ts +++ b/src/api/project/project.controller.ts @@ -86,10 +86,10 @@ export const createProject = async (userId: string, token: string) => { } }; -export const updateProject = async (id: string, body: any, token: string) => { +export const updateProject = async (id: string, body: any, token: string, user_id: string) => { try { // 1. Validate if project exists - const existingProject = await db.select().from(projects).where(eq(projects.id, id)).limit(1); + const existingProject = await db.select().from(projects).where(eq(projects.id, id)); if (existingProject.length === 0) { return { status: 404, message: "Project not found", token }; } @@ -101,7 +101,8 @@ export const updateProject = async (id: string, body: any, token: string) => { object, name, description, - preview_url + preview_url, + userId: user_id, }).where(eq(projects.id, id)).returning({ id: projects.id, object: projects.object, diff --git a/src/api/project/project.route.ts b/src/api/project/project.route.ts index 69f7f72..1f13f84 100644 --- a/src/api/project/project.route.ts +++ b/src/api/project/project.route.ts @@ -54,7 +54,9 @@ projectRoutes.put("/update/:project_id", async ({ body, params: { project_id }, return authData; else { const token = authData.token; - const response = await updateProject(project_id, body, token); + const user_id = authData?.userId; + // sending user_id to the controller to update the project with the user_id, when user tried to design a existing project from the design project panel + const response = await updateProject(project_id, body, token, user_id as string); return response; } }, { diff --git a/src/app.ts b/src/app.ts index 242b20a..7f0db74 100644 --- a/src/app.ts +++ b/src/app.ts @@ -4,15 +4,21 @@ import cors from "@elysiajs/cors"; import { ENV } from "./config/env"; import { api } from "./api"; -const app = new Elysia() +const allowedOrigins = [ + "http://localhost:5175", + "http://localhost:5174", + "http://localhost:5173", + "https://dashboard.planpostai.com", + "https://canvas.planpostai.com", +]; + +const app = new Elysia({ + prefix: "", + tags: ["Default"], +}) .use( cors({ - origin: [ - "http://localhost:5175", - "http://localhost:5173", - "https://dashboard.planpostai.com", - "https://canvas.planpostai.com", - ], + origin: allowedOrigins, methods: ["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"], allowedHeaders: [ "Content-Type", @@ -25,22 +31,37 @@ const app = new Elysia() credentials: true, }) ) - .get("/test", () => "Hello World", {}) - .use(api) .use( swagger({ - path: "/swagger", + path: "/api/docs", documentation: { - openapi: "3.1.0", info: { title: "Canvas API", version: "1.0.0", description: "Canvas API Documentation", }, + tags: [ + { + name: "Projects", + description: "All APIs related to Projects", + }, + { + name: "Uploads", + description: "All APIs related to Uploads", + }, + ], }, }) ) - .listen(ENV.SERVER_PORT); + .onError(({ code, error }) => { + if (code === "NOT_FOUND") return "Not Found :("; + console.log("hello from app.ts under error"); + console.error(error); + }); -console.log(`🦊 Elysia is running at ${ENV.SERVER_URL}`); -console.log(`Swagger docs available at ${ENV.SERVER_URL}/swagger`); +// all routes here +app.use(api); + +app.listen(ENV.SERVER_PORT, () => { + console.log(`🦊 Elysia is running at ${ENV.SERVER_URL}:${ENV.SERVER_PORT}`); +}); diff --git a/src/config/env.ts b/src/config/env.ts index 1d67220..96a24f7 100644 --- a/src/config/env.ts +++ b/src/config/env.ts @@ -1,6 +1,7 @@ import "dotenv/config"; export const ENV = { +<<<<<<< HEAD SERVER_URL: process.env.SERVER_URL, SERVER_PORT: process.env.SERVER_PORT || 5000, DATABASE_URL: process.env.DATABASE_URL, @@ -12,3 +13,19 @@ export const ENV = { JWT_ACCESS_TOKEN_SECRET: process.env.JWT_ACCESS_TOKEN_SECRET, JWT_REFRESH_TOKEN_SECRET: process.env.JWT_REFRESH_TOKEN_SECRET, }; +======= + SERVER_URL: process.env.SERVER_URL, + SERVER_PORT: process.env.SERVER_PORT || 5000, + CANVAS_SERVER_URL_DEV: process.env.CANVAS_SERVER_URL_DEV, + DATABASE_URL: process.env.DATABASE_URL, + MINIO_ACCESS_KEY: process.env.MINIO_ACCESS_KEY, + MINIO_SECRET_KEY: process.env.MINIO_SECRET_KEY, + MINIO_ENDPOINT: process.env.MINIO_ENDPOINT, + MINIO_PORT: process.env.MINIO_PORT, + 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, +} +>>>>>>> canvas_updated_be