Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
ziteh committed Oct 3, 2024
2 parents a10fc02 + d7a2d63 commit f052a1f
Show file tree
Hide file tree
Showing 26 changed files with 617 additions and 258 deletions.
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,10 @@ repos:
rev: v0.19.1
hooks:
- id: gitlint
- repo: local
hooks:
- id: prettify
name: prettify
entry: bash ./prettify-pre-commit.sh
language: system
files: \.(js|ts|jsx|tsx|css|html)$
15 changes: 0 additions & 15 deletions .vscode/settings.json

This file was deleted.

2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Types of changes:

### Changed

- Updated the API of items/{id}, items/relation, tags/relation
- Updated the layout of explorer showcase
- Move "New Tag/Item/Folder" button from database page to sidebar

### Security
Expand Down
27 changes: 21 additions & 6 deletions app/api/folders/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,33 @@ export async function PATCH(
request: Request,
{ params }: { params: { id: string } }
) {
let name: string | undefined;
let path: string | undefined;
try {
const { name, path } = await request.json();
const json = await request.json();
name = json.name;
let rawPath = json.path;

// Normalization
let fmtPath = path.replace(/\\/g, "/").trim(); // Replace backslashes with forward
if (!fmtPath.endsWith("/")) {
fmtPath += "/"; // Always add trailing slash
if (rawPath === undefined) {
path = undefined;
} else {
// Normalization
path = rawPath.replace(/\\/g, "/").trim(); // Replace backslashes with forward
if (path !== undefined && !path.endsWith("/")) {
path += "/"; // Always add trailing slash
}
}
} catch (error) {
return NextResponse.json(
{ error: `Error parsing request body, ${error}` },
{ status: StatusCodes.BAD_REQUEST }
);
}

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

return NextResponse.json(updated);
Expand Down
21 changes: 15 additions & 6 deletions app/api/folders/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@ import { StatusCodes } from "http-status-codes";

// Create a folder
export async function POST(request: Request) {
let name: string;
let path: string;
try {
const { name, path } = await request.json();
console.debug("Received data:", { name, path });
const json = await request.json();
name = json.name;

// Normalization
let fmtPath = path.replace(/\\/g, "/").trim(); // Replace backslashes with forward
if (!fmtPath.endsWith("/")) {
fmtPath += "/"; // Always add trailing slash
path = json.path.replace(/\\/g, "/").trim(); // Replace backslashes with forward
if (!path.endsWith("/")) {
path += "/"; // Always add trailing slash
}
} catch (error) {
return NextResponse.json(
{ error: `Error parsing request body, ${error}` },
{ status: StatusCodes.BAD_REQUEST }
);
}

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

return NextResponse.json(created);
Expand Down
30 changes: 24 additions & 6 deletions app/api/items/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ export async function DELETE(
{ params }: { params: { id: string } }
) {
try {
await prisma.itemRelation.deleteMany({
where: { itemId: Number(params.id) },
});

const deleted = await prisma.item.delete({
where: { id: Number(params.id) },
});

return NextResponse.json(deleted);
return NextResponse.json(deleted); // TODO change to 204?
} catch (error) {
console.error("Error deleting item:", error);
return NextResponse.json(
Expand All @@ -58,22 +62,36 @@ export async function PATCH(
request: Request,
{ params }: { params: { id: string } }
) {
let path: string | undefined;
let folderId: number | undefined;
let name: string | undefined;
let starred: boolean | undefined;
try {
const { folderId, path, name, starred } = await request.json();
const json = await request.json();
let rawPath = json.path;
folderId = json.folderId;
name = json.name;
starred = json.starred;

// Normalization
const fmtPath = path.replace(/\\/g, "/"); // Replace backslashes with forward
path = rawPath === undefined ? undefined : rawPath.replace(/\\/g, "/"); // Replace backslashes with forward
} catch (error) {
return NextResponse.json(
{ error: `Error parsing request body, ${error}` },
{ status: StatusCodes.BAD_REQUEST }
);
}

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

return NextResponse.json(item);
} catch (error) {
console.error("Error update item:", error);
return NextResponse.json(
{ error: "Error deleting item" },
{ error: "Error update item" },
{ status: StatusCodes.INTERNAL_SERVER_ERROR }
);
}
Expand Down
81 changes: 81 additions & 0 deletions app/api/items/relation/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { NextResponse } from "next/server";
import { prisma } from "@/app/lib/config/prisma";
import { StatusCodes } from "http-status-codes";

export async function PATCH(
request: Request,
{ params }: { params: { id: string } }
) {
let tagId: number | undefined;
let itemId: number | undefined;
try {
const json = await request.json();
tagId = json.tagId;
itemId = json.itemId;

console.debug("Received data:", { tagId, itemId });
} catch (error) {
return NextResponse.json(
{ error: `Error parsing request body, ${error}` },
{ status: StatusCodes.BAD_REQUEST }
);
}

try {
if (tagId !== undefined) {
const tag = await prisma.tag.findUnique({
where: { id: tagId },
});
if (tag === null) {
return NextResponse.json(
{ error: "Tag not found" },
{ status: StatusCodes.NOT_FOUND }
);
}
}

if (itemId !== undefined) {
const item = await prisma.item.findUnique({
where: { id: itemId },
});
if (item === null) {
return NextResponse.json(
{ error: "Item not found" },
{ status: StatusCodes.NOT_FOUND }
);
}
}

const updated = await prisma.itemRelation.update({
where: { id: Number(params.id) },
data: { tagId, itemId },
});

return NextResponse.json(updated);
} catch (error) {
console.error("Error update item relation:", error);
return NextResponse.json(
{ error: "Error updating item relation" },
{ status: StatusCodes.INTERNAL_SERVER_ERROR }
);
}
}

export async function DELETE(
_request: Request,
{ params }: { params: { id: string } }
) {
try {
const deleted = await prisma.tagRelation.delete({
where: { id: Number(params.id) },
});

return NextResponse.json(deleted);
} catch (error) {
console.error("Error deleting item relation:", error);
return NextResponse.json(
{ error: "Error deleting item relation" },
{ status: StatusCodes.INTERNAL_SERVER_ERROR }
);
}
}
74 changes: 31 additions & 43 deletions app/api/items/relation/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,51 @@ import { prisma } from "@/app/lib/config/prisma";
import { StatusCodes } from "http-status-codes";

export async function POST(request: Request) {
let tagId: number;
let itemId: number;
try {
const { tagId, itemId } = await request.json();
console.debug("Received data:", { tagId, itemId });

const created = await prisma.itemRelation.create({
data: { tagId, itemId },
});
const json = await request.json();
tagId = json.tagId;
itemId = json.itemId;

return NextResponse.json(created);
console.debug("Received data:", { tagId, itemId });
} catch (error) {
console.error("Error creating item relation:", error);
return NextResponse.json(
{ error: "Error creating item relation" },
{ status: StatusCodes.INTERNAL_SERVER_ERROR }
{ error: `Error parsing request body, ${error}` },
{ status: StatusCodes.BAD_REQUEST }
);
}
}

export async function PATCH(request: Request) {
try {
const { id, tagId, itemId } = await request.json();
const tag = await prisma.tag.findUnique({
where: { id: tagId },
});
if (tag === null) {
return NextResponse.json(
{ error: "Tag not found" },
{ status: StatusCodes.NOT_FOUND }
);
}

const item = await prisma.item.findUnique({
where: { id: itemId },
});
if (item === null) {
return NextResponse.json(
{ error: "Item not found" },
{ status: StatusCodes.NOT_FOUND }
);
}

const updated = await prisma.itemRelation.update({
where: { id },
const created = await prisma.itemRelation.create({
data: { tagId, itemId },
});

return NextResponse.json(updated);
return NextResponse.json(created);
} catch (error) {
console.error("Error update item relation:", error);
console.error("Error creating item relation:", error);
return NextResponse.json(
{ error: "Error updating item relation" },
{ error: "Error creating item relation" },
{ status: StatusCodes.INTERNAL_SERVER_ERROR }
);
}
Expand All @@ -52,28 +65,3 @@ export async function GET() {
);
}
}

export async function DELETE(request: Request) {
try {
const { id } = await request.json();

if (!id) {
return NextResponse.json(
{ error: "ID is required" },
{ status: StatusCodes.BAD_REQUEST }
);
}

const deleted = await prisma.tagRelation.delete({
where: { id },
});

return NextResponse.json(deleted);
} catch (error) {
console.error("Error deleting tag:", error);
return NextResponse.json(
{ error: "Error deleting tag" },
{ status: StatusCodes.INTERNAL_SERVER_ERROR }
);
}
}
22 changes: 18 additions & 4 deletions app/api/items/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,29 @@ import { StatusCodes } from "http-status-codes";

// Create a new item
export async function POST(request: Request) {
let path: string;
let folderId: number;
let name: string;
let starred: boolean;
try {
const { path, folderId, name, starred } = await request.json();
console.debug("Received data:", { path, name, starred });
const json = await request.json();
let rawPath = json.path;
folderId = json.folderId;
name = json.name;
starred = json.starred;

// Normalization
const fmtPath = path.replace(/\\/g, "/").trim(); // Replace backslashes with forward
path = rawPath.replace(/\\/g, "/").trim(); // Replace backslashes with forward
} catch (error) {
return NextResponse.json(
{ error: `Error parsing request body, ${error}` },
{ status: StatusCodes.BAD_REQUEST }
);
}

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

return NextResponse.json(item);
Expand Down
Loading

0 comments on commit f052a1f

Please sign in to comment.