Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/app/api/favorites/batch/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { getUserIdFromRequest } from "@/lib/auth-server";
import { db, schema } from "@/lib/db";
import { and, eq } from "drizzle-orm";
import { NextRequest, NextResponse } from "next/server";

// POST /api/favorites/batch - 複数論文を一括でお気に入りに追加
export async function POST(request: NextRequest) {
const userId = await getUserIdFromRequest(request);
if (!userId) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}

if (!db) {
return NextResponse.json(
{ error: "Database not configured" },
{ status: 500 }
);
}

try {
const body = await request.json();
const { paperIds, folderId } = body;

if (!Array.isArray(paperIds) || paperIds.length === 0) {
return NextResponse.json(
{ error: "paperIds must be a non-empty array" },
{ status: 400 }
);
}

// folderId が指定されている場合、自分のフォルダか検証
if (folderId) {
const folderCheck = await db
.select()
.from(schema.folders)
.where(
and(
eq(schema.folders.id, folderId),
eq(schema.folders.userId, userId)
)
);
if (folderCheck.length === 0) {
return NextResponse.json(
{ error: "Folder not found or not authorized" },
{ status: 403 }
);
}
}

// 一括挿入(重複は無視)
const values = paperIds.map((paperId: number) => ({
userId,
paperId,
folderId: folderId || null,
}));

const inserted = await db
.insert(schema.favorites)
.values(values)
.onConflictDoNothing()
.returning();

return NextResponse.json(
{ added: inserted.length, total: paperIds.length },
{ status: 201 }
);
} catch (error) {
console.error("Error batch adding favorites:", error);
return NextResponse.json(
{ error: "Internal server error" },
{ status: 500 }
);
}
}
1 change: 1 addition & 0 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ export default function Home() {
<CategorizationResults
groupedPapers={groupedPapers}
categories={categorizationInfo.categories}
searchKeyword={keyword}
/>
</motion.main>
)}
Expand Down
2 changes: 1 addition & 1 deletion src/components/app-sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export function AppSidebar() {
<SidebarMenuSub>
{item.items?.map((subItem) => (
<SidebarMenuSubItem
key={subItem.title}
key={subItem.folderId || subItem.title}
className="group/sub-item relative"
>
<SidebarMenuSubButton asChild>
Expand Down
Loading