try to solve swagger issue

This commit is contained in:
smfahim25 2025-03-19 13:02:19 +06:00
parent 1f57c95a4f
commit d68db66cc4
2 changed files with 274 additions and 188 deletions

View file

@ -8,18 +8,26 @@ import { removeBucket } from "../../helper/upload/removeBucket";
export const getAllProjects = async (userId: string, token: string) => { export const getAllProjects = async (userId: string, token: string) => {
try { try {
// Fetch all projects for the given user // Fetch all projects for the given user
const allProjects = await db.select({ const allProjects = await db
.select({
id: projects.id, id: projects.id,
name: projects.name, name: projects.name,
description: projects.description, description: projects.description,
preview_url: projects.preview_url, preview_url: projects.preview_url,
object: projects.object, object: projects.object,
}).from(projects).where(eq(projects.userId, userId)); })
.from(projects)
.where(eq(projects.userId, userId));
// Identify projects where 'object' is empty or 'object.objects' is empty // Identify projects where 'object' is empty or 'object.objects' is empty
const projectsToDelete = allProjects.filter(proj => const projectsToDelete = allProjects.filter(
(proj.object && typeof proj.object === "object" && Object.keys(proj.object).length === 0) || (proj) =>
(proj.object?.objects && Array.isArray(proj.object.objects) && proj.object.objects.length === 0) (proj.object &&
typeof proj.object === "object" &&
Object.keys(proj.object).length === 0) ||
(proj.object?.objects &&
Array.isArray(proj.object.objects) &&
proj.object.objects.length === 0)
); );
// Delete projects with empty 'object' or empty 'object.objects' // Delete projects with empty 'object' or empty 'object.objects'
@ -37,10 +45,15 @@ export const getAllProjects = async (userId: string, token: string) => {
); );
// Get remaining projects // Get remaining projects
const remainingProjects = allProjects.filter(proj => const remainingProjects = allProjects.filter(
(proj) =>
!( !(
(proj.object && typeof proj.object === "object" && Object.keys(proj.object).length === 0) || (proj.object &&
(proj.object?.objects && Array.isArray(proj.object.objects) && proj.object.objects.length === 0) typeof proj.object === "object" &&
Object.keys(proj.object).length === 0) ||
(proj.object?.objects &&
Array.isArray(proj.object.objects) &&
proj.object.objects.length === 0)
) )
); );
@ -48,29 +61,51 @@ export const getAllProjects = async (userId: string, token: string) => {
return { status: 404, message: "No projects found", token }; return { status: 404, message: "No projects found", token };
} }
return { status: 200, message: "Projects fetched successfully", data: remainingProjects, token }; return {
status: 200,
message: "Projects fetched successfully",
data: remainingProjects,
token,
};
} catch (error: any) { } catch (error: any) {
console.log(error.message); console.log(error.message);
return { status: 500, message: "An error occurred while fetching projects", token }; return {
status: 500,
message: "An error occurred while fetching projects",
token,
};
} }
}; };
export const getEachProjects = async (id: string, token: string) => { export const getEachProjects = async (id: string, token: string) => {
try { try {
const project = await db.select({ const project = await db
.select({
id: projects.id, id: projects.id,
name: projects.name, name: projects.name,
description: projects.description, description: projects.description,
preview_url: projects.preview_url, preview_url: projects.preview_url,
object: projects.object, object: projects.object,
}).from(projects).where(eq(projects.id, id)).limit(1); })
.from(projects)
.where(eq(projects.id, id))
.limit(1);
if (project.length === 0) { if (project.length === 0) {
return { status: 404, message: "Project not found", token }; return { status: 404, message: "Project not found", token };
} }
return { status: 200, message: "Project fetched successfully", data: project[0], token }; return {
status: 200,
message: "Project fetched successfully",
data: project[0],
token,
};
} catch (error: any) { } catch (error: any) {
console.log(error.message); console.log(error.message);
return { status: 500, message: "An error occurred while fetching projects", token }; return {
status: 500,
message: "An error occurred while fetching projects",
token,
};
} }
}; };
@ -78,18 +113,34 @@ export const createProject = async (userId: string, token: string) => {
try { try {
const { id } = await createEmptyProject(userId); const { id } = await createEmptyProject(userId);
const bucket = await createBucket(id); const bucket = await createBucket(id);
return { status: 200, message: "New project created successfully", data: { id, bucketName: bucket }, token }; return {
status: 200,
message: "New project created successfully",
data: { id, bucketName: bucket },
token,
};
} catch (error: any) { } catch (error: any) {
console.log(error.message); console.log(error.message);
return { status: 500, message: "An error occurred while creating projects", token } return {
status: 500,
message: "An error occurred while creating projects",
token,
};
} }
}; };
export const updateProject = async (id: string, body: any, token: string, user_id: string) => { export const updateProject = async (
id: string,
body: any,
token: string,
user_id: string
) => {
try { try {
// 1. Validate if project exists // 1. Validate if project exists
const existingProject = await db.select().from(projects).where(eq(projects.id, id)); const existingProject = await db
.select()
.from(projects)
.where(eq(projects.id, id));
if (existingProject.length === 0) { if (existingProject.length === 0) {
return { status: 404, message: "Project not found", token }; return { status: 404, message: "Project not found", token };
} }
@ -97,27 +148,40 @@ export const updateProject = async (id: string, body: any, token: string, user_i
const { object, name, description, preview_url } = body; const { object, name, description, preview_url } = body;
// The preview_url will come from client-side as well, where before updating the project a project capture will be taken and uploaded to the bucket. than the url will be sent to the server.And rest of them are normal process // The preview_url will come from client-side as well, where before updating the project a project capture will be taken and uploaded to the bucket. than the url will be sent to the server.And rest of them are normal process
const updatedProject = await db.update(projects).set({ const updatedProject = await db
.update(projects)
.set({
object, object,
name, name,
description, description,
preview_url, preview_url,
userId: user_id, userId: user_id,
}).where(eq(projects.id, id)).returning({ })
.where(eq(projects.id, id))
.returning({
id: projects.id, id: projects.id,
object: projects.object, object: projects.object,
name: projects.name, name: projects.name,
description: projects.description, description: projects.description,
preview_url: projects.preview_url preview_url: projects.preview_url,
}); });
if (updatedProject.length === 0) { if (updatedProject.length === 0) {
return { status: 500, message: "Failed to update the project", token }; return { status: 500, message: "Failed to update the project", token };
} }
return { status: 200, message: "Project updated successfully", data: updatedProject[0], token }; return {
status: 200,
message: "Project updated successfully",
data: updatedProject[0],
token,
};
} catch (error: any) { } catch (error: any) {
console.log("Error updating project:", error.message || error.toString()); console.log("Error updating project:", error.message || error.toString());
return { status: 500, message: "An error occurred while updating the project", token }; return {
status: 500,
message: "An error occurred while updating the project",
token,
};
} }
}; };
@ -146,16 +210,21 @@ export const deleteProject = async (id: string, token: string) => {
return { return {
status: bucketDeletionResult.status, status: bucketDeletionResult.status,
message: `Error deleting bucket: ${bucketDeletionResult.message}`, message: `Error deleting bucket: ${bucketDeletionResult.message}`,
token token,
}; };
} }
return { status: 200, message: "Project and associated bucket deleted successfully", token }; return {
status: 200,
message: "Project and associated bucket deleted successfully",
token,
};
} }
} catch (error: any) { } catch (error: any) {
console.log("Error in deleteProject:", error.message || error.toString()); console.log("Error in deleteProject:", error.message || error.toString());
return { status: 500, message: "An error occurred while deleting the project", token }; return {
status: 500,
message: "An error occurred while deleting the project",
token,
};
} }
}; };

