From f709707463485241bafb7c28e3a6182c5d4f1084 Mon Sep 17 00:00:00 2001 From: shb Date: Thu, 12 Jun 2025 13:17:33 +0800 Subject: [PATCH] Added patch functionality for renaming and moving folders Added extra functions: - Move folder into different directories. - Rename folders functionality --- server/api/dms/folder.patch.js | 91 ++++++++++++++++++++++++++++++++++ server/api/dms/folder.post.js | 1 - 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/server/api/dms/folder.patch.js b/server/api/dms/folder.patch.js index e69de29..a7e9662 100644 --- a/server/api/dms/folder.patch.js +++ b/server/api/dms/folder.patch.js @@ -0,0 +1,91 @@ +import { PrismaClient } from "@prisma/client"; + +const prisma = new PrismaClient(); + +export default defineEventHandler( async (event) => { + console.log("This is a test for a patch request."); + + const body = await readBody(event); + + /* + This is a sample of the expected body data structure: + { + function: "", ["rename", "move"] // Required to determine patch functionality. + cabinet_id: "", // REQUIRED to identify the folder to be patched. + new_name: "", // REQUIRED to rename the folder. + new_parent_id: "", // REQUIRED to move the folder to a new parent. + } + + */ + + // Function to check for body data. + if (!body.cabinet_id) { + return { + status: 400, + message: "Folder or cabinet_id is required", + } + } else { + + const folder = await prisma.cabinets.findUnique({ + where: { + cb_id: body.cabinet_id, + } + }); + + if (!folder) { + return { + status: 404, + message: "The requested folder was not found", + } + } + } + + switch (body.function) { + case "rename": + if (!body.new_name) { + return { + status: 400, + message: "new_name was not provided. The folder was not renamed.", + } + } + const renameFolder = await prisma.cabinets.update({ + where: { + cb_id: body.cabinet_id, + }, + data: { + cb_name: body.new_name, + } + }) + + return { + status: 200, + message: "Folder renamed successfully", + folder: renameFolder, + } + + case "move": + if (!body.new_parent_id || body.new_parent_id === null){ + return + } + const moveFolder = await prisma.cabinets.update({ + where: { + cb_id: body.cabinet_id, + }, + data: { + cb_parent_id: body.new_parent_id, + } + }) + + return { + status: 200, + message: "Folder moved successfully", + folder: moveFolder, + } + + default: + return { + status: 400, + message: "Invalid function", + } + } +}) \ No newline at end of file diff --git a/server/api/dms/folder.post.js b/server/api/dms/folder.post.js index 836f253..efa3082 100644 --- a/server/api/dms/folder.post.js +++ b/server/api/dms/folder.post.js @@ -1,5 +1,4 @@ import { PrismaClient } from "@prisma/client"; -// import { readBody } from "h3"; const prisma = new PrismaClient();