Files
jotion/nextjs/app/api/documents/route.ts
Mayne0213 e82ea71c22 REFACTOR(repo): simplify project structure
- Move services/nextjs to nextjs/
- Move deploy/docker/Dockerfile.prod to Dockerfile
- Add GitHub Actions workflows (ci.yml, build.yml)
- Remove deploy/, services/, scripts/ folders
2026-01-05 02:29:10 +09:00

85 lines
2.0 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { withAuth } from '@/shared/lib/middleware'
import { db } from '@/shared/lib/db'
// GET /api/documents - Get all documents for the authenticated user
export const GET = withAuth(async (req: NextRequest, userId: string) => {
try {
const url = new URL(req.url)
const folderId = url.searchParams.get('folderId')
const documents = await db.document.findMany({
where: {
userId,
isArchived: false,
...(folderId && { folderId }),
},
include: {
folder: {
select: {
id: true,
name: true,
icon: true,
},
},
},
orderBy: {
createdAt: 'desc',
},
})
return NextResponse.json(documents)
} catch (error) {
console.error('Error fetching documents:', error)
return NextResponse.json(
{ error: 'Failed to fetch documents' },
{ status: 500 }
)
}
})
// POST /api/documents - Create a new document
export const POST = withAuth(async (req: NextRequest, userId: string) => {
try {
const { title, parentId, folderId } = await req.json()
if (!title) {
return NextResponse.json(
{ error: 'Title is required' },
{ status: 400 }
)
}
const document = await db.document.create({
data: {
title,
userId,
parentId: parentId || null,
folderId: folderId || null,
content: {
type: 'doc',
content: [
{
type: 'paragraph',
content: [
{
type: 'text',
text: 'Start writing...',
},
],
},
],
},
},
})
return NextResponse.json(document, { status: 201 })
} catch (error) {
console.error('Error creating document:', error)
return NextResponse.json(
{ error: 'Failed to create document' },
{ status: 500 }
)
}
})