canvas-backend/src/api/project/project.controller.ts
2025-02-01 17:43:17 +06:00

115 lines
4.6 KiB
TypeScript

import { eq } from "drizzle-orm";
import { db } from "../../db";
import { projects } from "../../db/schema";
import { createEmptyProject } from "../../helper/projects/createProject";
import { createBucket } from "../../helper/upload/createBucket";
import { removeBucket } from "../../helper/upload/removeBucket";
export const getEachProjects = async (id: string) => {
try {
const project = await db.select().from(projects).where(eq(projects.id, id)).limit(1);
if (project.length === 0) {
return { status: 404, message: "Project not found" };
}
return { status: 200, message: "Project fetched successfully", data: project[0] };
} catch (error: any) {
console.log(error.message);
return { status: 500, message: "An error occurred while fetching projects" };
}
};
export const getAllProjects = async (userId: string) => {
try {
// Fetch all projects for the given user
const allProjects = await db.select().from(projects).where(eq(projects.userId, userId));
if (allProjects.length === 0) {
return { status: 404, message: "No projects found" };
}
// Filter out projects where `object` is empty (null or an empty object)
const validProjects = [];
for (const project of allProjects) {
if (!project.object || Object.keys(project.object).length === 0) {
// Remove the project from the database
await db.delete(projects).where(eq(projects.id, project.id));
// Remove the associated MinIO bucket
await removeBucket(project.id);
} else {
validProjects.push(project);
}
}
if (validProjects.length === 0) {
return { status: 404, message: "No projects found" };
}
return { status: 200, message: "Projects fetched successfully", data: validProjects };
} catch (error: any) {
console.log(error.message);
return { status: 500, message: "An error occurred while fetching projects" };
}
};
export const createProject = async (userId: string) => {
try {
const { id } = await createEmptyProject(userId);
const bucket = await createBucket(id);
return { status: 200, message: "New project created successfully", data: { id, bucketName: bucket } };
} catch (error: any) {
console.log(error.message);
return { status: 500, message: "An error occurred while creating projects" }
}
}
export const updateProject = async (id: string, body: any) => {
try {
// 1. Validate if project exists
const existingProject = await db.select().from(projects).where(eq(projects.id, id)).limit(1);
if (existingProject.length === 0) {
return { status: 404, message: "Project not found" };
}
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
}).where(eq(projects.id, id)).returning();
if (updatedProject.length === 0) {
return { status: 500, message: "Failed to update the project" };
}
return { status: 200, message: "Project updated successfully", data: updatedProject[0] };
} catch (error: any) {
console.log("Error updating project:", error.message || error.toString());
return { status: 500, message: "An error occurred while updating the project" };
}
};
export const deleteProject = async (id: string) => {
try {
const deleteProject = await db.delete(projects).where(eq(projects.id, id)).returning({ id: projects.id });
if (deleteProject.length === 0) {
return { status: 404, message: "Project not found" };
}
const projectId = deleteProject[0].id;
const bucketDeletionResult = await removeBucket(projectId);
if (bucketDeletionResult.status !== 200) {
return { status: bucketDeletionResult.status, message: `Error deleting bucket: ${bucketDeletionResult.message}` };
}
return { status: 200, message: "Project and associated bucket deleted successfully" };
} catch (error: any) {
console.log("Error in deleteProject:", error.message || error.toString());
return { status: 500, message: "An error occurred while deleting the project" };
}
};