Skip to content
This repository has been archived by the owner on Feb 10, 2025. It is now read-only.

Commit

Permalink
feat: add normalization for Folder and Item path
Browse files Browse the repository at this point in the history
  • Loading branch information
ziteh committed Sep 5, 2024
1 parent cf811b9 commit 6896e20
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
16 changes: 14 additions & 2 deletions app/api/folders/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ export async function POST(request: Request) {
const { name, path } = await request.json();
console.debug("Received data:", { name, path });

// Normalization
let fmtPath = path.replace(/\\/g, "/"); // Replace backslashes with forward
if (!fmtPath.endsWith("/")) {
fmtPath += "/"; // Always add trailing slash
}

const created = await prisma.folder.create({
data: { name, path },
data: { name, path: fmtPath },
});

return NextResponse.json(created);
Expand All @@ -25,9 +31,15 @@ export async function PATCH(request: Request) {
try {
const { id, name, path } = await request.json();

// Normalization
let fmtPath = path.replace(/\\/g, "/"); // Replace backslashes with forward
if (!fmtPath.endsWith("/")) {
fmtPath += "/"; // Always add trailing slash
}

const updated = await prisma.folder.update({
where: { id },
data: { name, path },
data: { name, path: fmtPath },
});

return NextResponse.json(updated);
Expand Down
18 changes: 9 additions & 9 deletions app/api/items/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ export async function POST(request: Request) {
const { path, folderId, name, starred } = await request.json();
console.debug("Received data:", { path, name, starred });

// Normalization
const fmtPath = path.replace(/\\/g, "/"); // Replace backslashes with forward

const item = await prisma.item.create({
data: { path, folderId, name, starred },
data: { path: fmtPath, folderId, name, starred },
});

return NextResponse.json(item);
Expand All @@ -19,17 +22,14 @@ export async function POST(request: Request) {

export async function PATCH(request: Request) {
try {
const {
id,
basePathId: folderId,
path,
name,
starred,
} = await request.json();
const { id, folderId, path, name, starred } = await request.json();

// Normalization
const fmtPath = path.replace(/\\/g, "/"); // Replace backslashes with forward

const item = await prisma.item.update({
where: { id },
data: { path, folderId, name, starred },
data: { path: fmtPath, folderId, name, starred },
});

return NextResponse.json(item);
Expand Down
5 changes: 0 additions & 5 deletions app/lib/folders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ export async function createFolder(
name: string,
path: string
): Promise<Folder | null> {
path = path.replace(/\\/g, "/"); // Replace backslashes with forward
if (!path.endsWith("/")) {
path += "/"; // Always add trailing slash
}

try {
const response = await fetch(apiUrl, {
method: "POST",
Expand Down

0 comments on commit 6896e20

Please sign in to comment.