View file

@ -1,5 +1,11 @@
import { Elysia, t } from "elysia"; import { Elysia, t } from "elysia";
import { createProject, deleteProject, getAllProjects, getEachProjects, updateProject } from "./project.controller"; import {
createProject,
deleteProject,
getAllProjects,
getEachProjects,
updateProject,
} from "./project.controller";
import { verifyAuth } from "../../middlewares/auth.middlewares"; import { verifyAuth } from "../../middlewares/auth.middlewares";
export const projectRoutes = new Elysia({ export const projectRoutes = new Elysia({
@ -7,29 +13,31 @@ export const projectRoutes = new Elysia({
tags: ["Projects"], tags: ["Projects"],
detail: { detail: {
description: "Routes for managing projects", description: "Routes for managing projects",
} },
}).derive(async ({ cookie }) => { }).derive(async ({ cookie }) => {
const authData = await verifyAuth(cookie); const authData = await verifyAuth(cookie);
return { authData }; // Inject into context return { authData }; // Inject into context
}); });
projectRoutes.get("/each/:project_id", async ({ params: { project_id }, authData }) => { projectRoutes.get(
if (authData.status !== 200) "/each/:project_id",
return authData; async ({ params: { project_id }, authData }) => {
if (authData.status !== 200) return authData;
else { else {
const token = authData.token; const token = authData.token;
const response = await getEachProjects(project_id, token); const response = await getEachProjects(project_id, token);
return response; return response;
} }
}, { },
{
params: t.Object({ params: t.Object({
project_id: t.String() project_id: t.String(),
}) }),
}); }
);
projectRoutes.get("/", async ({ authData }: any) => { projectRoutes.get("/", async ({ authData }: any) => {
if (authData.status !== 200) if (authData.status !== 200) return authData;
return authData;
else { else {
const userId = authData.userId; const userId = authData.userId;
const token = authData.token; const token = authData.token;
@ -39,8 +47,7 @@ projectRoutes.get("/", async ({ authData }: any) => {
}); });
projectRoutes.post("/create", async ({ authData }: any) => { projectRoutes.post("/create", async ({ authData }: any) => {
if (authData.status !== 200) if (authData.status !== 200) return authData;
return authData;
else { else {
const userId = authData.userId; const userId = authData.userId;
const token = authData.token; const token = authData.token;
@ -49,39 +56,49 @@ projectRoutes.post("/create", async ({ authData }: any) => {
} }
}); });
projectRoutes.put("/update/:project_id", async ({ body, params: { project_id }, authData }) => { projectRoutes.put(
if (authData.status !== 200) "/update/:project_id",
return authData; async ({ body, params: { project_id }, authData }) => {
if (authData.status !== 200) return authData;
else { else {
const token = authData.token; const token = authData.token;
const user_id = authData?.userId; 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 // 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); const response = await updateProject(
project_id,
body,
token,
user_id as string
);
return response; return response;
} }
}, { },
{
params: t.Object({ params: t.Object({
project_id: t.String() project_id: t.String(),
}), }),
body: t.Object({ body: t.Object({
object: t.Record(t.String(), t.Any()), // Allows any JSON object object: t.Record(t.String(), t.Any()), // Allows any JSON object
name: t.String(), name: t.String(),
description: t.String(), description: t.String(),
preview_url: t.String(), preview_url: t.String(),
}) }),
}); }
);
projectRoutes.delete("/delete/:project_id", async ({ params: { project_id }, authData }) => { projectRoutes.delete(
if (authData.status !== 200) "/delete/:project_id",
return authData; async ({ params: { project_id }, authData }) => {
if (authData.status !== 200) return authData;
else { else {
const token = authData.token; const token = authData.token;
const response = await deleteProject(project_id, token); const response = await deleteProject(project_id, token);
return response; return response;
} }
}, { },
{
params: t.Object({ params: t.Object({
project_id: t.String() project_id: t.String(),
}) }),
}); }
);