some bugs fixed
This commit is contained in:
parent
0e49fc53ab
commit
55b0a0300c
2 changed files with 40 additions and 7 deletions
|
|
@ -1,13 +1,19 @@
|
||||||
import { eq } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
import { db } from "../../db";
|
import { db } from "../../db";
|
||||||
import { projects } from "../../db/schema";
|
import { projects, uploads } from "../../db/schema";
|
||||||
import { createEmptyProject } from "../../helper/projects/createProject";
|
import { createEmptyProject } from "../../helper/projects/createProject";
|
||||||
import { createBucket } from "../../helper/upload/createBucket";
|
import { createBucket } from "../../helper/upload/createBucket";
|
||||||
import { removeBucket } from "../../helper/upload/removeBucket";
|
import { removeBucket } from "../../helper/upload/removeBucket";
|
||||||
|
|
||||||
export const getEachProjects = async (id: string, token: string) => {
|
export const getEachProjects = async (id: string, token: string) => {
|
||||||
try {
|
try {
|
||||||
const project = await db.select().from(projects).where(eq(projects.id, id)).limit(1);
|
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) {
|
if (project.length === 0) {
|
||||||
return { status: 404, message: "Project not found", token };
|
return { status: 404, message: "Project not found", token };
|
||||||
}
|
}
|
||||||
|
|
@ -21,7 +27,13 @@ export const getEachProjects = async (id: string, token: string) => {
|
||||||
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().from(projects).where(eq(projects.userId, userId));
|
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));
|
||||||
|
|
||||||
if (allProjects.length === 0) {
|
if (allProjects.length === 0) {
|
||||||
return { status: 404, message: "No projects found", token };
|
return { status: 404, message: "No projects found", token };
|
||||||
|
|
@ -79,7 +91,13 @@ export const updateProject = async (id: string, body: any, token: string) => {
|
||||||
name,
|
name,
|
||||||
description,
|
description,
|
||||||
preview_url
|
preview_url
|
||||||
}).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
|
||||||
|
});
|
||||||
|
|
||||||
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 };
|
||||||
|
|
@ -93,19 +111,34 @@ export const updateProject = async (id: string, body: any, token: string) => {
|
||||||
|
|
||||||
export const deleteProject = async (id: string, token: string) => {
|
export const deleteProject = async (id: string, token: string) => {
|
||||||
try {
|
try {
|
||||||
const deleteProject = await db.delete(projects).where(eq(projects.id, id)).returning({ id: projects.id });
|
// First, delete related records from the 'uploads' table
|
||||||
|
await db.delete(uploads).where(eq(uploads.projectId, id));
|
||||||
|
|
||||||
|
// Now delete the project
|
||||||
|
const deleteProject = await db
|
||||||
|
.delete(projects)
|
||||||
|
.where(eq(projects.id, id))
|
||||||
|
.returning({ id: projects.id });
|
||||||
|
|
||||||
if (deleteProject.length === 0) {
|
if (deleteProject.length === 0) {
|
||||||
return { status: 404, message: "Project not found", token };
|
return { status: 404, message: "Project not found", token };
|
||||||
}
|
}
|
||||||
|
|
||||||
const projectId = deleteProject[0].id;
|
const projectId = deleteProject[0].id;
|
||||||
|
|
||||||
|
// Delete the bucket associated with the project
|
||||||
const bucketDeletionResult = await removeBucket(projectId);
|
const bucketDeletionResult = await removeBucket(projectId);
|
||||||
|
|
||||||
if (bucketDeletionResult.status !== 200) {
|
if (bucketDeletionResult.status !== 200) {
|
||||||
return { status: bucketDeletionResult.status, message: `Error deleting bucket: ${bucketDeletionResult.message}`, token };
|
return {
|
||||||
|
status: bucketDeletionResult.status,
|
||||||
|
message: `Error deleting bucket: ${bucketDeletionResult.message}`,
|
||||||
|
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 };
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,7 @@ const app = new Elysia()
|
||||||
.onError(({ code, error }) => {
|
.onError(({ code, error }) => {
|
||||||
if (code === 'NOT_FOUND')
|
if (code === 'NOT_FOUND')
|
||||||
return 'Not Found :(';
|
return 'Not Found :(';
|
||||||
console.log("hello");
|
console.log("hello from app.ts under error");
|
||||||
console.error(error)
|
console.error(error)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue