canvas-backend/src/api/project/project.route.ts
2025-02-05 15:24:06 +06:00

85 lines
2.4 KiB
TypeScript

import { Elysia, t } from "elysia";
import { createProject, deleteProject, getAllProjects, getEachProjects, updateProject } from "./project.controller";
import { verifyAuth } from "../../middlewares/auth.middlewares";
export const projectRoutes = new Elysia({
prefix: "/projects",
tags: ["Projects"],
detail: {
description: "Routes for managing projects",
}
}).derive(async ({ cookie }) => {
const authData = await verifyAuth(cookie);
return { authData }; // Inject into context
});
projectRoutes.get("/each/:project_id", async ({ params: { project_id }, authData }) => {
if (authData.status !== 200)
return authData;
else {
const token = authData.token;
const response = await getEachProjects(project_id, token);
return response;
}
}, {
params: t.Object({
project_id: t.String()
})
});
projectRoutes.get("/", async ({ authData }: any) => {
if (authData.status !== 200)
return authData;
else {
const userId = authData.userId;
const token = authData.token;
const response = await getAllProjects(userId, token);
return response;
}
});
projectRoutes.post("/create", async ({ authData }: any) => {
if (authData.status !== 200)
return authData;
else {
const userId = authData.userId;
const token = authData.token;
const response = await createProject(userId, token);
return response;
}
});
projectRoutes.put("/update/:project_id", async ({ body, params: { project_id }, authData }) => {
if (authData.status !== 200)
return authData;
else {
const token = authData.token;
const response = await updateProject(project_id, body, token);
return response;
}
}, {
params: t.Object({
project_id: t.String()
}),
body: t.Object({
object: t.Record(t.String(), t.Any()), // Allows any JSON object
name: t.String(),
description: t.String(),
preview_url: t.String(),
})
});
projectRoutes.delete("/delete/:project_id", async ({ params: { project_id }, authData }) => {
if (authData.status !== 200)
return authData;
else {
const token = authData.token;
const response = await deleteProject(project_id, token);
return response;
}
}, {
params: t.Object({
project_id: t.String()
})
});