try to solve swagger issue
This commit is contained in:
parent
1f57c95a4f
commit
d68db66cc4
2 changed files with 274 additions and 188 deletions
|
|
@ -6,156 +6,225 @@ import { createBucket } from "../../helper/upload/createBucket";
|
|||
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({
|
||||
id: projects.id,
|
||||
name: projects.name,
|
||||
description: projects.description,
|
||||
preview_url: projects.preview_url,
|
||||
object: projects.object,
|
||||
}).from(projects).where(eq(projects.userId, userId));
|
||||
try {
|
||||
// Fetch all projects for the given user
|
||||
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));
|
||||
|
||||
// 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)
|
||||
);
|
||||
// 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)
|
||||
);
|
||||
|
||||
// Delete projects with empty 'object' or empty 'object.objects'
|
||||
await Promise.all(
|
||||
projectsToDelete.map(async (proj) => {
|
||||
// Step 1: Delete associated uploads first
|
||||
await db.delete(uploads).where(eq(uploads.projectId, proj.id));
|
||||
// Delete projects with empty 'object' or empty 'object.objects'
|
||||
await Promise.all(
|
||||
projectsToDelete.map(async (proj) => {
|
||||
// Step 1: Delete associated uploads first
|
||||
await db.delete(uploads).where(eq(uploads.projectId, proj.id));
|
||||
|
||||
// Step 2: Delete the project itself
|
||||
await db.delete(projects).where(eq(projects.id, proj.id));
|
||||
// Step 2: Delete the project itself
|
||||
await db.delete(projects).where(eq(projects.id, proj.id));
|
||||
|
||||
// Step 3: Delete the associated bucket
|
||||
await removeBucket(proj.id);
|
||||
})
|
||||
);
|
||||
// Step 3: Delete the associated bucket
|
||||
await removeBucket(proj.id);
|
||||
})
|
||||
);
|
||||
|
||||
// Get remaining projects
|
||||
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)
|
||||
)
|
||||
);
|
||||
// Get remaining projects
|
||||
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)
|
||||
)
|
||||
);
|
||||
|
||||
if (remainingProjects.length === 0) {
|
||||
return { status: 404, message: "No projects found", 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 };
|
||||
if (remainingProjects.length === 0) {
|
||||
return { status: 404, message: "No projects found", 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,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
export const getEachProjects = async (id: string, token: string) => {
|
||||
try {
|
||||
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);
|
||||
if (project.length === 0) {
|
||||
return { status: 404, message: "Project not found", 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 };
|
||||
try {
|
||||
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);
|
||||
if (project.length === 0) {
|
||||
return { status: 404, message: "Project not found", 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,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
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 };
|
||||
|
||||
} catch (error: any) {
|
||||
console.log(error.message);
|
||||
return { status: 500, message: "An error occurred while creating projects", token }
|
||||
}
|
||||
try {
|
||||
const { id } = await createEmptyProject(userId);
|
||||
const bucket = await createBucket(id);
|
||||
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,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
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));
|
||||
if (existingProject.length === 0) {
|
||||
return { status: 404, message: "Project not found", token };
|
||||
}
|
||||
|
||||
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({
|
||||
object,
|
||||
name,
|
||||
description,
|
||||
preview_url,
|
||||
userId: user_id,
|
||||
}).where(eq(projects.id, id)).returning({
|
||||
id: projects.id,
|
||||
object: projects.object,
|
||||
name: projects.name,
|
||||
description: projects.description,
|
||||
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 };
|
||||
} catch (error: any) {
|
||||
console.log("Error updating project:", error.message || error.toString());
|
||||
return { status: 500, message: "An error occurred while updating the project", token };
|
||||
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));
|
||||
if (existingProject.length === 0) {
|
||||
return { status: 404, message: "Project not found", token };
|
||||
}
|
||||
|
||||
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({
|
||||
object,
|
||||
name,
|
||||
description,
|
||||
preview_url,
|
||||
userId: user_id,
|
||||
})
|
||||
.where(eq(projects.id, id))
|
||||
.returning({
|
||||
id: projects.id,
|
||||
object: projects.object,
|
||||
name: projects.name,
|
||||
description: projects.description,
|
||||
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,
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.log("Error updating project:", error.message || error.toString());
|
||||
return {
|
||||
status: 500,
|
||||
message: "An error occurred while updating the project",
|
||||
token,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
export const deleteProject = async (id: string, token: string) => {
|
||||
try {
|
||||
const deletedUploads = await db
|
||||
.delete(uploads)
|
||||
.where(eq(uploads.projectId, id))
|
||||
.returning({ id: uploads.id });
|
||||
try {
|
||||
const deletedUploads = await db
|
||||
.delete(uploads)
|
||||
.where(eq(uploads.projectId, id))
|
||||
.returning({ id: uploads.id });
|
||||
|
||||
if (deletedUploads.length >= 0) {
|
||||
// Step 4: Delete the project
|
||||
const deletedProject = await db
|
||||
.delete(projects)
|
||||
.where(eq(projects.id, id))
|
||||
.returning({ id: projects.id });
|
||||
if (deletedUploads.length >= 0) {
|
||||
// Step 4: Delete the project
|
||||
const deletedProject = await db
|
||||
.delete(projects)
|
||||
.where(eq(projects.id, id))
|
||||
.returning({ id: projects.id });
|
||||
|
||||
if (deletedProject.length === 0) {
|
||||
return { status: 404, message: "Project not found", token };
|
||||
}
|
||||
if (deletedProject.length === 0) {
|
||||
return { status: 404, message: "Project not found", token };
|
||||
}
|
||||
|
||||
// Step 5: Delete the associated bucket
|
||||
const bucketDeletionResult = await removeBucket(id);
|
||||
// Step 5: Delete the associated bucket
|
||||
const bucketDeletionResult = await removeBucket(id);
|
||||
|
||||
if (bucketDeletionResult.status !== 200) {
|
||||
return {
|
||||
status: bucketDeletionResult.status,
|
||||
message: `Error deleting bucket: ${bucketDeletionResult.message}`,
|
||||
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 };
|
||||
if (bucketDeletionResult.status !== 200) {
|
||||
return {
|
||||
status: bucketDeletionResult.status,
|
||||
message: `Error deleting bucket: ${bucketDeletionResult.message}`,
|
||||
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,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,87 +1,104 @@
|
|||
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({
|
||||
prefix: "/projects",
|
||||
tags: ["Projects"],
|
||||
detail: {
|
||||
description: "Routes for managing projects",
|
||||
}
|
||||
prefix: "/projects",
|
||||
tags: ["Projects"],
|
||||
detail: {
|
||||
description: "Routes for managing projects",
|
||||
},
|
||||
}).derive(async ({ cookie }) => {
|
||||
const authData = await verifyAuth(cookie);
|
||||
return { authData }; // Inject into context
|
||||
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;
|
||||
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;
|
||||
else {
|
||||
const userId = authData.userId;
|
||||
const token = authData.token;
|
||||
const response = await getAllProjects(userId, token);
|
||||
return response;
|
||||
}
|
||||
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;
|
||||
}
|
||||
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;
|
||||
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);
|
||||
return response;
|
||||
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
|
||||
);
|
||||
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(),
|
||||
})
|
||||
});
|
||||
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;
|
||||
const token = authData.token;
|
||||
const response = await deleteProject(project_id, token);
|
||||
return response;
|
||||
}
|
||||
}, {
|
||||
},
|
||||
{
|
||||
params: t.Object({
|
||||
project_id: t.String()
|
||||
})
|
||||
});
|
||||
|
||||
project_id: t.String(),
|
||||
}),
|
||||
}
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue