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) => {
try {
// Fetch all projects for the given user
const allProjects = await db.select({
const allProjects = await db
.select({
id: projects.id,
name: projects.name,
description: projects.description,
preview_url: projects.preview_url,
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
const projectsToDelete = allProjects.filter(proj =>
(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)
const projectsToDelete = allProjects.filter(
(proj) =>
(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'
@ -37,10 +45,15 @@ export const getAllProjects = async (userId: string, token: string) => {
);
// 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?.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)
)
);
@ -48,29 +61,51 @@ export const getAllProjects = async (userId: string, token: string) => {
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) {
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) => {
try {
const project = await db.select({
const project = await db
.select({
id: projects.id,
name: projects.name,
description: projects.description,
preview_url: projects.preview_url,
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) {
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) {
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 {
const { id } = await createEmptyProject(userId);
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) {
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 {
// 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) {
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;
// 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,
name,
description,
preview_url,
userId: user_id,
}).where(eq(projects.id, id)).returning({
})
.where(eq(projects.id, id))
.returning({
id: projects.id,
object: projects.object,
name: projects.name,
description: projects.description,
preview_url: projects.preview_url
preview_url: projects.preview_url,
});
if (updatedProject.length === 0) {
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) {
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 {
status: bucketDeletionResult.status,
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) {
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 { createProject, deleteProject, getAllProjects, getEachProjects, updateProject } from "./project.controller";
import {
createProject,
deleteProject,
getAllProjects,
getEachProjects,
updateProject,
} from "./project.controller";
import { verifyAuth } from "../../middlewares/auth.middlewares";
export const projectRoutes = new Elysia({
@ -7,29 +13,31 @@ export const projectRoutes = new Elysia({
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;
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()
})
});
project_id: t.String(),
}),
}
);
projectRoutes.get("/", async ({ authData }: any) => {
if (authData.status !== 200)
return authData;
if (authData.status !== 200) return authData;
else {
const userId = authData.userId;
const token = authData.token;
@ -39,8 +47,7 @@ projectRoutes.get("/", async ({ authData }: any) => {
});
projectRoutes.post("/create", async ({ authData }: any) => {
if (authData.status !== 200)
return authData;
if (authData.status !== 200) return authData;
else {
const userId = authData.userId;
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 }) => {
if (authData.status !== 200)
return authData;
projectRoutes.put(
"/update/:project_id",
async ({ body, params: { project_id }, authData }) => {
if (authData.status !== 200) return authData;
else {
const token = authData.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);
const response = await updateProject(
project_id,
body,
token,
user_id as string
);
return response;
}
}, {
},
{
params: t.Object({
project_id: t.String()
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;
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()
})
});
project_id: t.String(),
}),
}
);